【发布时间】:2015-06-28 02:53:12
【问题描述】:
我正在尝试将 rgb 图像转换为灰度图,然后使用 matlab 的 kmean 函数对其进行聚类。
这是我的代码
he = imread('tumor2.jpg');
%convert into a grayscale image
ab=rgb2gray(he);
nrows = size(ab,1);
ncols = size(ab,2);
%convert the image into a column vector
ab = reshape(ab,nrows*ncols,1);
%nColors=no of clusters
nColors = 3;
%cluster_idx is a n x 1 vector where cluster_idx(i) is the index of cluster assigned to ith pixel
[cluster_idx, cluster_center ,cluster_sum] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',1,'EmptyAction','drop' );
figure;
%converting vector into a matrix of dimensions equal to that of original
%image dimensions (nrows x ncols)
pixel_labels = reshape(cluster_idx,nrows,ncols);
pixel_labels
imshow(pixel_labels,[]), title('image labeled by cluster index');
问题
1) 输出图像始终是纯白色图像。 我尝试了下面链接中给出的解决方案,但在这种情况下,图像的输出是纯灰色图像。
2)当我第二次执行我的代码时,执行不会超出 k-mean 函数(它就像那里的无限循环)。因此在这种情况下没有输出。
【问题讨论】:
-
你能发一张示例图片吗?
-
这很好奇,因为如果我设置 30
nColors那么我会得到 3 个集群作为摄影师图像。也许有更多kmeans经验的人可以提供帮助 -
警告:在复制 1 期间在迭代 1 中创建了空集群
-
这些警告可能是摄影师图像中只有 3 个集群的原因。
-
我理解警告。该算法尝试聚类并陷入优化的局部最小值,其中一些聚类没有项目。我在回答中解释了它;)
标签: matlab image-processing k-means