Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem with findContours and drawContours

Hi, I'm new to OpenCV, I was trying to run this simple code to find the contours of an image:

int main( int argc, char** argv )
{
    cv::Mat immat = cv::imread("board.jpg", 1);
    cv::Mat img;
    cv::cvtColor(immat, img, CV_BGR2GRAY);

    cv::Mat imcanny;
    cv::Canny (img,imcanny,75,150,3);

    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(imcanny,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
    cv::drawContours(immat,contours,-1,CV_RGB(255,0,0),2);

    std::cout << "found " << contours.size() << " contours\n";
    cv::imwrite("contours.png", immat);
    cv::waitKey();
    return 0;
}

But there was something strange about the output of the contours, it looks like this when I checked it: image description

There are a lot of contours with an extremely huge size and all of their points are {x=??? y=???}. Is this normal output for findcontours function?

Not only that, when I execute the program (release mode) to the end of the main function, I got two messages "myproject has triggered a breakpoint", after I pressed continue on both of them, I got "A heap has been corrupted" exception. While in debug mode, I got a Debug Assertion Failed: _CrtIsValidHeapPointer(pUserData). Everything else works fine if I comment the findContours and drawContours lines.

How can I solve this problem? For more information, I was using VS2012 and OpenCV 2.49.

click to hide/show revision 2
retagged

updated 2014-06-25 12:56:18 -0600

berak gravatar image

Problem with findContours and drawContours

Hi, I'm new to OpenCV, I was trying to run this simple code to find the contours of an image:

int main( int argc, char** argv )
{
    cv::Mat immat = cv::imread("board.jpg", 1);
    cv::Mat img;
    cv::cvtColor(immat, img, CV_BGR2GRAY);

    cv::Mat imcanny;
    cv::Canny (img,imcanny,75,150,3);

    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(imcanny,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
    cv::drawContours(immat,contours,-1,CV_RGB(255,0,0),2);

    std::cout << "found " << contours.size() << " contours\n";
    cv::imwrite("contours.png", immat);
    cv::waitKey();
    return 0;
}

But there was something strange about the output of the contours, it looks like this when I checked it: image description

There are a lot of contours with an extremely huge size and all of their points are {x=??? y=???}. Is this normal output for findcontours function?

Not only that, when I execute the program (release mode) to the end of the main function, I got two messages "myproject has triggered a breakpoint", after I pressed continue on both of them, I got "A heap has been corrupted" exception. While in debug mode, I got a Debug Assertion Failed: _CrtIsValidHeapPointer(pUserData). Everything else works fine if I comment the findContours and drawContours lines.

How can I solve this problem? For more information, I was using VS2012 and OpenCV 2.49.