How can I get constant clusters using this code?

1 次查看(过去 30 天)
I am using this code but getting different result for the desired cluster. Using the following code, the desired cluster for image hand.jpg is cluster 1 but for foot.jpg is cluster 4. I want the desired cluster number for every image not to be changed or any other ways that I can automatically pick the desired cluster for further analysis if the desired cluster number is changing.
I = imread(['ColorSpace\ForExperiment\foot.jpg']);
lab_he = rgb2lab(I);
% Extract first channel (L)
ab = lab_he(:, :, 2:3);
% Convert to data type single
ab = im2single(ab);
% Perform clustering using K-Means
nColors = 4;
pixel_labels = imsegkmeans(ab, nColors, "NumAttempts", 5);
imshow(pixel_labels, []);
% Display each cluster with their objects
mask1 = pixel_labels == 1;
cluster1 = I .* uint8(mask1);
imshow(cluster1); title('Objects in cluster1');
% Display second cluster's objects
mask2 = pixel_labels == 2;
cluster2 = I .* uint8(mask2);
imshow(cluster2); title('Objects in cluster2');
% Display third cluster's objects
mask3 = pixel_labels == 3;
cluster3 = I .* uint8(mask3);
imshow(cluster3); title('Objects in cluster3');
% Display fourth cluster's objects
mask4 = pixel_labels == 4;
cluster4 = I .* uint8(mask4);
imshow(cluster4); title('Objects in cluster4');
Any help is greatly appreciated.

回答(1 个)

Image Analyst
Image Analyst 2020-3-9
See my code for re-numbering clusters. Adapt as needed.

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by