contourArea and drawContours help [closed]

asked 2014-05-10 09:51:19 -0500

tracy gravatar image

updated 2014-05-10 10:01:47 -0500

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;
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2016-01-25 07:03:33.787641

Comments

Your code works fine here, can you post the image you tested?.

Haris gravatar imageHaris ( 2014-05-10 10:51:14 -0500 )edit

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

thanks alot

tracy gravatar imagetracy ( 2014-05-10 17:06:33 -0500 )edit