【发布时间】:2014-05-10 15:03:48
【问题描述】:
我对 OpenCV 还很陌生,遇到了一个小问题,这可能很容易解决。 基本上我在做一些基本的图像处理,我试图找到轮廓区域()
问题是,我在尝试绘制轮廓和/或调用 contourArea() 函数时遇到以下错误:
cv:contourArea() 行发生错误,错误信息是:
OpenCV Error: Assertion failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S)) in cv::contourArea,
file ..\..\..\..\opencv\modules\imgproc\src\contours.cpp, line 1904
非常感谢任何帮助。代码如下:
using namespace cv;
cv::Mat greyMat, binaryMat, newMat;
cv::Mat image = cv::imread("image.png", 1);
// First convert image to gray scale
cv::cvtColor(image, greyMat, CV_BGR2GRAY);
cv::adaptiveThreshold(greyMat, binaryMat, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY_INV, 45, 0);
erode(binaryMat, binaryMat, getStructuringElement(MORPH_ELLIPSE, Size(2, 2)));
dilate(binaryMat, binaryMat, getStructuringElement(MORPH_ELLIPSE, Size(1, 1)));
// Remove unclosed curves (the circled hashtag)
cv::copyMakeBorder(binaryMat, newMat, 1, 1, 1, 1, cv::BORDER_CONSTANT, 0);
cv::floodFill(newMat, cv::Point(0, 0), 255);
newMat = 255 - newMat;
cv::Mat cMat;
newMat.copyTo(cMat);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(cMat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
std::cout << "Found: " << contours.size() << " " << contours[0][0] << std::endl;
for (size_t i = 0; i < contours.size(); i++)
{
if (cv::contourArea(contours[i]) < 3000)
{
cv::drawContours(newMat, contours, i, 255, -1);
}
}
cv::imshow("Debug", newMat);
cv::waitKey(0);
return 0;
【问题讨论】:
-
你用的是什么编译器? This question 在 VS 2012 上演示了相同的断言失败。您能否还发布代码失败的示例图像?这尤其相关,因为我刚刚尝试了您的代码,但无法重现您的问题。