Create label matrix from
Read a binary image into the workspace.
BW = imread('text.png');
Calculate the connected components using
CC = bwconncomp(BW);
Create a label matrix using
L = labelmatrix(CC);
For comparison, create a second label matrix using
L2 = bwlabel(BW);
View both label matrices in the workspace. Note that
labelmatrix uses the smallest numeric class necessary for the number of objects and therefore is more memory efficient than
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. To make it easier to differentiate the different connected components, shuffle the color order of the labels.
CC— Connected components
Connected components, specified as a structure with four fields.
|Connectivity of the connected components (objects)|
|Size of the binary image|
|Number of connected components (objects) in the binary image.|
L— Label matrix
Label matrix of contiguous regions, returned as matrix of nonnegative
integers. The pixels labeled
0 are the background. The
1 make up one object; the pixels labeled
2 make up a second object; and so on.
The size of
L depends on
as shown in the table.