python實(shí)現(xiàn)LBP方法提取圖像紋理特征實(shí)現(xiàn)分類的步驟
題目描述
這篇博文是數(shù)字圖像處理的大作業(yè).
題目描述:給定40張不同風(fēng)格的紋理圖片,大小為512*512,要求將每張圖片分為大小相同的9塊,利用其中的5塊作為訓(xùn)練集,剩余的4塊作為測(cè)試集,構(gòu)建適當(dāng)?shù)哪P蛯?shí)現(xiàn)圖片的分類.
圖片如下圖所示:

分析:由于數(shù)據(jù)集太小,所以神經(jīng)網(wǎng)絡(luò)模型并不適合此類的圖像處理.就需要尋找方法提取圖像的紋理信息.本文采用LBP的方法提取圖像的紋理信息,然后轉(zhuǎn)化成直方圖作為圖像的特征,然后使用多分類的方法進(jìn)行分類.
環(huán)境
python2.7,jupyter notebook,anaconda
實(shí)現(xiàn)
讀取數(shù)據(jù)
Numpy包數(shù)組操作API格式化數(shù)據(jù)
def loadPicture():
train_index = 0;
test_index = 0;
train_data = np.zeros( (200,171,171) );
test_data = np.zeros( (160,171,171) );
train_label = np.zeros( (200) );
test_label = np.zeros( (160) );
for i in np.arange(40):
image = mpimg.imread('picture/'+str(i)+'.tiff');
data = np.zeros( (513,513) );
data[0:image.shape[0],0:image.shape[1]] = image;
#切割后的圖像位于數(shù)據(jù)的位置
index = 0;
#將圖片分割成九塊
for row in np.arange(3):
for col in np.arange(3):
if index<5:
train_data[train_index,:,:] = data[171*row:171*(row+1),171*col:171*(col+1)];
train_label[train_index] = i;
train_index+=1;
else:
test_data[test_index,:,:] = data[171*row:171*(row+1),171*col:171*(col+1)];
test_label[test_index] = i;
test_index+=1;
index+=1;
return train_data,test_data,train_label,test_label;
特征提取
LBP特征提取方法
radius = 1;
n_point = radius * 8;
def texture_detect():
train_hist = np.zeros( (200,256) );
test_hist = np.zeros( (160,256) );
for i in np.arange(200):
#使用LBP方法提取圖像的紋理特征.
lbp=skft.local_binary_pattern(train_data[i],n_point,radius,'default');
#統(tǒng)計(jì)圖像的直方圖
max_bins = int(lbp.max() + 1);
#hist size:256
train_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins));
for i in np.arange(160):
lbp = skft.local_binary_pattern(test_data[i],n_point,radius,'default');
#統(tǒng)計(jì)圖像的直方圖
max_bins = int(lbp.max() + 1);
#hist size:256
test_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins));
return train_hist,test_hist;
訓(xùn)練分類器
SVM支持向量機(jī)分類.
import matplotlib.image as mpimg import matplotlib.pyplot as plt import numpy as np from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import SVR from skimage import feature as skft train_data,test_data,train_label,test_label= loadPicture(); train_hist,test_hist = texture_detect(); svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1); OneVsRestClassifier(svr_rbf,-1).fit(train_hist, train_label).score(test_hist,test_label)
實(shí)驗(yàn)測(cè)試集結(jié)果的正確率為:90.6%

第一次使用python的numpy包,對(duì)其中的api是真的不熟悉,代碼還可以優(yōu)化.其中和matlab里的矩陣操作也有不少不同,但是關(guān)于機(jī)器學(xué)習(xí)的scikitlearn包確實(shí)很好用.
總結(jié):結(jié)果的正確率不是很高,所以還是可以在分類器上優(yōu)化,或者尋找更好的特征提取的方式.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python+OpenCV繪制多instance的Mask圖像
Mask圖像中,不同值表示不同的實(shí)例(instance)。本文將詳細(xì)為大家講講如何利用OpenCV繪制多instance的Mask圖像,感興趣的可以學(xué)習(xí)一下2022-06-06
Python Pillow.Image 圖像保存和參數(shù)選擇方式
今天小編就為大家分享一篇Python Pillow.Image 圖像保存和參數(shù)選擇方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python+MongoDB自增鍵值的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狿ython+MongoDB自增鍵值的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Python-while 計(jì)算100以內(nèi)奇數(shù)和的方法
今天小編就為大家分享一篇Python-while 計(jì)算100以內(nèi)奇數(shù)和的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
詳解APScheduler如何設(shè)置任務(wù)不并發(fā)
本文主要介紹了APScheduler如何設(shè)置任務(wù)不并發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn)
本文主要介紹了Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn),主要使用img2pdf和PyPDF2軟件包,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Python中pandas的dataframe過濾數(shù)據(jù)方法
這篇文章主要介紹了Python中pandas的dataframe過濾數(shù)據(jù)方法,Pandas是另外一個(gè)用于處理高級(jí)數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析的Python庫,Pandas是基于Numpy構(gòu)建的一種工具,需要的朋友可以參考下2023-07-07

