title: why does kmean color segmentation show different color every time?
body: <p>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. 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</p>
  
  <p>This is the code that i use:</p>
  
  <pre><code>[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');
  %%
  a = im2bw (segmented_images{2},0.05);
  figure (7)
  imshow (a);
  
  b = im2bw (segmented_images{3},0.05);
  figure (8)
  imshow (b);
  </code></pre>
  
  <p>In my situation, the region with yellow color should be in cluster 2 and the area with blue region should be in cluster 3. Please show me how to do this</p>