0
我正在嘗試爲OpenCV創建一個C++包裝器,但不幸的是無法使其工作。我一直得到同樣的錯誤:「OpenCV錯誤:錯誤的參數(凸包既不是序列也不是矩陣)」。我不明白爲什麼。一個C++凸性缺陷包裝(OpenCV)?
下面的代碼:
void convexityDefects (vector <vector<Point> > contour, vector <vector<int> > convexHullPoints, vector <vector<Point> > *convexHullDefects)
{
CvSeq *contourSeq;
CvSeq *convexHullDefectsSeq;
CvSeq *convexHullSeq;
CvMemStorage *contourSeqStorage = cvCreateMemStorage (0);
CvMemStorage *convexHullSeqStorage = cvCreateMemStorage (0);
CvMemStorage *convexHullDefectsSeqStorage = cvCreateMemStorage (0);
CvMemStorage *cvConvexityDefectsStorage = cvCreateMemStorage (0);
contourSeq = cvCreateSeq (CV_SEQ_KIND_GENERIC | CV_32SC2, sizeof (CvSeq), sizeof (CvPoint), contourSeqStorage);
convexHullDefectsSeq = cvCreateSeq (CV_SEQ_KIND_GENERIC, sizeof (CvSeq), sizeof (CvPoint), convexHullDefectsSeqStorage);
convexHullSeq = cvCreateSeq (CV_SEQ_KIND_GENERIC, sizeof (CvSeq), sizeof (int), convexHullSeqStorage);
for (int counter = 0; counter < contour.at (0).size(); counter ++)
{
CvPoint element = {contour.at (0).at (counter).x, contour.at (0).at (counter).y};
cvSeqPush (contourSeq, &element);
}
int hullArray [convexHullPoints.at (0).size()];
for (int counter = 0; counter < convexHullPoints.size(); counter ++)
{
hullArray [counter] = convexHullPoints.at (0).at (counter);
}
for (int counter = 0; counter < convexHullPoints.size(); counter ++)
{
cvSeqPush (convexHullSeq, &(hullArray [counter]));
}
convexHullDefectsSeq = cvConvexityDefects (contourSeq, &convexHullSeq, cvConvexityDefectsStorage);
CvConvexityDefect convexHullDefectsArray [convexHullDefectsSeq -> total];
cvCvtSeqToArray (convexHullDefectsSeq, &convexHullDefectsArray, CV_WHOLE_SEQ);
for (int counter = 0; counter < convexHullDefectsSeq -> total; counter ++)
{
CvPoint start = {convexHullDefectsArray [counter].start -> x, convexHullDefectsArray [counter].start -> y};
CvPoint depthPoint = {convexHullDefectsArray [counter].depth_point -> x, convexHullDefectsArray [counter].depth_point -> y};
CvPoint end = {convexHullDefectsArray [counter].end -> x, convexHullDefectsArray [counter].end -> y};
vector <Point> convexityDefectVector;
convexityDefectVector.push_back (start);
convexityDefectVector.push_back (depthPoint);
convexityDefectVector.push_back (end);
convexHullDefects -> push_back (convexityDefectVector);
}
cvReleaseMemStorage (&contourSeqStorage);
cvReleaseMemStorage (&convexHullSeqStorage);
cvReleaseMemStorage (&convexHullDefectsSeqStorage);
cvReleaseMemStorage (&cvConvexityDefectsStorage);
}
請參閱工作源代碼:http://stackoverflow.com/a/35190031/763355 – MoDJ