Search code examples
opencvconvex-hull

OpenCV CvConvexityDefects can't work and can't detect fingers!


I'm using OpenCv to detect fingertips, but now I meet with some obstacles! The findContours & convexHull & convexityDefects method doesn't work with my project. It's my first time to ask a question. Thanks everybody!

My code is below:

int main()
{
CvScalar white = cvRealScalar(255);
CvScalar black = cvRealScalar(0);

CvSeq* contours = 0;
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* img = cvLoadImage("h.jpg");
IplImage* imgGray = cvCreateImage(cvGetSize(img), img->depth, 1);
IplImage* contoursImg = cvCreateImage(cvGetSize(img), img->depth, 1);
cvZero(contoursImg);
int nWidth = contoursImg->width;
int nHeight = contoursImg->height;
int nChannels = contoursImg->nChannels;
int nStep = contoursImg->widthStep;
for (int i=0; i<nHeight; i++)
    for(int j=0; j<nWidth; j++)
        contoursImg->imageData[i*nStep + j*nChannels] = 255 - contoursImg->imageData[i*nStep + j*nChannels];

cvCvtColor(img, imgGray, CV_BGR2GRAY);
cvThreshold(imgGray, imgGray, 230, 255, CV_THRESH_BINARY);
cvSmooth(imgGray, imgGray);

cvFindContours(imgGray, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
contours = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 35, 1);
cvDrawContours(contoursImg, contours, CV_RGB(0, 0, 0), CV_RGB(0, 0, 0), 1, 1, 8, cvPoint(0, 0));

CvSeq* hull = cvConvexHull2(contours, 0, CV_CLOCKWISE, 0);
CvSeq* defect = cvConvexityDefects(contours, hull, NULL);
for(; defect; defect = defect->h_next)
{
    int nomdef = defect->total;
    if(!nomdef)
        continue;
    CvConvexityDefect* defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef);
    cvCvtSeqToArray(defect, defectArray, CV_WHOLE_SEQ);
    for(int i = 0; i < nomdef; i++)
    {
        cvCircle(contoursImg, *(defectArray[i].end), 5, CV_RGB(255, 0, 0), -1, 8, 0);
        cvCircle(contoursImg, *(defectArray[i].start), 5, CV_RGB(0, 255, 0), -1, 8, 0);
        cvCircle(contoursImg, *(defectArray[i].depth_point), 5, CV_RGB(0, 0, 255), -1, 8, 0);
    }
    free(defectArray);
}

cvNamedWindow("image", 1);
cvShowImage("image", contoursImg);

cvWaitKey(0);
cvReleaseMemStorage(&storage);
cvReleaseImage(&img);
cvReleaseImage(&imgGray);
cvReleaseImage(&contoursImg);
cvDestroyWindow("image");

return 0;       
}

This is my hand picture:

http://www.billwang.net/bbs/oldattach/2006/12/29/billwang_5315954-038-embed.jpg


Solution

  • The methods are working fine. You have one rectangular contour detected on the outside of the image. As this covers the entire image, there are no defects and so, none detected. See attached image.Look at the cyan box on the outside of the image