Ask Your Question

Problem with findContours and drawContours

asked 2014-06-25 12:28:06 -0500

Kien Huynh gravatar image

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

berak gravatar image

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;

    std::cout << "found " << contours.size() << " contours\n";
    cv::imwrite("contours.png", immat);
    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.

edit retag flag offensive close merge delete


Did you get those values when running in debug code? Was the optimization option turned off?

GilLevi gravatar imageGilLevi ( 2014-06-25 12:33:29 -0500 )edit

Yes, those values were from the debug code, but the release code returned the same values. You meant the JIT optimization of debug mode? Yes it was suppressed.

Kien Huynh gravatar imageKien Huynh ( 2014-06-26 01:28:02 -0500 )edit

Would you like to send me the images and source code and I'll try it on my machine?

GilLevi gravatar imageGilLevi ( 2014-06-26 07:28:19 -0500 )edit

Well, I don't know why but I tested the same code with the same image on my friends computer, it works perfectly fine. The entire source code was what I posted in the question excluding the necessary headers, and the image board.jpg was in the opencv sample folder: sources\samples\cpp\board.jpg

Kien Huynh gravatar imageKien Huynh ( 2014-06-26 10:18:08 -0500 )edit

Ho Kien! I am experiencing the same problem with the function findcontours. Can you please tell me if you have found a solution eventually?

riccardo.via gravatar imagericcardo.via ( 2014-08-22 08:52:04 -0500 )edit

@Kien Huynh Same question as @riccardo.via did you find what the problem was?

HappyForce gravatar imageHappyForce ( 2015-12-29 16:26:47 -0500 )edit

@HappyForce are you using Visual Studio? it seems a configuration problem.

sturkmen gravatar imagesturkmen ( 2015-12-29 17:29:48 -0500 )edit

Yes, VS2015 reinstalling right now following this guide

Sadly there I am stuck since I get an error for example when building ALL_BUILD I get: "unable to start program... ALL_BUILD". access denied (I ran VS with admin rights... so ...)

HappyForce gravatar imageHappyForce ( 2015-12-29 17:32:20 -0500 )edit

@sturkmen I have now reinstalled openCV with a different guide and it works now :)

HappyForce gravatar imageHappyForce ( 2015-12-30 05:01:19 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-12-30 05:05:38 -0500

HappyForce gravatar image

updated 2015-12-30 10:44:59 -0500

I had the exact same problem. Made me sleep awfully bad... Anyways I solved it now

When you link OpenCV to VS then it is important that it is linked to the right build of OpenCV.

For example:

VS2013 <--> D:\opencv\build\install\x64\vc12

In my case VS2015 <--> D:\opencv\build\install\x64\vc14 (which does not exist and you have to make yourself)

Your case would be VS2012 <--> D:\opencv\build\install\x64\vc11

The guide I followed to install it correctly is:

edit flag offensive delete link more

Question Tools


Asked: 2014-06-25 12:28:06 -0500

Seen: 2,025 times

Last updated: Dec 30 '15