Image Processing Toolbox User's Guide |
Flood-Fill Operations
The imfill
function performs a flood-fill operation on binary and grayscale 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. See Finding Areas of High or Low Intensity for more information.) This operation can be useful in removing irrelevant artifacts from images.
This section includes information about
Specifying Connectivity
For both binary and grayscale images, the boundary of the fill operation is determined by the connectivity you specify.
The implications of connectivity can be illustrated with this matrix.
BW = [ 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. See the reference page for imfill
for more information about using imfill
interactively.
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 donut shaped because of reflections in the original photograph. Before doing any further processing of the image, you might want to first fill in the "donut holes" using imfill
.
Because the use of flood-fill to fill holes is so common, imfill
includes 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'); imshow(I);figure, imshow(Ifill)
Pixel Connectivity | Finding Peaks and Valleys |
© 1994-2005 The MathWorks, Inc.