Image Processing Toolbox | ![]() ![]() |
Connected-Component Labeling
The bwlabel
and the bwlabeln
functions perform connected-component labeling, which is a method for identifying each object in a binary image. The bwlabel
function supports 2-D inputs only; the bwlabeln
function supports inputs of any dimension.
These functions return a matrix, called a label matrix. A label matrix is an image, the same size as the input image, in which the objects in the input image are distinguished by different integer values in the output matrix. For example, bwlabel
can identify the objects in this binary image.
BW = [0 0 0 0 0 0 0 0; 0 1 1 0 0 1 1 1; 0 1 1 0 0 0 1 1; 0 1 1 0 0 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 0 0 0 0 0]; X = bwlabel(BW,4) X = 0 0 0 0 0 0 0 0 0 1 1 0 0 3 3 3 0 1 1 0 0 0 3 3 0 1 1 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 2 2 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0
In the output matrix, the 1's represent one object, the 2's a second object, and the 3's a third. (If you had used 8-connected neighborhoods (the default), there would be only two objects, because the first and second objects would be a single object, connected along the diagonal.)
Viewing a Label Matrix
The label matrix returned by bwlabel
or bwlabeln
is of class double
; it is not a binary image. One way to view it is to display it as a pseudo-color indexed image, using label2rgb
. In the pseudo-color image, the number that identifies each object in the label matrix maps to a different color in the associated colormap matrix. When you view a label matrix as an RGB image, the objects in the image are easier to distinguish.
To illustrate this technique, this example uses label2rgb
to view the label matrix, X
. The call to label2rgb
specifies one of the standard MATLAB colormaps, jet
. The third argument, 'k'
, specifies the background color (black).
Figure 9-9: Using Color to Distinguish Objects in a Binary Image
![]() | Objects, Regions, and Feature Measurement | Selecting Objects in a Binary Image | ![]() |