title: Finger tracking / counting using OpenCV convex hull and Convexity Defect functions
body: <p>I have been working on a basic hand/finger tracking code using OpenCV and the ConvexHull and ConvexityDefects method. </p>
  
  <p>Basically I am able to create a contour of the hand. I now need to be able to count the number of fingers. I know that the start and the end points of the Convex Hull are the finger tips but I am unsure how to count them and also how to highlight them by drawing circles on them or something. </p>
  
  <p>I want my code to perform something like <a href="http://www.youtube.com/watch?v=mGpTE1RkEvQ" rel="nofollow noreferrer">this</a>.  </p>
  
  <p>This is a sample part of my code so far:  </p>
  
  <pre><code>cvFindContours( hsv_mask, storage, &amp;contours, sizeof(CvContour), CV_RETR_LIST,         CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
  
  CvSeq* contours2 = NULL;
  
  CvRect rect = cvBoundingRect( contours2, 0 );
  
  cvRectangle( bitImage, cvPoint(rect.x, rect.y + rect.height), cvPoint(rect.x + rect.width, rect.y), CV_RGB(200, 0, 200), 1, 8, 0 );
  
  CvSeq* hull = cvConvexHull2( contours2, 0, CV_CLOCKWISE, 0 );
  
  CvSeq* defect = cvConvexityDefects( contours2, hull, dftStorage );
  
  CvBox2D box = cvMinAreaRect2( contours2, minStorage );
  
  cvDrawContours( bg, contours2,  CV_RGB( 0, 200, 0), CV_RGB( 0, 100, 0), 1, 1, 8, cvPoint(0,0));
  </code></pre>
  
  <p>I have played around with it and I can now draw the fingertip points using this code </p>
  
  <pre><code>for(;defect;defect = defect-&gt;h_next) 
  { 
          int nomdef = defect-&gt;total;
          if(nomdef == 0)  
      continue; 
      defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef);     
      cvCvtSeqToArray (defect, defectArray, CV_WHOLE_SEQ);
      for(i=0; i&lt;nomdef;&gt;
      { 
          cvCircle( bg, *(defectArray[i].end), 5, CV_RGB(255,0,0), -1, 8,0);  
          cvCircle( bg, *(defectArray[i].start), 5, CV_RGB(0,0,255), -1, 8,0); 
          cvCircle( bg, *(defectArray[i].depth_point), 5, CV_RGB(0,255,255), -1, 8,0);        
      }
      j++;
      free(defectArray);
      }
  </code></pre>
  
  <p>However I am still getting a lot of false positives. Also if anyone could suggest any methods to now count the fingers that would be wonderful.</p>