我正在使用 OpenCV 2.4 来计算图像的凸包。
我也在做一些处理以消除图像中的一些噪声,这与问题并不真正相关。
计算convexHull的代码如下:
...
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 );
}
...
拥有convexHull和轮廓我现在想计算hull(s)的convexityDefects,通过查看opencv文档我认为它会是这样的:
cv::Vec4i defects;
convexityDefects(cv::Mat(contours), hull, defects);
这样做我得到这个错误:
OpenCV Error: Assertion failed (ptnum > 3) in convexityDefects, file ./opencv/opencv/modules/imgproc/src/contours.cpp, line 1969
关于使用convexityDefects时我做错了什么的任何想法?
提前致谢。
更新
感谢影射的回答,我将主循环代码更新为:
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]);
}
使用它,我现在得到的错误是:
OpenCV Error: Assertion failed (hull.checkVector(1, CV_32S) > 2) in convexityDefects