Image Processing Toolbox User's Guide Previous page   Next Page
bwlabel

Label connected components in a binary image

Syntax

Description

L = bwlabel(BW,n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW. n can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects; if the argument is omitted, it defaults to 8.

The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one object, the pixels labeled 2 make up a second object, and so on.

[L,num] = bwlabel(BW,n) returns in num the number of connected objects found in BW.

Remarks

bwlabel supports 2-D inputs only; bwlabeln supports inputs of any dimension. In some cases, you might prefer to use bwlabeln even for 2-D problems because it can be faster. If you have a 2-D input whose objects are relatively thick in the vertical direction, bwlabel is probably faster; otherwise bwlabeln is probably faster.

Class Support

BW can be logical or numeric, and it must be real, two-dimensional, and nonsparse. L is of class double.

Remarks

You can use the MATLAB find function in conjunction with bwlabel to return vectors of indices for the pixels that make up a specific object. For example, to return the coordinates for the pixels in object 2,

You can display the output matrix as a pseudocolor indexed image. Each object appears in a different color, so the objects are easier to distinguish than in the original image. See label2rgb for more information.

Example

This example illustrates using 4-connected objects. Notice objects 2 and 3; with 8-connected labeling, bwlabel would consider these a single object rather than two separate objects.

Algorithm

bwlabel uses the general procedure outlined in reference [1], pp. 40-48:

  1. Run-length encode the input image.
  2. Scan the runs, assigning preliminary labels and recording label equivalences in a local equivalence table.
  3. Resolve the equivalence classes.
  4. Relabel the runs based on the resolved equivalence classes.

See Also

bweuler, bwlabeln, bwselect, label2rgb

Reference

[1]  Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992, pp. 28-48.


Previous page  bwhitmiss bwlabeln Next page

© 1994-2005 The MathWorks, Inc.