labelmatrix() - Image Processing
L = labelmatrix(CC) creates a label matrix
from the connected components structure CC returned
by bwconncomp. The size of L is CC.ImageSize.
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
class of L depends on CC.NumObjects,
as shown in the following table.ClassRange
'uint8'CC.NumObjects ≤ 255
'uint16'256 ≤ CC.NumObjects ≤ 65535
'uint32'65536 ≤ CC.NumObjects ≤232−1
'double'CC.NumObjects ≥232
labelmatrix is more memory efficient than bwlabel and bwlabeln because
it returns its label matrix in the smallest numeric class necessary
for the number of objects.
Syntax
L = labelmatrix(CC)
Example
Calculate connected components and display resultsOpen This Example
Read binary image into the workspace.BW = imread('text.png');
Calculate the connected components, using bwconncomp .CC = bwconncomp(BW);
Create a label matrix, using labelmatrix .L = labelmatrix(CC);
For comparison, create a second label matrix, using bwlabel .L2 = bwlabel(BW);
View both label matrices in the workspace. Note that labelmatrix is more memory efficient than bwlabel , using the smallest numeric class necessary for the number of objects.whos L L2
Name Size Bytes Class Attributes
L 256x256 65536 uint8
L2 256x256 524288 double
Display the label matrix as an RGB image, using label2rgb .figure
imshow(label2rgb(L));
Output / Return Value
Limitations
Alternatives / See Also
Reference