程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python圖像紋理分割

編輯:Python
1️⃣作業要求

將下圖左右兩種不同類型的紋理區域分開,方法輸出結果是一幅與該圖像等大小的二值圖像,左邊為0,右邊為1,或者相反,灰色邊框線在設計的方法中不作考慮,自行去除。

2️⃣實現源碼
import matplotlib.image as mpimgimport matplotlib.pyplot as pltimport numpy as npfrom cv2 import cv2from sklearn.multiclass import OneVsRestClassifierfrom sklearn.svm import SVRfrom skimage import feature as skftfrom PIL import Image,ImageDraw,ImageFontclass Texture(): #n_point選取中心像素周圍的像素點的個數 #radius選取的區域的半徑 def __init__(self): self.radius = 1 self.n_point = self.radius * 8 #加載灰度圖片 def loadPicture(self): train_index = 0 test_index = 0 train_data = np.zeros((10, 171, 171)) test_data = np.zeros((8, 171, 171)) train_label = np.zeros(10) test_label = np.zeros(8) for i in np.arange(2): image = mpimg.imread('dataset1/'+str(i)+'.tiff') data = np.zeros((513, 513)) data[0:image.shape[0], 0:image.shape[1]] = image index = 0 #圖片分成9塊,5塊訓練4塊預測 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 #紋理檢測 def texture_detect(self): train_data, test_data, train_label, test_label = self.loadPicture() n_point = self.n_point radius = self.radius train_hist = np.zeros((10, 256)) test_hist = np.zeros((8, 256)) #LBP特征提取 for i in np.arange(10): # 使用LBP方法提取圖像的紋理特征. lbp=skft.local_binary_pattern(train_data[i], n_point, radius, 'default') # 統計圖像的直方圖 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(8): lbp = skft.local_binary_pattern(test_data[i], n_point, radius, 'default') 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支持向量機分類 def classifer(self): train_data, test_data, train_label, test_label = self.loadPicture() train_hist, test_hist = self.texture_detect() #構建SVM支持向量機 svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) model= OneVsRestClassifier(svr_rbf, -1) model.fit(train_hist, train_label) image=cv2.imread('dataset1/image.png',cv2.IMREAD_GRAYSCALE) image=cv2.resize(image,(588,294)) img_ku=skft.local_binary_pattern(image,8,1,'default') thresh=cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) #對圖像進行識別分類 img_data=np.zeros((1,98,98)) data=np.zeros((294,588)) data[0:image.shape[0],0:image.shape[1]]=image img = Image.open('texture.png') draw = ImageDraw.Draw(img) # 設置字體和大小 myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=180) # 設置字體顏色 fillcolor = "#008B8B" # 讀取圖片的size,也就是寬度和高度 width, height = img.size #通過行列將圖像分為九塊 for row in np.arange(3): for col in np.arange(6): img_data[0,:,:]=data[98*row:98*(row+1),98*col:98*(col+1)] lbp= skft.local_binary_pattern(img_data[0], 8, 1, 'default') max_bins=int(lbp.max()+1) img_hist, _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins)) predict=model.predict(img_hist.reshape(1,-1)) if(predict==0): draw.text((width/6, height/6), '0', font=myfont, fill=fillcolor) data[98*row:98*(row+1),98*col:98*(col+1)]=0 else: draw.text((2*width /3, height/6), '1', font=myfont, fill=fillcolor) data[98*row:98*(row+1),98*col:98*(col+1)]=255 plt.subplot(211) plt.imshow(img_ku,'gray') #plt.subplot(312) #plt.imshow(thresh,'gray') plt.subplot(212) plt.imshow(img) plt.show()if __name__ == '__main__': test = Texture() # 計算分類准確度部分 accuracy = test.classifer() #print('Final Accuracy = '+str(accuracy)+'%')
3️⃣實驗結果

上面一張圖是結果LBP算子特征提取後的圖片,下面一張圖使用SVM進行紋理分類後得到的結果,與預測的左上圖紋理相同標為0,與右上圖相同標為1。

實驗源碼+報告

在Linux的各大發行版中,Ubuntu及其衍生版本一直享有對用戶友好的美譽。Ubuntu是一個開源操作系統,它的系統和軟件可以在官方網站(http://cn.ubuntu.com)免費下載,並且提供了詳細的安裝方式說明。


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved