Image Processing Toolbox User's Guide |
Convert an RGB image to an indexed image
Syntax
[X,map] = rgb2ind(RGB,n)
X = rgb2ind(RGB,map)
[X,map] = rgb2ind(RGB,tol)
[...] = rgb2ind(...,dither_option
)
Description
rgb2ind
converts RGB images to indexed images using one of three different methods:
For all these methods, rgb2ind
also dithers the image unless you specify 'nodither'
for dither_option
.
[X,map] = rgb2ind(RGB,n)
converts the RGB image to an indexed image X
using minimum variance quantization. map
contains at most n
colors. n
must be less than or equal to 65536.
X = rgb2ind(RGB,map)
converts the RGB image to an indexed image X
with colormap map
by matching colors in RGB
with the nearest color in the colormap map
. size(map,1)
must be less than or equal to 65536.
[X,map] = rgb2ind(RGB,tol)
converts the RGB image to an indexed image X
using uniform quantization. map
contains at most (floor(1/tol)+1)^3
colors. tol
must be between 0.0 and 1.0.
[...] = rgb2ind(...,dither_option)
enables or disables dithering. dither_option
is a string that can have one of these values:
'dither'
(default) dithers, if necessary, to achieve better color resolution at the expense of spatial resolution.
'nodither'
maps each color in the original image to the closest color in the new map. No dithering is performed.
Note
The values in the resultant image X are indexes into the colormap map and cannot be used in mathematical processing, such as filtering operations.
|
Class Support
The input image can be of class uint8
, uint16
, single
, or double
. If the length of map
is less than or equal to 256, the output image is of class uint8
. Otherwise, the output image is of class uint16
.
Remarks
If you specify tol
, rgb2ind
uses uniform quantization to convert the image. This method involves cutting the RGB color cube into smaller cubes of length tol
. For example, if you specify a tol
of 0.1, the edges of the cubes are one-tenth the length of the RGB cube. The total number of small cubes is
Each cube represents a single color in the output image. Therefore, the maximum length of the colormap is n
. rgb2ind
removes any colors that don't appear in the input image, so the actual colormap can be much smaller than n
.
If you specify n
, rgb2ind
uses minimum variance quantization. This method involves cutting 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 you specify, the output colormap is also smaller.
If you specify map
, rgb2ind
uses colormap mapping, which involves finding the colors in map
that best match the colors in the RGB image.
Example
See Also
cmunique
, dither
, imapprox
, ind2rgb
, rgb2gray
rgb2hsv | rgb2ntsc |
© 1994-2005 The MathWorks, Inc.