Label connected components in binary image
L = bwlabeln(BW)
L = bwlabeln(BW,conn)
[L,n] = bwlabeln(___)
Create simple sample 3-D binary image.
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])
BW = BW(:,:,1) = 1 1 0 0 0 0 1 0 0 BW(:,:,2) = 0 1 0 0 0 0 0 1 0 BW(:,:,3) = 0 1 1 0 0 0 0 0 1
Label connected components in the image.
ans = 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
BW— Binary image
Binary image, specified as a real, nonsparse, numeric or logical array of
any dimension. For numeric input, any nonzero pixels are considered to be
BW = imread('text.png'); CC =
conn— Connectivity for the connected components
8for two dimensions;
26for three dimensions (default) |
Connectivity for the connected components, specified as one of the following scalar values.
To calculate the default connectivity for higher dimensions,
Connectivity can be defined in a more general way for any dimension using
a 3-by-3-by- ... -by-3 matrix of 0s and 1s.
conn must be
symmetric about its center element. The 1-valued elements define
neighborhood locations relative to
BW = imread('text.png'); L =
L— Label matrix
Label matrix, returned as an array of nonnegative integers with the same
BW. The pixels labeled
the background. The pixels labeled
1 make up one object;
the pixels labeled
2 make up a second object; and so
L— Label matrix
Label matrix of contiguous regions, returned as a 2-D array of nonnegative
integers of class
double. The kth
region includes all elements in
L that have value
k. The number of objects and holes represented by
L is equal to
zero-valued elements of
L make up the background.
n— Number of connected objects
Number of connected objects in
BW, returned as a
bwconncomp all compute
connected components for binary images.
the use of
bwlabeln. It uses
significantly less memory and is sometimes faster than the other
|Function||Input Dimension||Output Form||Memory Use||Connectivity|
|2-D||Label matrix with double-precision||High||4 or 8|
|N-D||Double-precision label matrix||High||Any|
To compute a label matrix having a more memory-efficient data type (e.g.,
double), use the
labelmatrix function on the
C = bwconncomp(BW); L = labelmatrix(CC); CC = bwconncomp(BW,n); 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.