我想使用单个图像制作8种颜色的群集。然后,我将这些群集用于几个不同的图像。
我试图更改KMeans的中心,但这让我
TypeError:只有大小为1的数组可以转换为Python标量
这是我的代码的一部分
img = mpimg.imread('filter3.jpg')
img1 = mpimg.imread('flower (1).jpg')
img2 = mpimg.imread('flower (2).jpg')
img3 = mpimg.imread('flower (3).jpg')
img4 = mpimg.imread('flower (4).jpg')
Z = img.reshape((-1,3))
Z1 = img1.reshape((-1,3))
Z2 = img2.reshape((-1,3))
Z3 = img3.reshape((-1,3))
Z4 = img4.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
Z1 = np.float32(Z1)
Z2 = np.float32(Z2)
Z3 = np.float32(Z3)
Z4 = np.float32(Z4)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
ret1,label1,center1=cv2.kmeans(Z1,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
ret2,label2,center2=cv2.kmeans(Z2,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
ret3,label3,center3=cv2.kmeans(Z3,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
ret4,label4,center4=cv2.kmeans(Z4,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
ret1a,label1a,center1a=cv2.kmeans(Z1,K,None,criteria,1,center)
ret2a,label2a,center2a=cv2.kmeans(Z2,K,None,criteria,1,center)
ret3a,label3a,center3a=cv2.kmeans(Z3,K,None,criteria,1,center)
ret4a,label4a,center4a=cv2.kmeans(Z4,K,None,criteria,1,center)
我希望输出是label1a直到label4a的直方图。
答案 0 :(得分:0)
不要滥用kmeans进行分类。
修复调色板后,只需将每个像素映射到最接近的颜色即可。此步骤无需kmean,无需在那里进行“学习”。