简体   繁体   English

修复 KMeans 集群 position

[英]Fix KMeans cluster position

I'm trying to use KMeans for clustering RGB colors and automatically count how many pixels of each group is present on an image.我正在尝试使用 KMeans 对 RGB colors 进行聚类,并自动计算图像上每组的像素数。 For that, I'm setting the initial position of centroids at positions I would like to categorize and running KMeans from sklearn.为此,我将质心的初始 position 设置在我想从 sklearn 分类和运行 KMeans 的位置。

The problem is, depending on the image, the algorithm output changes the order of the initial centroid vector, so when I count the number of elements, it goes to the wrong color.问题是,根据图像,算法 output 改变了初始质心向量的顺序,所以当我计算元素的数量时,它会变成错误的颜色。

This usually happens when I dont have one or more colors that are in initial centroids on the image.当我没有一个或多个位于图像初始质心的 colors 时,通常会发生这种情况。 In this case, I would like it to count 0 instead.在这种情况下,我希望它改为 0。

Does anyone knows how to fix the order of initial centroids on the output of KMeans prediction?有谁知道如何在 KMeans 预测的 output 上修复初始质心的顺序?

Code bellow:代码如下:

 centroid_start = np.array([[0,0,0],#Black
                           [38,64,87], #Col1
                           [43,68,98], #Col2
                           [23,42,45], #Col3
                           [160, 62, 0],#Col3
                           [153, 82, 33], #Col5
                           [198, 130, 109], #Col6
                           [100,105,79], #Col7
                           [220,138, 22]#Col8
                           ], np.float64)      
    image = cv.cvtColor(img, cv.COLOR_HSV2RGB)
    reshape=image.reshape((image.shape[0]*image.shape[1], 3))
    cluster = KMeans(n_clusters =np.shape(centroid_start[0], init =centroid_start).fit(reshape)
 pixels = Counter(cluster.labels_)
print(pixels)

The problem is:when I check 'pixels' variable, 0 not always correspond to black, 1 not always correspond to Col1, etc.问题是:当我检查“像素”变量时,0 并不总是对应于黑色,1 并不总是对应于 Col1,等等。

If you don't want the colors to migrate, you probably should not use k-means.如果您不希望 colors 迁移,您可能不应该使用 k-means。 Instead, just use pairwise distances between your colors and the image pixels, then select the color with smallest distance.相反,只需使用 colors 和图像像素之间的成对距离,然后使用 select 距离最小的颜色。

If you really do want the initial colors to migrate, then you have to accept that some of your initial cluster centers (colors) may disappear or potentially migrate to something very different than your initial colors.如果您确实希望迁移初始 colors,那么您必须接受您的一些初始集群中心(颜色)可能会消失或可能迁移到与初始 colors 非常不同的位置。 One option is to re-order the rows of the cluster_centers_ attribute (and possibly labels_ ) of your fitted KMeans object.一种选择是重新排序您拟合的KMeans object 的cluster_centers_属性(可能还有labels_ )的行。 Another - probably safer - option is to compute a mapping of fitted cluster centers to your original colors (again using pairwise distances), then translate the results of your subsequent k-means classification.另一个可能更安全的选项是计算拟合集群中心到原始 colors 的映射(再次使用成对距离),然后转换后续 k-means 分类的结果。 If you want to do it all in one step, you could subclass KMeans or wrap it by creating your own class derived from BaseEstimator .如果您想一步完成所有操作,您可以将KMeans子类化或通过创建自己的从 BaseEstimator 派生的class来包装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM