I am using OpenCV 2.4 to compute the convex hull of an image.
I also do some processing to remove some noise from the image, which is not relevant to the issue.
The code for calculating the convex hull is as follows:
... cv::Mat sourceImage; // assume something is already here please cv::vector<cv::Vec4i> hierarchy; std::vector<std::vector<cv::Point> > contours; cv::findContours( sourceImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,cv::Point(0, 0)); // Find the convex hull object for each contour vector<cv::vector<cv::Point> >hull( contours.size() ); for (int i = 0; i < contours.size(); i++) { convexHull( contours[i], hull[i], false ); } ...
Having both a convex hull and contours, I now want to calculate the convexity of the Hull (s) defects, which, looking at the opencv documentation, I thought it would be like this:
cv::Vec4i defects; convexityDefects(cv::Mat(contours), hull, defects);
By doing this, I get this error:
OpenCV Error: Assertion failed (ptnum > 3) in convexityDefects, file ./opencv/opencv/modules/imgproc/src/contours.cpp, line 1969
Any ideas on what I'm doing wrong when using Defects bulge?
Opencv convexityDefects documentation
Thanks in advance.
UPDATE
Thanks to Innuendo's answer, I updated the main loop code to:
std::vector<Vec4i> defects; vector<cv::vector<cv::Point> >hull( contours.size() ); for (int i = 0; i < contours.size(); i++) { convexHull( contours[i], hull[i], false ); convexityDefects(contours[i], hull[i], defects[i]); }
Using this error, I get the following message:
OpenCV Error: Assertion failed (hull.checkVector(1, CV_32S) > 2) in convexityDefects