| Image Processing Toolbox | ![]() |
Label connected components in N-D binary image
Syntax
Description
L = bwlabeln(BW) returns a label matrix, L, containing labels for the connected components in BW. BW can have any dimension; L is the same size as BW. 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. The default connectivity is 8 for two dimensions, 26 for three dimensions, and conndef(ndims(BW), 'maximal') for higher dimensions.
[L,NUM] = bwlabeln(BW) returns in NUM the number of connected objects found in BW.
[L,NUM] = bwlabeln(BW,CONN) specifies the desired connectivity. CONN may have any of the following scalar values.
Connectivity may also be defined in a more general way for any dimension by using for CONN a 3-by-3-by- ...-by-3 matrix of 0's and 1's. The 1-valued elements define neighborhood locations relative to the center element of CONN. Note that CONN must be symmetric about its center element.
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 will probably be faster; otherwise bwlabeln will probably be faster.
Class Support
BW can be numeric or logical, and it must be real and nonsparse. L is of class double.
Example
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
Algorithm
bwlabeln uses the following general procedure:
See Also
Reference
[1] Robert Sedgewick, Algorithms in C, 3rd ed., Addison-Wesley, 1998, pp. 11-20.
| bwlabel | bwmorph | ![]() |