如果您能帮助我解决这个问题,我将不胜感激:)
与OpenCV 2.X / C++ 中的这个问题 cvConvexityDefects 有关吗?, 我也有同样的问题。OpenCV C++ wrapper 没有 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);
输出是Convex hull must represented as a sequence of indices or sequence of pointers in function cvConvexityDefects
。真的我不知道如何以正确的方式进行转换,我一直在网上搜索并尝试调整/复制/理解一些代码,但它总是使用 C 语法。
我希望我很清楚。先感谢您!