Click here to Skip to main content
15,072,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, Just a quick question. I have been working on a basic hand/ finger tracking code using OpenCv and the ConvexHull and ConvexityDefects method. Basically i am able to create a contour of the hand. I now need to be able to count the number of fingers. I no 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 or them or something. I kinda what my code to preform something like this[^]

This is a sample part of my code so far:
cvFindContours( hsv_mask, storage, &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));

I have played around with it and I can now draw the fingertip points using this code

for(;defect;defect = defect->h_next) 
        int nomdef = defect->total;
        if(nomdef == 0)  
	defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef);
	cvCvtSeqToArray (defect, defectArray, CV_WHOLE_SEQ);
	for(i=0; i<nomdef;>
		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); 


However i am still getting a lot of false positives. Also if any one could suggest any methods to now count the fingers that would be wonderful

Any help would be very much appreciated
Updated 23-Nov-11 13:25pm

To count them or use the results, try something like

CvSeq* hull = cvConvexHull2( contours2, 0, CV_CLOCKWISE, 0 );

int i;
for(i=0; i < hull->total; i++)
float* p = (float*)cvGetSeqElem(hull, i);
// do something here, though float* may not be right for your data
Erin Burke 23-Nov-11 18:34pm
Sorry i am still a little confused, i understand how you need to loop through each occurring hull, however I'm not sure how to then count them.
Regarding false positives have you tried working on the image before calling the convex hull algorithm to try and remove some of the reasons for those false positives?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900