Convert RGB image to indexed image
___ = rgb2ind(___,
enables or disables dithering.
Read and display a truecolor
uint8 JPEG image of a nebula.
RGB = imread('ngc6543a.jpg'); figure imagesc(RGB) axis image zoom(4)
Convert RGB to an indexed image with 32 colors.
[IND,map] = rgb2ind(RGB,32); figure imagesc(IND) colormap(map) axis image zoom(4)
RGB— RGB image
RGB image, specified as an m-by-n-by-3 array.
Q— Number of quantized colors
Number of quantized colors used for minimum variance quantization,
specified as a positive integer that is less than or equal to 65,536. The
Tolerance used for uniform quantization, specified as a number in the
range [0, 1]. The returned colormap
(floor(1/tol)+1)^3 or fewer colors.
inmap— Input colormap
Input colormap, specified as a c-by-3 matrix with
values in the range [0, 1]. Each row of
inmap is a
three-element RGB triplet that specifies the red, green, and blue components
of a single color of the colormap. The colormap has a maximum of 65,536
dithering— Perform dithering
Perform dithering, specified as
'nodither'. Dithering increases the color resolution
at the expense of spatial resolution. For more information, see
If you select
rgb2ind does not perform dithering. Instead, the
function maps each color in the original image to the closest color in the
X— Indexed image
Indexed image, returned as an m-by-n
matrix of nonnegative integers. If the length of
less than or equal to 256, then the output image is of class
uint8. Otherwise, the output image is of class
uint16. The value
0 in the output
X corresponds to the first color in the
The values in image
X are indexes into the colormap
map and should not be used in mathematical
processing, such as filtering operations.
Colormap, returned as a c-by-3 matrix with values in
the range [0, 1]. Each row of
cmap is a three-element
RGB triplet that specifies the red, green, and blue components of a single
color of the colormap. The colormap has a maximum of 65,536 colors.
Uniform Quantization — If you specify
rgb2ind uses uniform quantization to convert the image.
Uniform quantization cuts the RGB color cube into smaller cubes of length
tol. For example, if you specify a
of 0.1, then the edges of the cubes are one-tenth the length of the RGB cube.
The total number of small cubes is:
t = (floor(1/tol)+1)^3
Each cube represents a single color in the output image. Therefore,
t is the maximum length of the colormap .
rgb2ind removes any colors that don’t appear in the input
image, so the actual colormap can be smaller than
Minimum Variance Quantization — If you specify
rgb2ind uses minimum variance quantization. Minimum
variance quantization cuts the RGB color cube into smaller boxes (not
necessarily cubes) of different sizes, depending on how the colors are
distributed in the image. If the input image actually uses fewer colors than the
number specified, then the output colormap is also smaller.
Inverse Colormap — If you specify an input colormap
rgb2ind uses colormap
mapping. The inverse colormap algorithm quantizes the specified colormap into 32
distinct levels per color component. Then, for each pixel in the input image,
the closest color in the quantized colormap is found.
 Spencer W. Thomas, "Efficient Inverse Color Map Computation", Graphics Gems II, (ed. James Arvo), Academic Press: Boston. 1991. (includes source code)