使用OpenCV凸包和凸度缺陷函数进行手指跟踪/计数

时间:2011-11-23 09:16:50

标签: c++ opencv computer-vision tracking gesture-recognition

我一直在使用OpenCV和ConvexHull和ConvexityDefects方法处理基本的手/手指跟踪代码。

基本上我能够创造出手的轮廓。我现在需要能够计算手指的数量。我知道Convex Hull的起点和终点是指尖,但我不确定如何计算它们以及如何通过在它们上面绘制圆圈来突出它们。

我希望我的代码执行this之类的操作。

到目前为止,这是我的代码的示例部分:

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));

我玩过它,我现在可以使用此代码绘制指尖点

for(;defect;defect = defect->h_next) 
{ 
        int nomdef = defect->total;
        if(nomdef == 0)  
    continue; 
    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);        
    }
    j++;
    free(defectArray);
    }

然而,我仍然得到很多误报。此外,如果有人可以建议任何方法,现在计算将是美妙的手指。

1 个答案:

答案 0 :(得分:9)

您拥有的一种可能性是计算缺陷的数量。如果你做得对,假设缺陷位于两个手指之间的底部。http://img27.imageshack.us/img27/6532/herpz.jpg

确保您没有得到任何&#34;不需要的&#34;缺陷,你可以使用&#39;深度&#39;来自CvConvexityDefect()的参数;用于过滤低长度缺陷的功能。更好地描述&#34;深度&#34;参数可以在这里找到: opencv.itseez.com defect description