|
||||||||||||||||||||||||
Графика и обработка изображений: КвантизацияColor Quantization. Overview.Even today from time to time articles appear in computer graphics magazines that deal with algorithms which try to solve the color quantization problem more efficiently (e.g. /4/). This demonstrates that there is still a need for such algorithms and obviously some people work on color quantization techniques. Quantization of color images is necessary if the display on which a specific image is presented works with less colors than the original image. This is the case when a true or full color image is displayed on a device with a color lookup-table. Therefore the number of colors in the image has to be reduced. It has to be considered during color reduction that especially these colors are identified and selected which appear in the image very often and the substituted colors produce no or only little erros. This process is called color quantization.It is used for previewing and for controlling the rendering process. Quantization is approximating a true intensity value with a displayable intensity value. The objective of color quantization is displaying a full color image (24 Bits per pixel) with a restricted set of color numbers (256, 64, 16) without a significant (almost preceptually not noticeable by the spectator) lack of color impression approximation as closely as possible when quantized.
Generally speaking quantization can be viewed as a stepwise process:
To assess quantization techniques the following quality criterias are considered
Each slide contains the image that is to be quantized (left upper corner), the reduced color image (right upper corner), the error distribution between the original image and the manipulated image (lower left corner), and the applied methods (lower right corner).
Different quantization methods are investigated:
and combined with error diffusion techniques:
1 Static color look-up table A important drawback of this method is the artifact of appearing edges in the image. With a 24 bit full color image to be displayed on a monitor with a color palette of up to 256 entries the problem arises to remove apparent edges caused by very small changes in color which are a result of color shading. Even the algorithm that is very fast the result is not acceptable.
2 Median cut algorithm The algorithm starts with a box that encloses all the different color values from the original image. The "size" of the box is given by the minimum and maximum of each of the color coordinates that encloses the box we look at. For splitting the box we have to decide on which "side" we want to subivide the box. Therefore the points are sorted along the longest dimension of the box. The partitioning into two halves is made at the median point. Approximately equal numbers of points will fall at each side of the cutting plane. This step is applied until K boxes are generated and K maybe the maximum number of color entries in the available colormap. The according color to each box is calculated by averaging the colors in each box. Variations in the algorithm could be made by changing the criterion where to intersect the box. An alternative to sorting the color values form a minimum value to a maximum value could be to look at the coordinate with the largest variance. Another alternative could be to minimize the sum of variances for the two new boxes.
3 Popularity algorithm Therefore the colors are stored in a histogram. The K most frequently occuring colors are extracted and they are made the entries in the color table. Now the true image can be quantized. The only problem that has still to be solved is how to map the other colors that appear in the original image. To keep the error small we have to apply a method that finds one out of the K most frequently used color values in the nearest neighbourhood of the actual pixel. That is why, in general each pixel has to be tested to find the shortest distance to one of the K most color values. The error is measured as the squared distance in euclidian space:
with quant and orig as color triples in the RGB color space. The brut force method to compute the distance between a particular pixel value and all K representatives is time consuming (exhaustive search). To make the algorithm practical, searching the nearest neighbour must be fast. Basically distance testing with all the values in the color look-up table has to be performed or even better a smaller list of potential candidates to minimize d(x,y) can be preselected. This can be achieved by generating an N x N x N lattice of cubical cells in the color space. Each cell contains the set of color values that belong to the K most frequently used color values of the true image which are within a particular cell. In addition to that further values form the K most frequently used values are considered, when their distance form the cell is smaller than a distance d. The value of d is computed as the distance of the candidate nearest to the center of the cell and its farest corner (locally sorted search). Thereby computation time can be saved. The amount of memory can be reduced by avoiding the computation and management of unused cells. This algorithm (nearest neighbor algorithm) works as described below:
}
4 Octrees The most important data structure are the nodes of the octree. Each inner node of the octree contain a maximum of eight successors, the leave nodes keep information for the color value (colorvalue), the color index (colorindex), and a counter (colorcount) for the pixel that are already mapped to a particular leave. Because each of the red, green and blue value is between 0 and 255 the maximum depth of the tree is eight. In level i Bit i of RGB is used as selector for the successors. The octree is constructed during reading the image that is to be quantized. Only that parts of the octree are created that are really needed. Initially the first K values are represented exactly (in level eight). When the number of leaves nodes (currentK) exceeds K, the tree has to reduced. That would mean that leaves at the largest depth are substituted by their predecessor:
} currentK = currentK - children + 1 In the end the octree for the entire image is generated. The K leaves are used as entries for the color look-up table. the representative color value for a leave is computed as the mean value of the color value and the color count. The color index is also stored in the octree. The quantization of the image is performed in a second step. Again the image has to be read. The color values of each pixel are used for traversing the octree data structure. The search along a path through the tree is finished when a leave node is reached.
5 Error Distribution
5.1 Error Diffusion via a Dither Matrix Applying a dither matrix to an image that is displayed on a monitor that has not the full color spectrum available but only a selectable subset. By averaging the intensities of neighbouring pixels one can get the impression of colors that are not represented in the colormap. The human eye will do the spatial blending if the resolution of the display is high enough. Therefore a color value is compared to a threshold matrix. This fits as long as the spectator keeps a certain distance to the monitor, what is the normal case. Experiments with a monitor that has only a color look-up-table with 256 entries available show good results. For the 2 x 2 size the dither matrix, called D (2) is
The entire algorithm looks like described below: void set_color_palette( ) { int r, g, b, colorindex = 0; for (r=0; r<64; r+=9 ) for (g=0; g<64; g+=9 ) for (b=0; b<64; b+=21 ) { } } void put_dither_pixel(int x, int y, unsigned char *pix) { unsigned char r,g,b; unsigned char ri, gi, bi; unsigned char DitherMatrix[4][4] = unsigned char Ditherval; Ditherval = DitherMatrix[x&3][y&3]; r = pix[0] >> 2; g = pix[1] >> 2; b = pix[2] >> 2; ri = (r >> 3); if (ri != 0 && ((r&7)<<1) <= Ditherval ) ri--; gi = (g >> 3); if (gi != 0 && ((g&7)<<1) <= Ditherval ) gi--; bi = (b >> 4); if (bi != 0 && (b&15) <= Ditherval ) bi--; putpixel(x,y, (255 & ((ri<<5)) | (gi<<2) | bi)); }
5.2 Floyd Steinberg Even better results can be achivied by alternating the scan direction and the error diffusion form left to right and vice versa. The Floyd-Steinberg approach is a serial process and that any pixel value affects all the pixels to the lower right of that pixel in the image. Another approach, ordered dither techniques, localize the effect of any single pixel. One big drawback of this approach is that the error made by approximating the accurate value is accumulated over the entire image. All errors that were made on the pixels on the left and up a pixel in question have to be considered again and lead to a significantly larger error. This error is more disturbing the impression on the complete image than making more but smaller errors in approximating the color values of a pixel.
Conclusion If the algorithms are evaluated in terms of resource allocation (memory and computation time) and quality issues (what does the user realize when he/she views and compares the original image with the color reduced image?) with respect to the results based in the test image the following assessment can be given:
Octree algorithm The algorithm is well suited for images that have to be displayed with the best quality and the quantization process is not considered. Artefacts may appear as local discontinuities in a color shade. A combination between the octree algorithm and the dithering technique is not appreciated because the color table entries are not equidistant. In comparison with the other algorithms this approach needs a larger implementation effort.
Static color table in combination with dithering The combination of a static color table with dithering works well for interactive work and fast viewing of arbitrary images.
Static color table in combination with Floyd Steinberg
Median cut algorithm
Popularity algorithm Quantization can immensly be improved by applying error distribution techniques. Sometime efforts are made to speed up that process. The above mentioned methods are modified to improve computation time and/or reduce memory requirements:
Examples for this are:
Raster Display and Interaction It also allows to work interactivly with the system. The error distribution with Floyd Steinberg leads to visual artifacts (locally large errors).
Bibliography
/1/ J. D. Foley, A. van Dam, S. K. Feiner, J. F. Hughes, Computer Graphics - Principles and Practice, Second Edition, Addison-Wesley Publishing Company, 1990, ISBN 0-201- 12110-7.
|