XterNalz
XterNalz

Reputation: 325

OpenCV C++ cv::convexityDefects error

vector<Point> hull;
vector<Point> defects;
convexHull(Mat(largest),hull,false);
convexityDefects(largest,hull,defects);

*largest is my largest contour in the image

But the convexityDefects gives me this error "Assertion failed (hull.checkVector(1, CV_32S) > 2)". Someone please help me, I do not want to resort to using C solution.

EDITED

vector<int> hull;
vector<Point> defects;
convexHull(Mat(largest),hull,false);

vector<vector<int>> testhull;
testhull.push_back(hull);
convexityDefects(largest,testhull,defects);

I tried making it with the type vector<vector<int>> before passing it to convexityDefects but convexityDefects is still giving me error "Assertion failed (ptnum > 3)..".

Upvotes: 0

Views: 3251

Answers (2)

ABSSH
ABSSH

Reputation: 144

for hull you should use a vector of vectors like this:

vector<vector<Point>> hullsP( contours.size() );
vector<vector<int> > hullsI(contours.size());

and pass the "int" type to covexityDefects.like this :

vector<vector<Vec4i>> convdefect(contours.size());

for( int i = 0; i < contours.size(); i++ )
{ 
    convexHull( Mat(contours[i]), hullsP[i], false );
    convexHull( Mat(contours[i]), hullsI[i], false );       
    if(hullsI[i].size() > 3 )
        convexityDefects(contours[i],hullsI[i],convdefect[i]);
}

Upvotes: 4

ArtemStorozhuk
ArtemStorozhuk

Reputation: 8725

The second argument of convexityDefects has to be the type of vector<vector<int>, while yours is vector<Point>.

Upvotes: 2

Related Questions