Ask Your Question
3

Can't draw contours [SOLVED]

asked 2014-02-09 04:59:37 -0600

jm gravatar image

updated 2014-02-13 05:48:47 -0600

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 :

image description

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 :

image description

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

Comments

Your code works fine with my PC, are you sure this is your full code ? And it showing total number of contour hierarchy 120.

Haris gravatar imageHaris ( 2014-02-09 09:43:03 -0600 )edit

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.

jm gravatar imagejm ( 2014-02-09 13:35:51 -0600 )edit

Which version of OpenCV you are using ? Are they same in both PC?

Haris gravatar imageHaris ( 2014-02-09 22:04:01 -0600 )edit

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.

jm gravatar imagejm ( 2014-02-10 06:52:09 -0600 )edit

-> 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?

jm gravatar imagejm ( 2014-02-10 13:12:59 -0600 )edit

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/

jm gravatar imagejm ( 2014-02-11 20:08:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-13 05:44:18 -0600

jm gravatar image

I've switched to Code::Blocks, works perfectly.

Although I wonder if the VS2012 problem could be solved. I hope this post - helps anyone else, if they face the same problem.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-09 04:59:37 -0600

Seen: 6,035 times

Last updated: Feb 13 '14