gpt4 book ai didi

matlab - 使用 k-mean 的灰度图像处理

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:52 25 4
gpt4 key购买 nike

我正在尝试将 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 函数(它就像一个无限循环)。因此在这种情况下没有输出。

最佳答案

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

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

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

Link to FEX

关于matlab - 使用 k-mean 的灰度图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com