title: Clustering of Superpixels
body: <p>I have a homework about clustering of superpixels and I have some difficulties with it. I am allowed to use VLFeat library to find superpixels only. </p>
  
  <p><img src="https://i.stack.imgur.com/Ihjm7.jpg" alt="The image I am processing:"></p>
  
  <p>After finding superpixels, I am defining a Feature Vector for every superpixel with the values; [ (Average "R" value of all pixels in the superpixel) (Average "G" value of all pixels in the superpixel) (Average "B" value of all pixels in the superpixel) (Average "Hue" value of all pixels in the superpixel) (Average "Saturation" value of all pixels in the superpixel) (Average "Value" value of all pixels in the superpixel)].</p>
  
  <p>After that I am sending feature vector and cluster number into built-in kmeans function to clustering superpixels.</p>
  
  <pre><code>function [] = segmentIt (impath, clusNum)
  
  %run vlfeat tools%    
  run ('vlfeat/toolbox/vl_setup');
  
  %read input image%
  image = imread (impath);
  imDouble = im2double (image);
  
  %save image W and H%
  IH = size (image, 1);
  IW = size (image, 2);
  
  %convert input image to single%
  imSingle = im2single (image);
  
  %get superpixel data%
  superPixels = vl_slic (imSingle, 80, 1);
  
  %how many superpixel do we have?%
  SPNUM = size (unique (superPixels), 1);
  
  %changing color space for feature vector%
  imHSV = rgb2hsv (image);
  
  %create feature vector with [averageR averageG averageB averageH averageS averageV]%
  FEATURE = zeros (SPNUM, 6);
  
  for i=0:SPNUM-1
      K = find (superPixels == i);
      L = zeros (size(K,1), 2);
      L(:,1) = ceil (K(:,1) ./ IW);
      L(:,2) = mod(K(:,1), IW) + 1;
      intensityTotalR = 0;
      intensityTotalG = 0;
      intensityTotalB = 0;
      vTotal = 0;
      sTotal = 0;
      hTotal = 0;
  
      for j = 1 : size (L, 1)
          intensityTotalR = intensityTotalR + ( imDouble (L(j,1), L(j,2), 1));
          intensityTotalG = intensityTotalG + ( imDouble (L(j,1), L(j,2), 2));
          intensityTotalB = intensityTotalB + ( imDouble (L(j,1), L(j,2), 3));
          vTotal = vTotal + imHSV (L(j,1), L(j,2), 3);
          sTotal = sTotal + imHSV (L(j,1), L(j,2), 2);
          hTotal = hTotal + imHSV (L(j,1), L(j,2), 1);
      end
  
      FEATURE(i+1,:) = [intensityTotalR/size(L,1) intensityTotalG/size(L,1) intensityTotalB/size(L,1) vTotal/size(L,1) sTotal/size(L,1) hTotal/size(L,1)]; 
  end
  
  RESULT = kmeans (FEATURE, clusNum);
  newIMAGE = zeros(IH, IW, 3);
  colorMultiplier = 1/(clusNum+5);
  
  for i=1:clusNum
      K = find (RESULT == i);
      for j = 1: size (K,1)
  
          L = find (superPixels == K(j,1));
          S = zeros (size(L,1), 2);
          S(:,1) = ceil (L(:,1) ./ IW);
          S(:,2) = ceil (mod (L(:,1), IW)) + 1;
  
          for z = 1:size(S,1)
              newIMAGE (S(z,1), S(z,2), 1) = colorMultiplier * i;
              newIMAGE (S(z,1), S(z,2), 2) = colorMultiplier * i;
              newIMAGE (S(z,1), S(z,2), 3) = 1;
          end
  
      end
  end
  
  imshow (newIMAGE)
  
  end
  </code></pre>
  
  <p>After running my function, the resultant image does not seem very good. Any idea what is missing in my code?</p>
  
  <p>Result image:</p>
  
  <p><img src="https://i.stack.imgur.com/Fm8RW.jpg" alt="Result Image"></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok, that's really weird. I recolour my superpixels and this is what I got.</p>
  
  <p><img src="https://i.stack.imgur.com/8lHTL.jpg" alt="Result of recolouring of superpixels"></p>
  
  <p><strong>EDIT-2</strong></p>
  
  <p>I edited my code in the light of what <code>lennon310</code> said. And wolaa. The problem was the way of my usage of find() function.</p>
  
  <p>That's the result when cluster number is equal to 5.</p>
  
  <p><img src="https://i.stack.imgur.com/cO6UF.jpg" alt="enter image description here"></p>
  
  <p>From now on, I will try to upgrade my feature vector to get better results.
  Thanks <code>lennon310</code> </p>