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:
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.
Did you get those values when running in debug code? Was the optimization option turned off?
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.
Would you like to send me the images and source code and I'll try it on my machine?
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
Ho Kien! I am experiencing the same problem with the function findcontours. Can you please tell me if you have found a solution eventually?
@Kien Huynh Same question as @riccardo.via did you find what the problem was?
@HappyForce are you using Visual Studio? it seems a configuration problem.
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 ...)
@sturkmen I have now reinstalled openCV with a different guide and it works now :)