labelmatrix
Create label matrix from objects in connected components
Syntax
Description
A label matrix assigns unique integer values to objects or connected components in a binary image. Use a label matrix to visualize distinct objects or connected components.
Examples
Find and Display Connected Components
Read a binary image into the workspace. Display the image.
BW = imread('text.png');
imshow(BW)
Calculate the connected components using bwconncomp
.
CC = bwconncomp(BW);
Create a label matrix using labelmatrix
. Each label has a unique numeric index.
L = labelmatrix(CC);
Find the maximum value of the label matrix. This value indicates the number of detected objects, in this case, 88.
numObjects = max(L(:))
numObjects = uint8
88
Display the label matrix as an image. Because the maximum label value is much smaller than the maximum value of the uint8
data type, increase the display range of the image to make the labels more distinct.
imshow(L,[])
It is challenging to see the objects labeled with small label values. Further, it is challenging to differentiate objects with comparable label values. To make it easier to differentiate the different connected components, display the label matrix as an RGB image using label2rgb
and shuffle the color order of the labels.
imshow(label2rgb(L,'jet','k','shuffle'));
Input Arguments
CC
— Connected components
structure
Connected components, specified as a structure with four fields. You can
get a connected components structure by using the bwconncomp
or bwpropfilt
function.
Field | Description |
---|---|
Connectivity | Connectivity of the connected components (objects) |
ImageSize | Size of the binary image |
NumObjects | Number of connected components (objects) in the binary image |
PixelIdxList | 1-by-NumObjects cell array where
the k-th element in the cell array
is a vector containing the linear indices of the pixels
in the k-th object |
Output Arguments
L
— Label matrix
matrix of nonnegative integers
Label matrix of contiguous regions, returned as matrix of nonnegative
integers. 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 size of L
is determined by the value of the
CC.ImageSize
field. The class of
L
depends upon the number of contiguous regions.
labelmatrix
uses the smallest data type that can
represent the number of objects, CC.NumObjects
, as shown
in the table.
Data Type | Range |
---|---|
uint8 |
|
uint16 | 256 ≤ |
uint32 | 65536 ≤ |
double |
|
Data Types: double
| uint8
| uint16
| uint32
Version History
Introduced in R2009a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)