python實現(xiàn)LBP方法提取圖像紋理特征實現(xiàn)分類的步驟
題目描述
這篇博文是數(shù)字圖像處理的大作業(yè).
題目描述:給定40張不同風格的紋理圖片,大小為512*512,要求將每張圖片分為大小相同的9塊,利用其中的5塊作為訓練集,剩余的4塊作為測試集,構建適當?shù)哪P蛯崿F(xiàn)圖片的分類.
圖片如下圖所示:
分析:由于數(shù)據(jù)集太小,所以神經網(wǎng)絡模型并不適合此類的圖像處理.就需要尋找方法提取圖像的紋理信息.本文采用LBP的方法提取圖像的紋理信息,然后轉化成直方圖作為圖像的特征,然后使用多分類的方法進行分類.
環(huán)境
python2.7,jupyter notebook,anaconda
實現(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)計圖像的直方圖 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)計圖像的直方圖 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;
訓練分類器
SVM支持向量機分類.
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)
實驗測試集結果的正確率為:90.6%
第一次使用python的numpy包,對其中的api是真的不熟悉,代碼還可以優(yōu)化.其中和matlab里的矩陣操作也有不少不同,但是關于機器學習的scikitlearn包確實很好用.
總結:結果的正確率不是很高,所以還是可以在分類器上優(yōu)化,或者尋找更好的特征提取的方式.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
selenium + python 獲取table數(shù)據(jù)的示例講解
今天小編就為大家分享一篇selenium + python 獲取table數(shù)據(jù)的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python利用beautifulSoup實現(xiàn)爬蟲
這篇文章主要介紹了python利用beautifulSoup實現(xiàn)爬蟲,需要的朋友可以參考下2014-09-09keras中epoch,batch,loss,val_loss用法說明
這篇文章主要介紹了keras中epoch,batch,loss,val_loss用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python實現(xiàn)GUI學生管理系統(tǒng)的示例代碼
這篇文章主要為大家介紹了如何留Python語言實現(xiàn)簡易的GUI學生管理系統(tǒng),文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考下2022-06-06