【问题标题】:grayscale image processing using k-mean使用 k-mean 进行灰度图像处理
【发布时间】: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) 输出图像始终是纯白色图像。 我尝试了下面链接中给出的解决方案,但在这种情况下,图像的输出是纯灰色图像。

find the solution tried here

2)当我第二次执行我的代码时,执行不会超出 k-mean 函数(它就像那里的无限循环)。因此在这种情况下没有输出。

【问题讨论】:

  • 你能发一张示例图片吗?
  • 这很好奇,因为如果我设置 30 nColors 那么我会得到 3 个集群作为摄影师图像。也许有更多kmeans经验的人可以提供帮助
  • 警告:在复制 1 期间在迭代 1 中创建了空集群
  • 这些警告可能是摄影师图像中只有 3 个集群的原因。
  • 我理解警告。该算法尝试聚类并陷入优化的局部最小值,其中一些聚类没有项目。我在回答中解释了它;)

标签: matlab image-processing k-means


【解决方案1】:

实际上,当您进行颜色分割时,kmeans 看起来会落在局部最小值中。这意味着它通常不会找到您想要的集群数量,因为最小化不是最好的(这就是为什么很多人使用其他类型的分割,例如水平集或简单的区域增长)。

一个选项是增加重复的数量(kmeans 尝试找到答案的次数)。目前您将其设置为 1,但您可以尝试 3 或 4,它可能会以这种方式达到解决方案。

this 问题中,接受的答案建议使用专门为图像分割创建的算法的kmeans 版本。我自己没试过,但我认为值得一试。

Link to FEX

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2011-03-11
    • 2020-10-22
    • 2015-09-09
    • 1970-01-01
    • 2013-01-20
    • 2023-01-30
    相关资源
    最近更新 更多