k-表示图像的分割

问题描述 投票:2回答:1

我遇到了以下代码,它使用num no来分割图像。通过k均值聚类算法得到聚类。但是,我无法理解第一个for循环中第二个语句的含义。请帮助我理解声明的作用以及~=的含义(这里)。

此外,当我运行代码时,我收到以下错误:

??? Attempt to grow array along ambiguous dimension.

Error in ==> kmeansseg at 42
    color(rgb_label ~= k) = 0;

似乎我为num=3以外的每个值都得到了这个错误。那么,这是否意味着我无法将rgb图像聚类成3种以上的颜色?输入图像有6种颜色,我可以分辨。有人可以为此提出解决方案吗?

功能调用:

>> f=imread('peppers.png'); 

>> kmeansseg(f,6)

这是代码:

function kmeansseg(im,num)


figure(1),imshow(im), title('original image');

cform = makecform('srgb2lab');
lab_im = applycform(im,cform);

ab = double(lab_im(:,:,2:3));

nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);

nColors = num;

[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
    'Replicates',3);

pixel_labels = reshape(cluster_idx,nrows,ncols);
figure(2),imshow(pixel_labels,[]), title('image labeled by cluster index');

segmented_images = cell(1,nColors);
rgb_label = repmat(pixel_labels,[1 1 nColors]);

for k = 1:nColors
    color = im;
    color(rgb_label ~= k) = 0;      %meaning?
    segmented_images{k} = color;
end


figure(3),imshow(segmented_images{1}), title('objects in cluster 1');

figure(4),imshow(segmented_images{2}), title('objects in cluster 2');

figure(5),imshow(segmented_images{3}), title('objects in cluster 3');



end
end
matlab image-processing k-means
1个回答
0
投票

它将图像中与该特定标签不对应的任何元素设置为零。这就是你获得一系列分段图像的方法。它从rgb_label变量中获取隔离标签。

什么〜=意味着“对于分割图像的每个像素都不等于当前分割数,将图像像素设置为零,而保持其他图像像素不变”

关于你的编辑 - 看起来颜色和rgb_label矩阵没有相同的尺寸。

© www.soinside.com 2019 - 2024. All rights reserved.