溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python實(shí)現(xiàn)LBP方法提取圖像紋理特征實(shí)現(xiàn)分類的步驟

發(fā)布時(shí)間:2020-10-04 10:13:38 來源:腳本之家 閱讀:563 作者:selous 欄目:開發(fā)技術(shù)

題目描述

這篇博文是數(shù)字圖像處理的大作業(yè).

題目描述:給定40張不同風(fēng)格的紋理圖片,大小為512*512,要求將每張圖片分為大小相同的9塊,利用其中的5塊作為訓(xùn)練集,剩余的4塊作為測(cè)試集,構(gòu)建適當(dāng)?shù)哪P蛯?shí)現(xiàn)圖片的分類.

圖片如下圖所示:

python實(shí)現(xiàn)LBP方法提取圖像紋理特征實(shí)現(xiàn)分類的步驟

分析:由于數(shù)據(jù)集太小,所以神經(jīng)網(wǎng)絡(luò)模型并不適合此類的圖像處理.就需要尋找方法提取圖像的紋理信息.本文采用LBP的方法提取圖像的紋理信息,然后轉(zhuǎn)化成直方圖作為圖像的特征,然后使用多分類的方法進(jìn)行分類.

環(huán)境

python2.7,jupyter notebook,anaconda

數(shù)據(jù)集的地址

實(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實(shí)現(xiàn)LBP方法提取圖像紋理特征實(shí)現(xiàn)分類的步驟

第一次使用python的numpy包,對(duì)其中的api是真的不熟悉,代碼還可以優(yōu)化.其中和matlab里的矩陣操作也有不少不同,但是關(guān)于機(jī)器學(xué)習(xí)的scikitlearn包確實(shí)很好用.

總結(jié):結(jié)果的正確率不是很高,所以還是可以在分類器上優(yōu)化,或者尋找更好的特征提取的方式.

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI