# Algorithm that Fills the White Holes Within the Character Image

I have this degraded image :

Using paintbucket tool in Paint.NET Windows application, I fill the character image black.

My questions are :

1. What algorithm can we use to fill up the holes within the character image?
2. Is there any corresponding built-in function in Python that fills up the white holes in a character image?
3. What probable algorithm does the paint bucket tool in Paint.Net uses ?

Thanks.

Based on the blog post - The Paint Bucket in Paint.Net 4.0 (Video) I can tell it uses some edge detection to handle similar colors within a piece wise smooth area.

Usually the way it can be implemented is by defining color metric. How far a color is form another color. If it within the tolerance, it is within the mask.

1. Morphological operation can fill small holes.
2. I am not sure, but it won't be too hard to implement something similar.
3. Probably they convert the RGB data to HSL / HSV Color Model. Then just create a mask based on the distance between the reference point to the surrounding colors. Now the mask creates a logical (Black and White) mask to be filled.
In case of Paint.NET the algorithm should be something like Flood Fill.

So the steps are:

1. Convert image to the Color Model of choice (HSV / HSL or even LAB).
2. Generate a mask using color similarity (Distance Function) on the Color Model. The distance function is parameterized by some Threshold parameter.
3. Use Flood Fill like algorithm to fill the pixels in the mask using the target color. The mask is basically the stopping condition for the Flood Fill operation.

In MATLAB you have imfill() and Flood Fill.
For something more advanced you may use adaptive morphological functions like Active Contours and Morphological Snakes.