Ask Your Question

Hi we are beginner with opencv and we would like to obtain the code of the function imfill with opencv 2.2

asked 2014-02-06 08:12:41 -0600

Prisonniers gravatar image

In matlab, I used imfill (img2 = imfill(img,'holes')) to the image to fill the holes in a gray image and we don't know how to use her in opencv 2.2. Help us please.

edit retag flag offensive close merge delete


First use the search button on this forum, which would lead you to this topic and which already supplies a solution to the problem. However I have the idea that this is the same person looking for a solution. I would like to stress that this is not a 'please give me the solution' - forum ... imfill is a complex function which is internally programmed in matlab. If you want the same result in openCV, i suggest reading the basics on how imfill works ... for example start with the paper.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-06 08:22:25 -0600 )edit

Also if you are porting an application to openCV, stop using deprecated versions like opencv 2.2 but move to 2.4.8 which is the latest stable release.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-06 08:27:51 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-03-05 10:21:25 -0600

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,

void fillEdgeImage(cv::Mat edgesIn, cv::Mat& filledEdgesOut) const
    cv::Mat edgesNeg = edgesIn.clone();

    cv::floodFill(edgesNeg, cv::Point(0,0), CV_RGB(255,255,255));
    bitwise_not(edgesNeg, edgesNeg);
    filledEdgesOut = (edgesNeg | edgesIn);


Here is an example result

image description


edit flag offensive delete link more

Question Tools


Asked: 2014-02-06 08:12:41 -0600

Seen: 1,949 times

Last updated: Mar 05 '14