Flood-Fill Operations
The imfill
function performs a
flood-fill operation on binary and grayscale images. This
operation can be useful in removing irrelevant artifacts from images.
For binary images,
imfill
changes connected background pixels (0
s) to foreground pixels (1
s), stopping when it reaches object boundaries.For grayscale images,
imfill
brings the intensity values of dark areas that are surrounded by lighter areas up to the same intensity level as surrounding pixels. In effect,imfill
removes regional minima that are not connected to the image border. For more information, see Find Image Peaks and Valleys.
Specifying Connectivity
For both binary and grayscale images, the boundary of the fill operation is determined by the pixel connectivity that you specify.
Note
imfill
differs from the other object-based operations in
that it operates on background pixels. When you specify
connectivity with imfill
, you are specifying the
connectivity of the background, not the foreground.
The implications of connectivity can be illustrated with this matrix.
BW = logical([0 0 0 0 0 0 0 0; 0 1 1 1 1 1 0 0; 0 1 0 0 0 1 0 0; 0 1 0 0 0 1 0 0; 0 1 0 0 0 1 0 0; 0 1 1 1 1 0 0 0; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0]);
If the background is 4-connected, this binary image contains two separate background elements (the part inside the loop and the part outside). If the background is 8-connected, the pixels connect diagonally, and there is only one background element.
Specifying the Starting Point
For binary images, you can specify the starting point of the fill operation by
passing in the location subscript or by using imfill
in interactive mode,
selecting starting pixels with a mouse.
For example, if you call imfill
, specifying the pixel
BW(4,3)
as the starting point, imfill
only fills the inside of the loop because, by default, the background is
4-connected.
imfill(BW,[4 3])
ans = 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
If you specify the same starting point, but use an 8-connected background
connectivity, imfill
fills the entire image.
imfill(BW,[4 3],8)
ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Filling Holes
A common use of the flood-fill operation is to fill holes in images. For example,
suppose you have an image, binary or grayscale, in which the foreground objects
represent spheres. In the image, these objects should appear as disks, but instead
are doughnut shaped because of reflections in the original photograph. Before doing
any further processing of the image, you might want to first fill in the "doughnut
holes" using imfill
.
Because the use of flood-fill to fill holes is so common,
imfill
includes a special syntax to support it for both
binary and grayscale images. In this syntax, you just specify the argument
'holes'
; you do not have to specify starting locations in
each hole.
To illustrate, this example fills holes in a grayscale image of a spinal column.
[X,map] = imread('spine.tif'); I = ind2gray(X,map); Ifill = imfill(I,'holes'); figure montage({I,Ifill})