0

I am trying to create a C++ wrapper for OpenCV, but unfortunately can't get it to work. I keep getting the same error again and again: "OpenCV Error: Bad argument (Convex hull is neither sequence nor matrix)". I don't really understand why.

Here's the code:

   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);
  }
undur_gongor
  • 15,657
  • 5
  • 63
  • 75
fdh
  • 5,256
  • 13
  • 58
  • 101

1 Answers1

2

You should have a look at this other post on SO. It appears he has already solved this problem.

Community
  • 1
  • 1
mevatron
  • 13,911
  • 4
  • 55
  • 72
  • Thanks. I already checked out the post, but I cannot use his wrapper because he uses a CvMat instead of a CvSeq in his program to represent the convex hull. I cannot use a CvMat for several reasons I am unable to mention. Do you actually see anything wrong with my code though? – fdh Nov 25 '11 at 00:18