我对OpenCV很陌生,有一个小问题,这可能是很容易修复的。基本上我在做一些基本的图像处理,我试图找到轮廓有一个contourArea() <3000 .
问题是,当尝试绘制轮廓和/或调用contourArea()函数时,我得到以下错误:
cv:contourArea()
行出现错误,错误信息为:
OpenCV Error: Assertion failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S)) in cv::contourArea,
file ........opencvmodulesimgprocsrccontours.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;
不确定,但从我在错误消息中读到的,该函数期望一个浮点值,你给他Point
的向量的向量。
根据目前的手册,这个类型是一个整数点,所以可能这就是问题所在