Documentation Center

  • Trial Software
  • Product Updates

imfill

Fill image regions and holes

Syntax

BW2 = imfill(BW)
[BW2,locations] = imfill(BW)
BW2 = imfill(BW,locations)
BW2 = imfill(BW,'holes')
I2 = imfill(I)
BW2 = imfill(BW,locations,conn)
[gpuarrayI2]= imfill(gpuarrayI,___)

Description

BW2 = imfill(BW) displays the binary image BW on the screen and lets you define the region to fill by selecting points interactively by using the mouse. To use this interactive syntax, BW must be a 2-D image. Press Backspace or Delete to remove the previously selected point. A shift-click, right-click, or double-click selects a final point and starts the fill operation. Pressing Return finishes the selection without adding a point.

[BW2,locations] = imfill(BW) returns the locations of points selected interactively in locations. locations is a vector of linear indices into the input image. To use this interactive syntax, BW must be a 2-D image.

BW2 = imfill(BW,locations) performs a flood-fill operation on background pixels of the binary image BW, starting from the points specified in locations. If locations is a P-by-1 vector, it contains the linear indices of the starting locations. If locations is a P-by-ndims(BW) matrix, each row contains the array indices of one of the starting locations.

BW2 = imfill(BW,'holes') fills holes in the binary image BW. A hole is a set of background pixels that cannot be reached by filling in the background from the edge of the image.

I2 = imfill(I) fills holes in the grayscale image I. In this syntax, a hole is defined as an area of dark pixels surrounded by lighter pixels.

BW2 = imfill(BW,locations,conn) fills the area defined by locations, where conn specifies the connectivity. conn can have any of the following scalar values.

Value

Meaning

Two-dimensional connectivities

4

4-connected neighborhood

8

8-connected neighborhood

Three-dimensional connectivities

6

6-connected neighborhood

18

18-connected neighborhood

26

26-connected neighborhood

Connectivity can be defined in a more general way for any dimension by using for conn a 3-by-3-by- ... -by-3 matrix of 0's and 1's. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric about its center element.

[gpuarrayI2]= imfill(gpuarrayI,___) performs the fill operation on a GPU. The input image and the return image are 2-D gpuArrays. This syntax requires the Parallel Computing Toolbox™.

    Note:   The GPU implementation of this function does not support the interactive syntaxes where you select locations.

Specifying Connectivity

By default, imfill uses 4-connected background neighbors for 2-D inputs and 6-connected background neighbors for 3-D inputs. For higher dimensions the default background connectivity is determined by using conndef(NUM_DIMS,'minimal'). You can override the default connectivity with these syntaxes:

BW2 = imfill(BW,locations,conn)
BW2 = imfill(BW,conn,'holes')
I2  = imfill(I,conn)

To override the default connectivity and interactively specify the starting locations, use this syntax:

BW2 = imfill(BW,0,conn)

Code Generation

imfill supports the generation of efficient, production-quality C/C++ code from MATLAB.

When generating code, note the following:

  • The optional input arguments, conn and 'holes' must be a compile-time constants.

  • Supports only up to 3-D inputs. (No N-D support.)

  • The interactive mode to select points, imfill(BW,0,CONN) is not supported.

  • With the locations input argument, once you select a format at compile-time, you cannot change it at run-time. However, the number of points in locations can be varied at run-time.

Generated code for this function uses a precompiled platform-specific shared library. To see a complete list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

Class Support

The input image can be numeric or logical, and it must be real and nonsparse. It can have any dimension. The output image has the same class as the input image.

The input gpuArray image can have its underlying class be logical or numeric (excluding uint64 or int64), and it must be real and 2-D. The output image has the same class as the input image.

Examples

expand all

Fill Image from Specified Starting Point

BW1 = logical([1 0 0 0 0 0 0 0
               1 1 1 1 1 0 0 0
               1 0 0 0 1 0 1 0
               1 0 0 0 1 1 1 0
               1 1 1 1 0 1 1 1
               1 0 0 1 1 0 1 0
               1 0 0 0 1 0 1 0
               1 0 0 0 1 1 1 0]);

BW2 = imfill(BW1,[3 3],8)
BW2 =

     1     0     0     0     0     0     0     0
     1     1     1     1     1     0     0     0
     1     1     1     1     1     0     1     0
     1     1     1     1     1     1     1     0
     1     1     1     1     1     1     1     1
     1     0     0     1     1     1     1     0
     1     0     0     0     1     1     1     0
     1     0     0     0     1     1     1     0

Fill Holes in a Binary Image

BW4 = im2bw(imread('coins.png'));
BW5 = imfill(BW4,'holes');
imshow(BW4), figure, imshow(BW5)

Fill Holes in a Grayscale Image

I = imread('tire.tif');
I2 = imfill(I,'holes');
figure, imshow(I), figure, imshow(I2)

Fill Operation on a GPU

Fill in the background of a binary gpuArray image from a specified starting location.

BW1 = logical([1 0 0 0 0 0 0 0
              1 1 1 1 1 0 0 0
              1 0 0 0 1 0 1 0
              1 0 0 0 1 1 1 0
              1 1 1 1 0 1 1 1
              1 0 0 1 1 0 1 0
              1 0 0 0 1 0 1 0
              1 0 0 0 1 1 1 0]);
BW1 = gpuArray(BW1);    
BW2 = imfill(BW1,[3 3],8)

More About

expand all

Algorithms

imfill uses an algorithm based on morphological reconstruction [1].

References

[1] Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 173-174.

See Also

| |

Was this topic helpful?