1

I've looked at and tried to solution to the similar question at Calculating convexityDefects using OpenCV 2.4 in c++, but his solution doesn't seem to be working for me.

// Get contours
vector<vector<cv::Point> > contours;
findContours(inputMat, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

vector<vector<cv::Point> > hulls(contours.size());
vector<vector<int> > intHulls(contours.size());
vector<vector<Vec4i> > defects(contours.size());

for(int i = 0; i < contours.size(); i++){
    convexHull(Mat(contours[i]), hulls[i]);
    convexHull(Mat(contours[i]), intHulls[i]);
    convexityDefects(contours[i], intHulls[i], defects[i]);
}

I always get:

OpenCV Error: Assertion failed (ptnum > 3) in convexityDefects, file /Users/user/slave/ios_framework/src/opencv/modules/imgproc/src/contours.cpp, line 1969

So I went and downloaded the source to check out contours.cpp, line 1969.

1965 void cv::convexityDefects( InputArray _points, InputArray _hull, OutputArray _defects )
1966 {
1967     Mat points = _points.getMat();
1968     int ptnum = points.checkVector(2, CV_32S);
1969     CV_Assert( ptnum > 3 );

It looks like it's trying to make a Mat of the contour points, then it checks the Mat for... something? There wasn't any documentation at http://docs.opencv.org/ for checkVector, but googling found this documentation:

int cv::Mat::checkVector    (   int     elemChannels,
    int     depth = -1,
    bool    requireContinuous = true 
    )        const

returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise

After reading that a few times, I'm still completely lost and unsure about what would be causing this error or what I can do to fix it. I've been trying to figure out this whole convexityDefects() problem myself for the last week and haven't had any luck at all.

Edit: I just double-checked a few things to be safe.

My input mat is always 100 cols x 120 rows, just like it should be. However, the contours, which I assumed would be as smooth and continuous as they look on the screen, are apparently not. When I convert them to mats and check the size, they're almost always always 1 col by XXX rows. So I guess my problem is that I have to get a two-dimensional contour -- is there a way to join them together? They look seamless on my screen.

Edit 2: Even drawing the contours on a proper mat and passing that gives me the same "Assertion failed (ptnum > 3)" error. I really don't know what to do now.

Community
  • 1
  • 1
Scott R
  • 11
  • 2
  • What about googling question before asking? Here's exactly the same problem and solution: http://stackoverflow.com/questions/11840752/opencv-c-cvconvexitydefects-error – ArtemStorozhuk Sep 05 '12 at 10:39
  • There is no solution there. That's the same problem in the same situation -- my second argument is indeed vector > as the specifications say it should be. Ironically, on the comment you just linked now, you linked the comment that I reference at the very top of my problem explanation. Apparently you didn't read what I wrote? – Scott R Sep 05 '12 at 22:21
  • you call `convexityDefects` with type `` not with `vector >`... – ArtemStorozhuk Sep 06 '12 at 15:21
  • And also links are not the same... – ArtemStorozhuk Sep 06 '12 at 15:24
  • @Astor, you linked to [this post](http://stackoverflow.com/questions/11840752/opencv-c-cvconvexitydefects-error) in your initial comment; I went there to look at the solution and found that there, you linked to [another post](http://stackoverflow.com/questions/11840752/opencv-c-cvconvexitydefects-error), which is exactly what I looked at and linked at the top. BECAUSE the solution there isn't working, I asked my own question with more details. Changing my call to `convexityDefects(contours[i], intHulls, defects[i]);` to provide a vector > doesn't help; I get the same error. – Scott R Sep 06 '12 at 22:26
  • I posed an answer to this question on: http://stackoverflow.com/questions/10620981/calculating-convexitydefects-using-opencv-2-4-in-c Hope it helps :) – Nacho Sep 11 '12 at 12:41
  • Thank you! That's so obvious, now that I think of it... it's just something you'd expect to be handled gracefully by the function itself or mentioned in the documentation. I actually ended up writing a wrapper for the old C function. :( – Scott R Sep 11 '12 at 23:26

0 Answers0