This question already has an answer here:
- Filling holes inside a binary object 7 answers
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.
According to the documentation of
imfill
in MATLAB:Therefore to get the "holes" pixels, make a call to
cvFloodFill
with the left corner pixel of the image as a seed. You get the holes by complementing the image obtained in the previous step.MATLAB Example:
Here's a quick and dirty approach:
Just an appendix for Amro's answer.
If you have the points from the edges you can use fillConvexPoly() or fillPoly() (if poly not convex).
One way to get the points from edges is to do findContours() -> approxPolyDP().
I made a simple function that is equivalent to matlab's imfill('holes'). I've not tested it for many cases, but it has worked so far. I'm using it on edge images but it accepts any kind of binary image, like from a thresholding operation.
A hole is no more than a set of pixels that cannot be "reached" when background is filled, so,
Here is an example result
Recently I'am also finding the solution to this problem. Here I implemented Amro 's idea as follows:
Hope this will be helpful.