Label connected components in binary image
Calculate Centroids of 3-D Objects
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
numeric array | logical array
Binary image, specified as a numeric or logical array of any dimension.
For numeric input, any nonzero pixels are considered to be
BW = imread('text.png');
conn — Pixel connectivity
26 | 3-by-3-by- ... -by-3 matrix of
Pixel connectivity, specified as one of the values in this table. The
default connectivity is
8 for 2-D images, and
26 for 3-D images.
Pixels are connected if their edges touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal or vertical direction.
Pixels are connected if their edges or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal, vertical, or diagonal direction.
Pixels are connected if their faces touch. Two adjoining pixels are part of the same object if they are both on and are connected in:
Pixels are connected if their faces or edges touch. Two adjoining pixels are part of the same object if they are both on and are connected in
Pixels are connected if their faces, edges, or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected in
For higher dimensions,
bwlabeln uses the default value
L — Label matrix
array of nonnegative integers
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
n — Number of connected objects
Number of connected objects in
BW, returned as a
bwconncompall compute connected components for binary images.
bwconncompreplaces the use of
bwlabeln. It uses significantly less memory and is sometimes faster than the other functions.
To extract features from a binary image using
regionpropswith default connectivity, just pass
regionpropsusing the command
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.