Search code examples
c++opencvconvex-hull

C++ OpenCv error in function ConvexityDefects


i've already read the posts here and here but they do not solve the error i get.

My code

vector<Vec4i> defects;
vector<vector<int> >hull( contours.size() );
for (int i = 0; i < contours.size(); i++)
{
    convexHull( contours[i], hull[i], false, false );
    if(contours[i].size() > 3 )
        convexityDefects(contours[i], hull[i], defects[i]);
}

According to the posts above this should work, but it does not. I still get the error
error: (-215) hull.checkVector(1, CV_32S) > 2 in function convexityDefects
I really don't see the problem here.


Solution

  • Okay, the problem was mainly because due to some odd reason i had contours so small that the hull was merely a straight line (meaning consisting only of 2 points). So the error was referencing to the size of the hull vector, other than in the other posts where it seemed to have something to do with the type of vector.

    So, just replacing
    if(contours[i].size() > 3 ) with if(hulls[i].size() > 2 ) works fine.