凸性缺陷C++ OpenCv

cab*_*nal 11 c++ opencv defects convex-hull

如果你能帮我解决这个问题,我将不胜感激:)

关于这个问题cvConvexityDefects在OpenCV 2.X/C++中?, 我也有同样的问题.OpenCV C++包装器没有出现在C版本中的函数cvConvexityDefects,所以我尝试编写自己的版本.

部分代码是(请注意,countour和hull都是vector <Point>,单独计算:

CvSeq* contourPoints;
CvSeq* hullPoints;
CvSeq* defects;
CvMemStorage* storage;
CvMemStorage* strDefects;
CvMemStorage* contourStr;
CvMemStorage* hullStr;
CvConvexityDefect *defectArray = 0;

strDefects = cvCreateMemStorage();
defects = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq),sizeof(CvPoint), strDefects );

//We start converting vector<Point> resulting from findContours
contourStr = cvCreateMemStorage();
contourPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), contourStr);
printf("Metiendo valores\n");
for(int i=0; i<(int)contour.size(); i++) {
    CvPoint cp = {contour[i].x,  contour[i].y};
    cvSeqPush(contourPoints, &cp);
}
//Now, the hull points obtained from convexHull c++
hullStr = cvCreateMemStorage(0);
hullPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), hullStr);
for(int i=0; i<(int)hull.size(); i++) {
    CvPoint cp = {hull[i].x,  hull[i].y};
    cvSeqPush(hullPoints, &cp);
}

//And we compute convexity defects
storage = cvCreateMemStorage(0);
defects = cvConvexityDefects(contourPoints, hullPoints, storage);
Run Code Online (Sandbox Code Playgroud)

输出是Convex hull must represented as a sequence of indices or sequence of pointers in function cvConvexityDefects.我真的不知道如何以正确的方式进行转换,我一直在网上搜索并尝试调整/复制/理解一些代码片段,但它总是使用C语法.

我希望我很清楚.先感谢您!

cab*_*nal 7

我提出这个问题是因为我无法找到解决方案(今天不仅是我正在处理这个问题),但毕竟我能够解决问题!

我不得不改变我使用索引数组形式计算凸包的方式.所以现在我们有一个vector <int>而不是vector <Point>.

这是我使用的代码(它可以在图像上绘制点):

void HandDetection::findConvexityDefects(vector<Point>& contour, vector<int>& hull, vector<Point>& 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<Point> 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; i<count; i++){hullK[i] = hull.at(i);}
    CvMat hullMat = cvMat(1, count, CV_32SC1, hullK);

    //We calculate convexity defects
    storage = cvCreateMemStorage(0);
    defects = cvConvexityDefects(contourPoints, &hullMat, storage);
    defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*defects->total);
    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; i<defects->total; 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这对我有用.如果您发现错误或其他方式来管理它,请告诉我!

  • 从openCV 2.4开始,该方法有一个C++ API版本:http://opencv.itseez.com/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=convexity%20defects (3认同)