Can't draw contours [SOLVED]
Hello, I am trying to draw contours, but it throws an assertion error
"OpenCV Error: Assertion failed (0 <= contourIdx && contourIdx < (int)last) in unknown function,
file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1810"
My input image is :
Here's a code snippet of what I'm doing :
Mat input_image = imread(filename, 0);
Mat canny_op;
Canny(input_image, canny_op, 100, 200, 3);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( canny_op, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0) );
cout << "Size of contours [vector] is " << contours.size() << endl;
cout << "Size of hierarchy [vector] is " << hierarchy.size() << endl;
// Draw contours
Mat drawing = Mat::zeros( input_image.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ ) {
cout << i << endl;
Scalar color(0,0,255);
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
}
showImage("output of drawing",drawing);
Here is the output of Canny :
And here is the output of the program :
Size of contours [vector] is 160
Size of hierarchy [vector] is 120
0
1
2
...
120
OpenCV Error: Assertion failed (0 <= contourIdx && contourIdx < (int)last) in un
known function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line
1810
Your code works fine with my PC, are you sure this is your full code ? And it showing total number of contour hierarchy 120.
This is my full code -> http://pastebin.com/dZ7a3ted Here is the program's execution output -> http://pastebin.com/HaftZ9gV (though, of course, you'd have to pass the an input image as run-time parameter.)
I do agree, it should be perfectly fine (execution) - I even tried it on another machine. And it worked fine.
Total number of contour hierarchy is 120. Yes.
Which version of OpenCV you are using ? Are they same in both PC?
On my machine (PC) - 2.4.6; my development environment is - visual studio 2012 - release configuration - 64bit On my other machine (laptop) - 2.4.6 my development environment is - visual studio 2008 - release configuration - 32bit.
I think there is some issue with visual studio 2012 coupled with OpenCV 2.4.6, for drawContours(); I will update my openCV version and update the post.
-> i've just tried with openCV 2.4.8 & Visual Studio 2012, on a 64bit release environment; and the same error comes up.
tried with 32bit as well; same problem.
Did anyone face the same problem?
Another person has faced this problem, it seems visual studio 2012, is the culprit (i don't know why?)
Here -> http://answers.opencv.org/question/4283/program-crashes-during-findcontours-execution/