0

I want to extract contours from a binary canny edge image.

The original image is:

enter image description here

After applying cvCanny() and cvDilate(), I get the following image:

enter image description here

I need the enclosing box(the entire blue box) to be detected as a contour. I apply cvFindContours() and extract the contour with the largest area. However, when I apply cvFindContours(), it modifies the above canny image as follows:

enter image description here

which is not what I intend to do. It then outputs the largest contour to be the mailbox sign inside the blue box.

What is going wrong? Does cvFindContours() modify the input image? What should be done to get just the enclosing blue box?

Thanks.

Hrishikesh_Pardeshi
  • 995
  • 4
  • 19
  • 45

1 Answers1

4

Yes, findContours indeed changes the images. If you still need your original image, than use findContours on copy of your image.

Instead of:

findContours(image, contours, mode, method);

Use:

findContours(image.clone(), contours, mode, method);

*Edit (answer to comment): *

It depends on what you define as "largest". If you use area this may be problematic because calling findContours on edge map may result in very long but very thin contours. Better definition of "largest" is contour whose bounding rectangle has biggest area. You can use function called boundingRect to find it. And if you want to find bounding box of all polygons use OR operator between all bounding boxes:

Rect bbox = boundingRect(contours[0]);
for(i=1; i<contours.size(); i++)
    bbox = bbox | boundingRect(contours[i]);
Michael Burdinov
  • 4,348
  • 1
  • 17
  • 28
  • Thanks. But, how do i go about to find the outermost blue box. Shouldn't it be the largest contour? – Hrishikesh_Pardeshi Oct 23 '13 at 09:54
  • A small update from the future. "Since OpenCV 3.2, findContours() no longer modifies the source image but returns a modified image as the first of three return parameters." And since 4.0.0, it no longer returns the modified image. – Nicolas Abril Sep 04 '19 at 17:19