Draw Contours error [closed]

asked 2013-10-11 08:04:52 -0600

lobi gravatar image

updated 2013-10-11 08:36:16 -0600

Hello,

I'm trying to draw countours (Java) and I get this error.

OpenCV Error: Assertion failed (0 <= contourIdx && contourIdx < (int)last) in drawContours, file ~/code/OpenCV/opencv-2.4.6.1/modules/imgproc/src/contours.cpp, line 1810
Exception in thread "main" CvException [org.opencv.core.CvException: /home/slani/code/OpenCV/opencv-2.4.6.1/modules/imgproc/src/contours.cpp:1810: error: (-215) 0 <= contourIdx && contourIdx < (int)last in function drawContours
]
    at org.opencv.imgproc.Imgproc.drawContours_2(Native Method)
    at org.opencv.imgproc.Imgproc.drawContours(Imgproc.java:5013)
    at HsvImage.main(HsvImage.java:83)

Can someone please explain me what this error mean. Thanks,

P.S. Code

// convert color space
Imgproc.GaussianBlur(frame, smooth, new Size(19,19), 0.0);
Imgproc.cvtColor(smooth, hsvFrame, Imgproc.COLOR_BGR2HSV);
// checks if array elements lie beween min and max
Core.inRange(hsvFrame, min, max, thresholded);
Core.inRange(hsvFrame, min2, max2, thresholded2);
Core.bitwise_or(thresholded, thresholded2, thresholded);

Imgproc.findContours(thresholded, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(frame, contours, 4, new Scalar(40, 233, 45,0 ));

panel.setImage(frame);
HSVpanel.setImage(thresholded);
panel.repaint();
HSVpanel.repaint();
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-24 18:40:15.234015

Comments

your contourIdx is out of bounds. can you show the piece of code, where you're drawing ?

berak gravatar imageberak ( 2013-10-11 08:21:50 -0600 )edit
2

so, you're trying to draw contour 4. what if it finds only 2 contours in your img ? boom maybe you wanted:

Imgproc.drawContours(frame, contours, -1, new Scalar(40, 233, 45,0 ));

to draw all of them.

or:

for ( ... i&lt;contours.size() )
         Imgproc.drawContours(frame, contours, i, new Scalar(40, 233, 45,0 ));
berak gravatar imageberak ( 2013-10-11 09:06:58 -0600 )edit