how to choose specific color to display when using kmean clustering color segmentation?

26 ビュー (過去 30 日間)
hi, i am learning how to segment colors by using kmean clustering just like the example in matlab 2015a. but each time i run the codes, the colors that i want are in different clusters. for example, for the first run,it will display that yellow is in cluster 1 and blue is in cluster 2. but when i run it again, they will switch to different cluster. how to make the yellow and blue is in specific clusters even if i run it again and again? please help me. thanks in advance
[FileName,PathName] = uigetfile('*.jpg','Select the MATLAB code file');
he1= imread(FileName);
cform = makecform('srgb2lab');
lab_he = applycform(he1,cform);
figure (2)
imshow (lab_he)
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure (3)
imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = he1;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure (4)
imshow(segmented_images{1}), title('objects in cluster 1');
figure (5)
imshow(segmented_images{2}), title('objects in cluster 2');
figure (6)
imshow(segmented_images{3}), title('objects in cluster 3');

採用された回答

Image Analyst
Image Analyst 2016 年 5 月 7 日
You have to pick your class colors that you want the classes displayed in. Then for each cluster, you have to find the mean color (centroid) of all the colors in the cluster. Then see which class color is closest (Euclidean distance) to your cluster color.
  2 件のコメント
Kumar Arindam Singh
Kumar Arindam Singh 2017 年 4 月 18 日
Could u give the steps on how to implement the above method?
Image Analyst
Image Analyst 2017 年 4 月 18 日
See attached demo. Let me know of anything you don't understand.

サインインしてコメントする。

その他の回答 (1 件)

badrul hisham
badrul hisham 2016 年 5 月 7 日
Thank you sir

Community Treasure Hunt

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

Start Hunting!

Translated by