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_)
这可以很好地作为颜色聚类的kmeans的输入。没有使用空间信息(像素位置),通过索引产生的像素列表很好。 – welch
谢谢@welch,我不确定 – Soltius