2017-01-30 29 views
0

我试图找到使用K均值聚类的几个图像的3个主色。我面临的问题是K-means也聚集了图像的背景。我正在使用Python 2.7和OpenCV 3K均值颜色聚类 - 省略背景像素与蒙面numpy阵列

所有图像都具有以下RGB颜色的相同灰色背景:150,150,150。为了避免K-means对背景颜色进行聚类,我创建了一个蒙版数组,掩盖了来自原始图像数组的所有'150'像素值,理论上只留下数组中的非背景像素以供K-Means使用。但是,当我运行我的脚本时,它仍然返回灰色作为主色之一。

我的问题:是一个蒙面阵列要走的路(我做错了什么)或有更好的替代方法来排除像素K-均值聚类?

请在下面找到我的代码:

from sklearn.cluster import KMeans 
from sklearn import metrics 
import cv2 
import numpy as np 

def centroid_histogram(clt): 
    numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1) 
    (hist, _) = np.histogram(clt.labels_, bins=numLabels) 
    hist = hist.astype("float") 
    hist /= hist.sum() 
    return hist 

image = cv2.imread("test1.jpg") 
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 

h, w, _ = image.shape 
w_new = int(100 * w/max(w, h)) 
h_new = int(100 * h/max(w, h)) 
image = cv2.resize(image, (w_new, h_new)) 

image_array = image.reshape((image.shape[0] * image.shape[1], 3)) 
image_array = np.ma.masked_values(image_array,150) 

clt = KMeans(n_clusters=3) 
clt.fit(image_array) 

hist = centroid_histogram(clt) 
zipped = zip(hist, clt.cluster_centers_) 
zipped.sort(reverse=True, key=lambda x: x[0]) 

hist, clt.cluster_centers = zip(*zipped) 
print(clt.cluster_centers_) 

回答

1

如果你想提取比你的背景以外的像素的值,你可以使用numpy的指数化:

img2=image_array[image_array!=[150,150,150]] 
img2=img2.reshape((len(img2)/3,3)) 

这将产生清单不是[150,150,150]的像素。
但是,它不保留图像的结构,只是给你的像素列表。我不记得,但是对于K-means而言,您需要提供整个图像,也就是说您还需要将像素的位置提供给它。但是在这种情况下,掩蔽将无法提供帮助,因为掩蔽只是将某些像素的值替换为另一个像素的值,而不是一起去除像素。

+0

这可以很好地作为颜色聚类的kmeans的输入。没有使用空间信息(像素位置),通过索引产生的像素列表很好。 – welch

+0

谢谢@welch,我不确定 – Soltius