Label connected components in binary image
L = bwlabeln(BW)
[L, NUM] = bwlabeln(BW)
[L, NUM] = bwlabeln(BW, conn)
L = bwlabeln(BW) returns
a label matrix,
L, containing labels for the connected
BW. The input image
have any dimension;
L is the same size as
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
2 make up a second object; and so on. The
default connectivity is 8 for two dimensions, 26 for three dimensions,
conndef(ndims(BW), 'maximal') for higher dimensions.
[L, NUM] = bwlabeln(BW) returns
NUM the number of connected objects found in
[L, NUM] = bwlabeln(BW, conn) specifies
the desired connectivity.
conn can have any of
the following scalar values.
Connectivity can also be defined in a more general way for any
dimension by using for
conn a 3-by-3-by- ...-by-3
elements define neighborhood locations relative to the central element
conn. Note that
be symmetric about its central element.
bwconncomp all compute connected components
for binary images.
bwconncomp replaces the use
uses significantly less memory and is sometimes faster than the older
|Function||Input Dimension||Output Form||Memory Use||Connectivity|
|2-D||Double-precision label matrix||High||4 or 8|
|N-D||Double-precision label matrix||High||Any|
BW can be numeric or logical, and it must
be real and nonsparse.
L is of class
Calculate the centroids of the 3-D objects.
BW = cat(3, [1 1 0; 0 0 0; 1 0 0],... [0 1 0; 0 0 0; 0 1 0],... [0 1 1; 0 0 0; 0 0 1]) bwlabeln(BW) ans(:,:,1) = 1 1 0 0 0 0 2 0 0 ans(:,:,2) = 0 1 0 0 0 0 0 2 0 ans(:,:,3) = 0 1 1 0 0 0 0 0 2
To extract features from a binary image using
default connectivity, just pass
BW directly into
To compute a label matrix having a more memory-efficient data
labelmatrix function on the output of
C = bwconncomp(BW); L = labelmatrix(CC);
CC = bwconncomp(BW, conn); S = regionprops(CC);
bwlabeln uses the following general procedure:
Scan all image pixels, assigning preliminary labels to nonzero pixels and recording label equivalences in a union-find table.
Resolve the equivalence classes using the union-find algorithm .
Relabel the pixels based on the resolved equivalence classes.
 Sedgewick, Robert, Algorithms in C, 3rd Ed., Addison-Wesley, 1998, pp. 11-20.