views:

2642

answers:

6

I have an edge map extracted from edge detection module in OpenCV (canny edge detection). What I want to do is to fill the holes in the edge map.

I am using C++, and OpenCV libraries. In OpenCV there is a cvFloodFill() function, and it will fill the holes with a seed (with one of the location to start flooding). However, I am trying to fill all the interior holes without knowing the seeds.(similar to imfill() in MATLAB)

Q1: how to find all the seeds, so that I could apply 'cvFloodFill()'?
Q2: how to implement a 'imfill()' equivalent?

Newbie in OpenCV, and any hint is appreciated.

A: 

Try here http://oreilly.com/catalog/9780596516130

works for me!

Alan
I have already read the book, esp the floodfill part. I still can't find a clue.
Lily
+8  A: 

Accoring to the documentation of imfill in MATLAB:

BW2 = imfill(BW,'holes')     fills holes in the binary image BW. A hole is a set
         of background pixels that cannot be reached by filling in the background
         from the edge of the image.

Therefore to get the "holes" pixels, make a call to cvFloodFill with the corner pixel of the image as a seed. You get the holes by complementing the image obtained in the previous step.

MATLAB Example:

BW = im2bw( imread('coins.png') );
subplot(121), imshow(BW)

%# used here as if it was cvFloodFill
holes = imfill(BW, [1 1]);       %# [1 1] is the starting location point

BW(~holes) = 1;                  %# fill holes
subplot(122), imshow(BW)

screenshot1

screenshot2

Amro
that's awesome, Amro! Thanks!!!
Lily
+1  A: 

Have you tried ContourFinding over the Cannyied Image?

cvFindContours creates sort of a tree in which the outer countours are parents to the inner contours ('holes'). See contours.py sample. From the contours you could extract seeds

dnul
+1  A: 

Here's a quick and dirty approach:

  1. Perform canny on your input image so that the new binary image has 1's at the edges, and 0's otherwise
  2. Find the first 0 along a side of your edge image, and initiate a floodfill with 1's at that point on a blank image using your edge image as the mask. (We're hoping here that we didn't get unlucky and seed this first fill on the inside of a shape that is half-off the screen)
  3. This new floodfilled image is the 'background'. Any pixel here that has a 1 is the background, and any pixel that has a 0 is the foreground.
  4. Loop through the image and find any foreground pixels. Seed a floodfill on any you find.
  5. OR this new floodfilled image with your Canny image from step 1, and you're done.
Boatzart
A: 

Very useful thanks! if the image has shapes next to the edge it would be recommended to fill edges with zeros. e.g. shape touching the top left corner.

Jorge
A: 

Hello, the cvDrawContours function has an option to fill the contours that you have drawn.

Here is a short example cvDrawContours( IplImage, contours, color, color, -1, CV_FILLED, 8 );

Here is the documentation

http://opencv.willowgarage.com/documentation/drawing_functions.html?highlight=cvdrawcontours#cvDrawContours

I guess you posted this a long time ago, but I hope it helps someone

NotNamedDwayne