how to set the centroid in kmean clustering so that the cluster values do not change

2 visualizzazioni (ultimi 30 giorni)
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');

Risposte (1)

Image Analyst
Image Analyst il 16 Apr 2016
If you have reference CIE LAB values, then simply compute the delta E of each pixel in the image to each of your reference colors. Then I think you can stack all the delta E images into a 3D array and use min() to get the class (ref color that is closest) for each pixel. For example, if you have 4 reference colors, then you will have 4 delta E images, and you can do (I think - untested)
deltaE1 = sqrt((lImage - refL1).^2 + (aImage - refA1).^2 + (bImage - refB1).^2);
deltaE2 = sqrt((lImage - refL2).^2 + (aImage - refA2).^2 + (bImage - refB2).^2);
deltaE3 = sqrt((lImage - refL3).^2 + (aImage - refA3).^2 + (bImage - refB3).^2);
deltaE4 = sqrt((lImage - refL4).^2 + (aImage - refA4).^2 + (bImage - refB4).^2);
deltaE = cat(3, deltaE1, deltaE2, deltaE3, deltaE4);
[~, classifiedImage] = min(deltaE, 3);

Community Treasure Hunt

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

Start Hunting!

Translated by