title: hand gesture recognition in matlab
body: <p>I am doing gesture recognition in MATLAB. I need help regarding finger detection. I have detected the hand using skin color detection but don't know how to find the number of fingers. I have used <code>bwboundary</code> for boundaries detection in the image but it only works when the image is error free. That is, a slight disturbance in marking the hand area in the skin detection code will result in many boundaries. I would be very thankful if anybody just guides me in the right direction.</p>
  
  <p>Here in the <a href="https://imageshack.com/i/0fh4l3j" rel="nofollow">image after skin is detected and marked</a> I used <code>bwboundary</code> to find out the boundaries. I have also used <code>imfill</code> but it still didn't help me much.</p>
  
  <p>Following is the code </p>
  
  <pre><code>%Read the image, and capture the dimensions
  img_orig = imread('hand.jpg');
  height = size(img_orig,1);
  width = size(img_orig,2);
  
  %Initialize the output images
  out = img_orig;
  bin = zeros(height,width);
  
  %Convert the image from RGB to YCbCr
  img_ycbcr = rgb2ycbcr(img_orig);
  Cb = img_ycbcr(:,:,2);
  Cr = img_ycbcr(:,:,3);
  
  %Detect Skin
  [r,c,v] = find(Cb&gt;=77 &amp; Cb&lt;=127 &amp; Cr&gt;=133 &amp; Cr&lt;=173);
  numind = size(r,1);
  
  %Mark Skin Pixels
  for i=1:numind
      out(r(i),c(i),:) = [0 0 255];
      bin(r(i),c(i)) = 1;
  end
  imshow(img_orig);
  figure; imshow(out);
  figure; imshow(bin);
  
  %now for count of fingers
  
  img2=im2bw(bin,graythresh(bin));
  imshow(img2)
  img2=~img2;
  imshow(img2)
  B = bwboundaries(img2);
  imshow(img2)
  text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
  hold on
  </code></pre>