Ask Your Question

Android: cannot draw the interior of contours

asked 2014-06-17 09:10:39 -0500

Josef gravatar image

updated 2020-10-23 10:10:51 -0500


I don't know if this is a bug or a mistake by me but i cannot draw the interior of the contours of an image. My input image is this:

image description

I am using the Android version of OpenCv and the following code snippet for contour detection:

Mat canny = new Mat();
Imgproc.Canny(in_grayImg, canny, 100, 200);

List<MatOfPoint> contours = new LinkedList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(canny, contours, hierarchy, Imgproc.RETR_CCOMP,

These are the detected contours drawn with

Imgproc.drawContours(canny, contours, -1, new Scalar(255));

image description

According to the API using the drawContours with a negative thickness value draws the interior of the contours. However the following code to draw the contours results in the same image as before:

Imgproc.drawContours(canny, contours, -1, new Scalar(255), -1);

image description

I also tried using the detected hierarchy but the result is unchanged:

Imgproc.drawContours(canny, contours, -1, new Scalar(255), -1, 8, hierarchy, 2, new Point(0, 0));

image description

I really don't know what I am doing wrong and it seems to me that a negative thickness value is simply ignored.

Thanks in advance and best regards,


edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2014-06-17 22:39:20 -0500

Haris gravatar image

Use contour retrieval mode CV_RETR_TREE which will retrieves all of the contours and reconstructs a full hierarchy of nested contours.

Imgproc.findContours(canny, contours, hierarchy, Imgproc.CV_RETR_TREE,

Also draw contours in a loop like

for( int i = 0; i< contours.size(); i++ ) 
   Imgproc.drawContours(canny, contours, i, new Scalar(255), -1, 8, hierarchy, 2, new Point(0, 0));

and still if there are some issue let me know...

edit flag offensive delete link more



thank you very much for you quick answer. Drawing the contours in a loop works but the result is not exactly what I need to achieve. I should have stated my goal more precisely. With interior of the contours, I meant the pixels corresponding to the green and red lines of the input image.

This is the reason why I used RETR_CCOMP as contour retrieval mode. Maybe I have to analyze the hierarchy more thoroughly.

Again thanks for your helpful answer.

Josef gravatar imageJosef ( 2014-06-18 03:42:10 -0500 )edit

"I meant the pixels corresponding to the green and red lines of the input image", then why don’t threshold instead of findcontour, drawcontour.

Haris gravatar imageHaris ( 2014-06-18 04:07:34 -0500 )edit

I have tried thresholding before, but it failed due to bad lighting. I also tried and probably will continue to use image enhancement methods in a preprocessing step, or use different thresholding methods. The idea with contours is to binarize the image even if the lighting is bad which should work pretty well as the basic structure of my input images is known.

Josef gravatar imageJosef ( 2014-06-18 05:05:22 -0500 )edit

So will it be like, the background is always white and the foreground is always green and red?

Haris gravatar imageHaris ( 2014-06-18 05:12:51 -0500 )edit

I know that the background is brighter than the foreground. Hm I am starting to get the feeling that you are right with your advice of using thresholding :-) Until now i have used global thresholding with Otsu's method and adaptive thresholding, but the results were never satisfying. Maybe I will try image partitioning or watersheds. Thanks again you helped a lot.

Josef gravatar imageJosef ( 2014-06-18 07:31:18 -0500 )edit

Question Tools

1 follower


Asked: 2014-06-17 09:10:39 -0500

Seen: 779 times

Last updated: Jun 17 '14