【问题标题】:OpenCV contourArea() not workingOpenCV轮廓区域()不工作
【发布时间】: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 上演示了相同的断言失败。您能否还发布代码失败的示例图像?这尤其相关,因为我刚刚尝试了您的代码,但无法重现您的问题。

标签: c++ opencv


【解决方案1】:

不确定,但从我在错误消息中看到的内容来看,该函数需要一个浮点值,你给他一个向量的向量 Point

根据current manual,这个类型是整数点,所以可能是这个问题。

【讨论】:

  • 谢谢,但不,contourArea 返回一个双精度值,但需要一个 Point 类型的向量
  • 嗯。这里:docs.opencv.org/modules/imgproc/doc/…,我读过contour – Input vector of 2D points,但对浮点类型的整数一无所知。我刚刚检查了contourArea() (2.4.5) 的来源,它确实需要CV_32F 类型。或者我错过了什么?
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2012-11-09
  • 2018-05-15
  • 2015-02-02
  • 1970-01-01
  • 2020-07-11
  • 2019-12-07
相关资源
最近更新 更多