|
IDL Reference Guide: Procedures and Functions |
|
The COLOR_QUAN function quantizes a TrueColor image and returns a pseudo-color image and palette to display the image on standard pseudo-color displays. The output image and palette can have from 2 to 256 colors.
COLOR_QUAN solves the general problem of accurately displaying decomposed, TrueColor images, that contain a palette of up to 224 colors, on pseudo-color displays that can only display 256 (or fewer) simultaneous colors.
One of two color quantization methods can be used:
The original colors are then mapped to the nearest output color, and the original image is resampled to the new palette with optional Floyd-Steinberg color dithering. The resulting pseudo-color image and palette are usually a good approximation of the original image.
The number of colors in the output palette defaults to the number of colors supported by the currently-selected graphics output device. The number of colors can also be specified by the COLOR keyword parameter.
The CUBE method has the advantage that the color tables it produces are independent of the input image, so that multiple quantized images can be viewed simultaneously. The statistical method usually provides a better-looking result and a smaller global error.
COLOR_QUAN can use the same color mapping for a series of images. See the descriptions of the GET_TRANSLATION, MAP_ALL, and TRANSLATION keywords, below.
Result = COLOR_QUAN( Image_R, Image_G, Image_B, R, G, B)
or
Result = COLOR_QUAN( Image, Dim, R, G, B )
Keywords: [, COLORS=integer{2 to 256}] [, CUBE={2 | 3 | 4 | 5 | 6} | , GET_TRANSLATION=variable [, /MAP_ALL]] [, /DITHER] [, ERROR=variable] [, TRANSLATION=vector]
The input image parameter can be passed as either three, separate color-component arrays (Image_R, Image_G, Image_B) or as a three-dimensional array containing all three components, Image, and a scalar, Dim, indicating the dimension over which the colors are interleaved.
Returns a pseudo-color image composed of 2 to 256 colors.
Arrays containing the red, green, and blue components of the decomposed TrueColor image. For best results, the input image(s) should be scaled to the range of 0 to 255.
A three-dimensional array containing all three components of the TrueColor image.
A scalar that indicates the method of color interleaving in the Image parameter. A value of 1 indicates interleaving by pixel: (3, n, m). A value of 2 indicates interleaving by row: (n, 3, m). A value of 3 indicates interleaving by image: (n, m, 3).
Three output byte arrays containing the red, green, and blue components of the output palette.
The number of colors in the output palette. This value must be at least 2 and not greater than 256. The default is the number of colors supported by the current graphics output device.
If this keyword is set, the color space is divided into CUBE3 volumes, to which the input image is quantized. This result is always Floyd-Steinberg dithered. The value of CUBE can range from 2 to 6; providing from 23 = 8, to 63 = 216 output colors. If this keyword is set, the COLORS, DITHER, and ERROR keywords are ignored.
Set this keyword to dither the output image. Dithering can improve the appearance of the output image, especially when using relatively few colors.
Set this optional keyword to a named variable. A measure of the quantization error is returned. This error is proportional to the square of the Euclidean distance, in RGB space, between corresponding colors in the original and output images.
Set this keyword to a named variable in which the mapping between the original RGB triples (in the TrueColor image) and the resulting pseudo-color indices is returned as a vector. Do not use this keyword if CUBE is set.
Set this keyword to establish a mapping for all possible RGB triples into pseudo-color indices. Set this keyword only if GET_TRANSLATION is also present. Note that mapping all possible colors requires more compute time and slightly degrades the quality of the resultant color matching.
Set this keyword to a vector of translation indices obtained by a previous call to COLOR_QUAN using the GET_TRANSLATION keyword. The resulting image is quantized using this vector.
The following code segment reads a TrueColor, row interleaved, image from a disk file, and displays it on the current graphics display, using a palette of 128 colors:
;Open an input file: OPENR, unit, 'XXX.DAT', /GET_LUN ;Dimensions of the input image: a = BYTARR(512, 3, 480, /NOZERO) ;Read the image: READU, unit, a ;Close the file: FREE LUN, unit ;Show the quantized image. The 2 indicates that the colors are ;interleaved by row: TV, COLOR_QUAN(a, 2, r, g, b, COLORS=128) ;Load the new palette: TVLCT, r, g, b
To quantize the image into 216 equal-volume color cubes, replace the call to COLOR_QUAN with the following:
TV, COLOR_QUAN(a, 2, r, g, b, CUBE=6)
Although it is a relatively simple process to convert an RGB image to a grayscale image, the process needed to convert an RGB image to an indexed image is more complex. This process is more complex because the millions of possible colors provided by an RGB image must be decomposed into the 256 colors used by an indexed image. IDL's COLOR_QUAN function may be used to perform this process.
The following example shows how to use the COLOR_QUAN function to convert an RGB image to an indexed image. The elev_t.jpg file contains a pixel interleaved RGB image, which has its own color information. This example converts the image to an indexed image with an associated color table. Complete the following steps for a detailed description of the process.
| Example Code See rgbtoindexed.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. |
elev_t.jpg file:
elev_tFile = FILEPATH('elev_t.jpg', $
SUBDIRECTORY = ['examples', 'data'])
elev_t.jpg file into IDL:READ_JPEG, elev_tFile, elev_tImage
elev_tSize = SIZE(elev_tImage, /DIMENSIONS)
DEVICE, DECOMPOSED = 1
WINDOW, 0, TITLE = 'elev_t.jpg', $ XSIZE = elev_tSize[1], YSIZE = elev_tSize[2]
TV, elev_tImage, TRUE = 1
The following figure shows the original RGB image.
| Note If you are running IDL on a PseudoColor display, the RGB image will not be displayed correctly. A PseudoColor display only allows the display of indexed images. You can change the RGB image to an indexed image with the COLOR_QUAN routine. An example of this method is shown in this section. |
The RGB image is converted to an indexed image with the COLOR_QUAN routine, but the DECOMPOSED keyword to the DEVICE command must be set to zero (for TrueColor displays) before using COLOR_QUAN because it is a color table related routine. See COLOR_QUAN for more information.
| Note COLOR_QUAN may result in some loss of color information since it quantizes the image to a fixed number of colors (stored in the color table). |
DEVICE, DECOMPOSED = 0
imageIndexed = COLOR_QUAN(elev_tImage, 1, red, green, $ blue)
WRITE_PNG, 'elev_t.png', imageIndexed, red, green, blue
IDL Online Help (March 06, 2007)