why does the value of kmean color segmentation changes every time?

1 回表示 (過去 30 日間)
badrul hisham
badrul hisham 2016 年 4 月 13 日
hi, may i know why does the value of kmean color segmentation changes every time i run it? basically, i am using this as my function:
[FileName,PathName] = uigetfile('*.jpg','Select the MATLAB code file');
he1 = imread(FileName);
imshow(he1), title('original image');
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');
a = im2bw (segmented_images{2},0.05);
figure (7)
imshow (a);
b = im2bw (segmented_images{3},0.05);
figure (8)
imshow (b);
area1 = bwarea (a);
round (area1);
area2 = bwarea (b);
round (area2);
calculate =(area1/area2);
ratio = round(calculate,2)
acc = 0.05;
result = round(ratio/acc)*acc
i want to use segmented_images{2} and segmented_images{3} as my preferred images but everytime i run it, the value of these images changes. can someone please show me a way to do this?
thank you.

回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by