OpenCV的k - means聚类 -对图片进行颜色量化
?
OpenCV的k - means聚類
目標(biāo)
- 學(xué)習(xí)使用cv2.kmeans()數(shù)據(jù)聚類函數(shù)OpenCV
理解參數(shù)
輸入?yún)?shù)
樣品:它應(yīng)該的np.float32數(shù)據(jù)類型,每個特性應(yīng)該被放在一個單獨(dú)的列。
nclusters(K):數(shù)量的集群需要結(jié)束
標(biāo)準(zhǔn):這是迭代終止準(zhǔn)則。 當(dāng)這個標(biāo)準(zhǔn)是滿足,算法迭代停止。 實(shí)際上,它應(yīng)該是一個元組的三個參數(shù)。 他們是( type,max_iter,epsilon):
-
3. a -type 的終止條件:它有三個標(biāo)志如下:
cv2.TERM_CRITERIA_EPS——停止算法迭代如果指定的精度,ε是達(dá)到了。cv2.TERM_CRITERIA_MAX_ITER——停止指定數(shù)量的迭代算法后,max_iter。cv2。 TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER——任何上述條件時(shí)停止迭代。
- 3. b - max_iter整數(shù)指定最大迭代數(shù)。
- 3. c -epsilon 所需精度
嘗試:標(biāo)記來指定執(zhí)行的次數(shù)算法使用不同的初始標(biāo)簽。 算法返回標(biāo)簽,產(chǎn)生最佳的密實(shí)度。 這個密實(shí)度是作為輸出返回。
旗幟:這個標(biāo)志用于指定初始中心。 通常用于這兩個標(biāo)記:cv2.KMEANS_PP_CENTERS和cv2.KMEANS_RANDOM_CENTERS。
輸出參數(shù)
?
?
以下是示例代碼:
第一個基本語法 與 一維數(shù)據(jù)聚類
?
# -*- coding:utf-8 -*- __author__ = 'FontTian' __Date__ = '2017/5/13'import numpy as np import cv2 from matplotlib import pyplot as pltx = np.random.randint(25,100,25) y = np.random.randint(175,255,25) z = np.hstack((x,y)) z = z.reshape((50,1)) z = np.float32(z) plt.hist(z,256,[0,256]),plt.show()# Define criteria = ( type, max_iter = 10 , epsilon = 1.0 ) # 這是迭代終止準(zhǔn)則:type(A = TERM_CRITERIA_EPS 按照精度終止,B = TERM_CRITERIA_MAX_ITER,按照迭代次數(shù)終止,A+B 滿足任一條件時(shí)終止) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)# Set flags (Just to avoid line break in the code) # 用以指定初始中心 flags = cv2.KMEANS_RANDOM_CENTERS# Apply KMeans '''密實(shí)度 :這是每一個點(diǎn)的距離的平方和相應(yīng)的中心。標(biāo)簽 :這是標(biāo)簽陣列,其中每個元素標(biāo)記0,1.....中心 :這是一系列的集群中心。 ''' compactness,labels,centers = cv2.kmeans(z,2,None,criteria,10,flags) A = z[labels==0] B = z[labels==1] # Now plot 'A' in red, 'B' in blue, 'centers' in yellow plt.hist(A,256,[0,256],color = 'r') plt.hist(B,256,[0,256],color = 'b') plt.hist(centers,32,[0,256],color = 'y') plt.show()?
第二個:多維數(shù)據(jù)聚類
?
?
# -*- coding:utf-8 -*- __author__ = 'FontTian' __Date__ = '2017/5/13'import numpy as np import cv2 from matplotlib import pyplot as pltX = np.random.randint(25,50,(25,2)) Y = np.random.randint(60,85,(25,2)) Z = np.vstack((X,Y))# convert to np.float32 Z = np.float32(Z)# define criteria and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret,label,center=cv2.kmeans(Z,2,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)# Now separate the data, Note the flatten() A = Z[label.ravel()==0] B = Z[label.ravel()==1]# Plot the data plt.scatter(A[:,0],A[:,1]) plt.scatter(B[:,0],B[:,1],c = 'r') plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's') plt.xlabel('Height'),plt.ylabel('Weight') plt.show()?
第三個,圖片的顏色量化
?
?
# -*- coding:utf-8 -*- __author__ = 'FontTian' __Date__ = '2017/5/13' import numpy as np import cv2img = cv2.imread('spaceship.jpg') Z = img.reshape((-1,3)) # convert to np.float32 Z = np.float32(Z) j =0 # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) Klist = [2,4,6,8,10] for i in Klist:ret,label,center=cv2.kmeans(Z,i,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)j +=2# Now convert back into uint8, and make original imagecenter = np.uint8(center)res = center[label.flatten()]res2 = res.reshape((img.shape))cv2.imshow(str(("spaceship K=",i)), res2)cv2.waitKey(0) cv2.imshow('quondam image',img) cv2.waitKey(0) cv2.destroyAllWindows()
下面是我使用的示例圖片:
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的OpenCV的k - means聚类 -对图片进行颜色量化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV官方文档 理解k - mea
- 下一篇: 朴素贝叶斯分类器(Navie Bayes