label2rgb
Convert label matrix into RGB image
Syntax
Description
converts a label image, RGB
= label2rgb(L
)L
into an RGB color image for the
purpose of visualizing the labeled regions. The label2rgb
function determines the color to assign to each object based on the number of
objects in the label matrix. The label2rgb
function picks
colors from the entire range of the colormap.
enables you to specify that the function return a list of unique colors instead of
an RGB image.RGB
= label2rgb(___,"OutputFormat",outputFormat
)
Examples
Use Color to Highlight Elements in Label Matrix
Read an image and display it.
I = imread('rice.png');
imshow(I)
Create a label matrix from the image.
BW = imbinarize(I); CC = bwconncomp(BW); L = labelmatrix(CC);
Convert the label matrix into RGB image, using default settings.
RGB = label2rgb(L); figure imshow(RGB)
Convert the label matrix into an RGB image, specifying optional parameters. This example uses the 'spring'
colormap, sets background pixels to the color cyan, and randomizes how colors are assigned to the labels.
RGB2 = label2rgb(L,'spring','c','shuffle'); figure imshow(RGB2)
Input Arguments
L
— Label image
matrix of nonnegative integers | categorical matrix
Label image of contiguous regions, specified as one of the following.
A matrix of nonnegative integers. Pixels labeled
0
are the background. Pixels labeled1
make up one object; pixels labeled2
make up a second object; and so on. You can get a numeric label image from labeling functions such aswatershed
orlabelmatrix
.A categorical matrix.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| categorical
cmap
— Colormap
"jet"
(default) | c-by-3 matrix | colormap function | handle
Colormap to be used in the generated color image RGB
,
specified as one of the following.
Value | Description |
---|---|
c-by-3 matrix of data type
double | Colormap matrix specifying c
colors, each as an RGB triple. c
must be greater than or equal to the number of
labels, numlabels, in label
matrix If
c is greater than
numlabels, then
|
colormap function | Name of a MATLAB® colormap function, such as
|
colormap handle | Handle of a colormap function, such as
|
zerocolor
— Fill color
[1 1 1]
(white) (default) | 3-element vector | "b"
| "c"
| "g"
Fill color, specified as a 3-element vector representing an RGB triple or
one of the following color abbreviations for numeric label images.
label2rgb
applies the fill color to the label
0
for numeric label images or the label
<undefined>
for categorical label
images.
Value | Color |
---|---|
"b" | Blue |
"c" | Cyan |
"g" | Green |
"k" | Black |
"m" | Magenta |
"r" | Red |
"w" | White |
"y" | Yellow |
order
— Color order
"noshuffle"
(default) | "shuffle"
Color order, specified as "noshuffle"
or
"shuffle"
. The "noshuffle"
order
arranges colormap colors to label matrix regions in numerical order. The
"shuffle"
order assigns colormap colors
pseudorandomly.
outputFormat
— Output format
"image"
(default) | "triplets"
Output format of the RGB data returned in RGB
,
specified as one of the following.
"image"
— Return an RGB image. If the size of the input label matrixL
isM
-by-N
, then the size of the output RGB image isM
-by-N
-by-3."triplets"
— Return a list of RGB colors. The size of the output is aC
-by-3 matrix containing an RGB triplet for each of theC
labels in the input label matrix.
Output Arguments
RGB
— RGB data
numeric matrix
RGB data, returned as an numeric matrix.
Data Types: uint8
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
label2rgb
supports the generation of C code (requires MATLAB Coder™). For more information, see Code Generation for Image Processing.Input label images of data type categorical are not supported.
When generating code, for best results when using the standard syntax
RGB = label2rgb(L,cmap,zerocolor,order)
:Submit at least two input arguments: the label matrix,
L
, and the colormap matrix,cmap
.cmap
must be a c-by-3 matrix of data typedouble
. You cannot specify the name of a MATLAB colormap function or a function handle of a colormap function.If you set the background color
zerocolor
to the same color as one of the regions, thenlabel2rgb
will not issue a warning.If you supply a value for
order
, then it must be"noshuffle"
.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Input label images of data type categorical are not supported.
When generating code, for best results when using the standard syntax
RGB = label2rgb(L,cmap,zerocolor,order)
:Submit at least two input arguments: the label matrix,
L
, and the colormap matrix,cmap
.cmap
must be a c-by-3 matrix of data typedouble
. You cannot specify the name of a MATLAB colormap function or a function handle of a colormap function.If you set the background color
zerocolor
to the same color as one of the regions, thenlabel2rgb
will not issue a warning.If you supply a value for
order
, then it must be"noshuffle"
.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006aR2022b: Generate CUDA code using GPU Coder
label2rgb
now supports the generation of
optimized CUDA® code (requires GPU Coder™).
R2021b: Support for thread-based environments
label2rgb
now supports thread-based
environments.
See Also
bwconncomp
| bwlabel
| bwlabeln
| colormap
| ismember
| labelmatrix
| watershed
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)