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.