我正在使用 OpenCv 来检测指尖,但现在遇到了一些障碍!findContours & convexHull & convexityDefects 方法不适用于我的项目。这是我第一次提出问题。谢谢大家!
我的代码如下:
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;
}
这是我的手图:
http://www.billwang.net/bbs/oldattach/2006/12/29/billwang_5315954-038-embed.jpg