OpenCV C++ cv::convexityDefects error

混江龙づ霸主 提交于 2019-12-02 18:52:25

问题


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)..".


回答1:


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




回答2:


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]);
}


来源:https://stackoverflow.com/questions/11840752/opencv-c-cvconvexitydefects-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!