Ask Your Question

tracy's profile - activity

2019-12-11 21:11:57 -0600 received badge  Popular Question (source)
2017-07-17 17:06:01 -0600 received badge  Taxonomist
2015-07-17 12:28:27 -0600 received badge  Student (source)
2014-05-10 17:06:33 -0600 commented question contourArea and drawContours help

It looks like it was a problem with visual studio 2013. i changed the the toolset to vs2010 and it worked fine.

thanks alot

2014-05-10 09:51:48 -0600 received badge  Editor (source)
2014-05-10 09:51:19 -0600 asked a question contourArea and drawContours help

Hello guys, I'm pretty new to OpenCV and having a slight problem which is probably something very easy to fix.

Basically im doing some basic image processing, I'm trying to find contours which have an contourArea() of < 3000.

The problem is, I'm getting the following error when trying to draw contours and/or call contourArea() function:

to clarify this is happening on the cv:contourArea() line.

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

Any help is greatly appricated

The code is below:

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;