Convexity defects C++ OpenCv

前端 未结 2 641
小蘑菇
小蘑菇 2020-12-01 10:07

I would be grateful to you if you could help me with this issue :)

Relating to this question cvConvexityDefects in OpenCV 2.X / C++?, I have the same problem. The Op

2条回答
  •  无人及你
    2020-12-01 10:18

    I raised this question because I wasn't able to figure out a solution (it is not only today that I was dealing with the matter hehe), but after all I was able to manage the problem!

    I had to change the way I calculated the convex hull, using the index array form. So now we have a vector< int > instead a vector< Point >.

    This is the code I used (it works I painted the points over an image):

    void HandDetection::findConvexityDefects(vector& contour, vector& hull, vector& convexDefects){
        if(hull.size() > 0 && contour.size() > 0){
        CvSeq* contourPoints;
        CvSeq* defects;
        CvMemStorage* storage;
        CvMemStorage* strDefects;
        CvMemStorage* contourStr;
        CvConvexityDefect *defectArray = 0;
    
        strDefects = cvCreateMemStorage();
        defects = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq),sizeof(CvPoint), strDefects );
    
        //We transform our vector into a CvSeq* object of CvPoint.
        contourStr = cvCreateMemStorage();
        contourPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), contourStr);
        for(int i=0; i<(int)contour.size(); i++) {
            CvPoint cp = {contour[i].x,  contour[i].y};
            cvSeqPush(contourPoints, &cp);
        }
    
        //Now, we do the same thing with the hull index
        int count = (int)hull.size();
        //int hullK[count];
        int* hullK = (int*)malloc(count*sizeof(int));
        for(int i=0; itotal);
        cvCvtSeqToArray(defects, defectArray, CV_WHOLE_SEQ);
        //printf("DefectArray %i %i\n",defectArray->end->x, defectArray->end->y);
    
        //We store defects points in the convexDefects parameter.
        for(int i = 0; itotal; i++){
            CvPoint ptf;
            ptf.x = defectArray[i].depth_point->x;
            ptf.y = defectArray[i].depth_point->y;
            convexDefects.push_back(ptf);
        }
    
        //We release memory
        cvReleaseMemStorage(contourStr);
        cvReleaseMemStorage(strDefects);
        cvReleaseMemStorage(storage);
        }
    }
    

    This worked for me. If you see something wrong or another way to manage it, please tell me!

提交回复
热议问题