K-Mean 聚类中的质心

Centroids in K-Mean clustering

我正在尝试对灰度图像进行 k 均值聚类。

代码如下:

I = im2double(imread('sample.png'));
J = rgb2gray(I);
r = size(J,1);
c = size(J,2);
J = reshape(J,r*c,1);

[cluster_idx, cluster_center] = kmeans(J,k,'start','uniform','distance','sqEuclidean', 'Replicates', 3);

此处 cluster_idx 具有输入图像每一行的聚类索引。 cluster_center 具有簇质心位置。但这将是一个 k x 1 矩阵。我不明白的是这些值(双精度值)如何表示输入图像簇的质心?

cluster_center 表示每个簇的平均 灰度级 ,而 cluster_idx 为每个像素分配 簇成员.

只需 reshape 将您的聚类结果恢复为原始图像大小,并使用 cluster_idx 作为 cluster_center 索引:

J = im2double(imread('cameraman.tif'));
r = size(J,1);
c = size(J,2);
J = reshape(J,r*c,1);
k = 3;
[cluster_idx, cluster_center] = kmeans(J,k,'start','uniform','distance','sqEuclidean', 'Replicates', 3);

II = reshape(cluster_idx,[r c]);
JJ = cluster_center(II);
imshow(JJ);