This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Convert label matrix into RGB image


RGB = label2rgb(L)
RGB = label2rgb(L, map)
RGB = label2rgb(L, map, zerocolor)
RGB = label2rgb(L, map, zerocolor, order)


RGB = label2rgb(L) converts a label matrix, L, such as those returned by labelmatrix, bwlabel, bwlabeln, or watershed, 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 and range of colors in the colormap. The label2rgb function picks colors from the entire range.

RGB = label2rgb(L, map) specifies the colormap map to be used in the RGB image. map can have any of the following values:

  • n-by-3 colormap matrix

  • Name of a MATLAB® colormap function, such as 'jet' or 'gray' (See colormap for a list of supported colormaps.)

  • Function handle of a colormap function, such as @jet or @gray

If you do not specify map, the default value is 'jet'.

RGB = label2rgb(L, map, zerocolor) specifies the RGB color of the elements labeled 0 (zero) in the input label matrix L. As the value of zerocolor, specify an RGB triple or one of the colors listed in this table.



















If you do not specify zerocolor, the default value for zero-labeled elements is [1 1 1] (white).

RGB = label2rgb(L, map, zerocolor, order) controls how label2rgb assigns colormap colors to regions in the label matrix. If order is 'noshuffle' (the default), label2rgb assigns colormap colors to label matrix regions in numerical order. If order is 'shuffle', label2rgb assigns colormap colors pseudorandomly.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Class Support

The input label matrix L can have any numeric class. It must contain finite, nonnegative integers. The output of label2rgb is of class uint8.


collapse all

Read an image and display it.

I = imread('rice.png');
figure, imshow(I)

Create a label matrix from the image.

BW = im2bw(I, graythresh(I));
CC = bwconncomp(BW);
L = labelmatrix(CC);

Convert the label matrix into RGB image, using default settings.

RGB = label2rgb(L);
figure, imshow(RGB)

Convert label matrix into RGB image, specifying optional parameters.

RGB2 = label2rgb(L, 'spring', 'c', 'shuffle');
figure, imshow(RGB2)

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation for Image Processing.

When generating code, for best results when using the standard syntax RGB = label2rgb(L, map, zerocolor, order):

  • Submit at least two input arguments: the label matrix, L, and the colormap matrix, map.

  • map must be an n-by-3, double, colormap matrix. You cannot use the name of a MATLAB colormap function or a function handle of a colormap function.

  • If you set the boundary color zerocolor to the same color as one of the regions, label2rgb will not issue a warning.

  • If you supply a value for order, it must be 'noshuffle'.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Introduced before R2006a

Was this topic helpful?