Image Processing Toolbox User's Guide Previous page   Next Page

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.

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.

If you specify the same starting point, but use an 8-connected background connectivity, imfill fills the entire image.

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.


Previous page  Pixel Connectivity Finding Peaks and Valleys Next page

© 1994-2005 The MathWorks, Inc.