title: Finger peak detection using MATLAB
body: <p>I have to create an algorithm with Matlab that, with a image of a hand, can know the form of the hand by the number of raised fingers and the presence or absence of the thumb. So far, the algorithm is almost complete but I don't know what more I can do that could find the peaks that represents the fingers. We tried a lot of things but nothing works. The idea is to find when there is a sudden increasement but as the pixels are never completely aligned, nothing that we tried worked. Someone has any idea? Here is the code so far.</p>
  
  <p>The image that he is reading is this one:</p>
  
  <p><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRQsqJtlrOnSbJNTnj35Z0uG9BXsecX2AXn1vV0YDKodq-zSuqnnQ" alt=""></p>
  
  <p>To know if the finger is relevant or not, we already have an idea that might work... but we need to find the fingers first.</p>
  
  <pre><code>clear all
  close all
  
  image=imread('mao2.jpg');
  
  YCBCR = rgb2ycbcr(image);
  image=YCBCR;
  
  cb = image(:,:,2);
  cr = image(:,:,3);
  imagek(:,1) = cb(:);
  imagek(:,2) = cr(:);
  imagek = double(imagek);
  
  [IDX, C] = kmeans(imagek, 2, 'EmptyAction', 'singleton');
  
  s=size(image);
  IDX= uint8(IDX);
  C2=round(C);
  imageNew = zeros(s(1),s(2));
  temp = reshape(IDX, [s(1) s(2)]);
  for i = 1 : 1 : s(1)
   for j = 1 : 1 : s(2)
     imageNew(i,j,:) = C2(temp(i,j));
   end
  end
  imageNew=uint8(imageNew);
  
  [m,n]=size(imageNew);
  
  for i=1:1:m
      for j = 1:1:n
          if(imageNew(i,j)&gt;=127)
              pretobranco(i,j)=0;
          else
              pretobranco(i,j)=1;
          end
      end
  end
  
  I2=imfill(pretobranco);
  
  imshow(I2);
  imwrite(I2, 'mao1trab.jpg');
  
  
  [m,n]=size(I2);
  
  B=edge(I2);
  figure
  imshow(B);
  hold on;
  
  stats=regionprops(I2,'BoundingBox');
  
  rect=rectangle('position', [stats(1).BoundingBox(1), stats(1).BoundingBox(2), stats(1).BoundingBox(3), stats(1).BoundingBox(4)], 'EdgeColor', 'r');
  stats(1).BoundingBox(1)
  stats(1).BoundingBox(2)
  stats(1).BoundingBox(3)
  stats(1).BoundingBox(4)
  figure
  Bound = B( stats(1).BoundingBox(2): stats(1).BoundingBox(2)+stats(1).BoundingBox(4)-1, stats(1).BoundingBox(1):stats(1).BoundingBox(1)+stats(1).BoundingBox(3)-1);
  imshow(Bound)
  
  y1 = round(stats(1).BoundingBox(2))
  y2 = round(stats(1).BoundingBox(2)+stats(1).BoundingBox(4)-1)
  x1 = round(stats(1).BoundingBox(1))
  x2 = round(stats(1).BoundingBox(1)+stats(1).BoundingBox(3)-1)
  
  % Bounding box contida em imagem[M, N].
  
  [M,N] = size(Bound)
  vertical=0;
  horizontal=0;
  if M &gt; N
      vertical = 1 %imagem vertical
  else
      horizontal = 1 %imagem horizontal
  end
  
  %Find thumb
  
  MaoLeft = 0;
  MaoRight = 0;
  nPixelsBrancos = 0;
  
  if vertical==1
  for i = x1:1:x2
      for j= y1:1:y2
          if I2(j,i) == 1
              nPixelsBrancos = nPixelsBrancos + 1; %Numero de pixels da mão
          end
      end
  end
  
  for i=x1:1:x1+30
      for j=y1:1:y2
          if I2(j,i) == 1
              MaoLeft = MaoLeft + 1; %Number of pixels of the hand between the 30 first colums
          end
      end
  end
  
  for i=x2-30:1:x2
      for j=y1:1:y2
          if I2(j,1) == 1
              MaoRight = MaoRight + 1; %Number of pixels of the hand between the 30 last colums
          end
      end
  end
  
  TaxaBrancoLeft = MaoLeft/nPixelsBrancos
  TaxaBrancoRight = MaoRight/nPixelsBrancos
  
  if TaxaBrancoLeft &lt;= (7/100)
      if TaxaBrancoRight &lt;= (7/100)
          Thumb = 0 %Thumb in both borders is defined as no Thumb.
      else
          ThumbEsquerdo = 1 %Thumb on left
      end
  end
  
  if TaxaBrancoRight &lt;= (7/100) &amp;&amp; TaxaBrancoLeft &gt;= (7/100) 
      ThumbDireito = 1 %Thumb on right
  end
  end
  
  if horizontal==1
  for i = x1:1:x2
      for j= y1:y2
          if I2(i,j) == 1
              nPixelsBrancos = nPixelsBrancos + 1; %Numero de pixels da mão
          end
      end
  end
  
  for i=x1:1:x2
      for j=y1:1:y1+30
          if I2(i,j) == 1
              MaoLeft = MaoLeft + 1; %Numero de pixels da mão entre as 30 primeiras colunas
          end
      end
  end
  
  for i=x1:1:x2
      for j=y2-30:1:y2
          if I2(j,1) == 1
              MaoRight = MaoRight + 1; %Numero de pixels da mão entre as 30 ultimas colunas
          end
      end
  end
  TaxaBrancoLeft = MaoLeft/nPixelsBrancos
  TaxaBrancoRight = MaoRight/nPixelsBrancos
  
  if TaxaBrancoLeft &lt;= (7/100)
      if TaxaBrancoRight &lt;= (7/100)
          Thumb = 0 %Polegar nas duas bordas. Definimos como sem polegar.
      else
          ThumbEsquerdo = 1 %Polegar na borda esquerda
      end
  end
  
  if TaxaBrancoRight &lt;= (7/100) &amp;&amp; TaxaBrancoLeft &gt;= (7/100) 
      ThumbDireito = 1 %Polegar na borda direita
  end
  end
  figure
  imshow(I2);
  
  %detecção da centroid
  Ibw = im2bw(I2);
  
  Ilabel = bwlabel(Ibw);
  stat = regionprops(Ilabel,'centroid');
  figure
  imshow(I2); hold on;
  
  for x = 1: numel(stat)
      plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
  end
  
  centroid = [stat(x).Centroid(1) stat(x).Centroid(2)] %coordenadas x e y da centroid
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  </code></pre>