4ImageMagick Color Reduction Algorithm4NF,
C


GThis document describes how ImageMagickIperforms color reduction on an image. To fully understand this document,;you should have a knowledge of basic imaging techniques and(the tree data structure and terminology.

?For purposes of color allocation, an image is a set of nJpixels, where each pixel is a point in RGB space. RGB spaceAis a 3-dimensional vector space, and each pixel, p(i), is4defined by an ordered triple of red, green, and blue3coordinates, (r(i),g(i),b(i)).

GEach primary color component (red, green, or blue)9represents an intensity which varies linearly from 0 to aCmaximum value, Cmax, which corresponds to full saturation of6that color. Color allocation is defined over a domainAconsisting of the cube in RGB space with opposite verticesIat (0,0,0) and (Cmax,Cmax,Cmax). ImageMagick!requires Cmax= 255.

8The algorithm maps this domain onto a tree in which each<node represents a cube within that domain. In the following<discussion, these cubes are defined by the coordinate of two>opposite vertices: The vertex nearest the origin in RGB.space and the vertex farthest from the origin.

6The tree's root node represents the the entire domain,O(0,0,0) through (Cmax,Cmax,Cmax). Each lower level in the;tree is generated by subdividing one node's cube into eight;smaller cubes of equal size. This corresponds to bisecting<the parent cube with planes passing through the midpoints of each edge.

-The basic algorithm operates in three phases:





Classification@builds a color description tree for the image. Reduction;collapses the tree until the number it represents, at most,4is the number of colors desired in the output image.?Assignment defines the output image's color map and sets;each pixel's color by reclassification in the reduced tree.>Our goal is to minimize the numerical discrepancies between<the original colors and quantized colors. To learn more:about quantization error, see Measuring Color Reduction!Error later in this document.

@Classification begins by initializing a color description9tree of sufficient depth to represent each possible input:color in a leaf. However, it is impractical to generate a9fully-formed color description tree in the classification4phase for realistic values of Cmax. If color components in@the input image are quantized to k-bit precision, so thatNCmax = 2^k-1, the tree would need k levels below the root9node to allow representing each possible input color in a:leaf. This becomes prohibitive because the tree's:

/      total number of nodes = 1+Sum(8^i), i=1,k      For k=8, -      Number of nodes= 1 + (8^1+8^2+....+8^8)'                               8^8 - 1 (                     = 1 + 8.-----------$                               8 - 1"                     = 19,173,961 


ITherefore, to avoid building a fully populated tree, ImageMagick: 

    *
  1. Initializes data structures for nodesonly as they are needed; $
  2. Chooses a maximum depth for the9tree as a function of the desired number of colors in theDoutput image (currently based-two logarithm of Cmax). 

      For Cmax=255,                           &      Maximum tree depth = log (255)                                2.                         = log (255) / log (2)+                              e           e#                         =7.99 ~= 8
DA tree of this depth generally allows the best representation of the9source image with the fastest computational speed and the6least amount of memory. However, the default depth is9inappropriate for some images. Therefore, the caller canrequest a specific tree depth.

7For each pixel in the input image, classification scans9downward from the root of the color description tree. At;each level of the tree, it identifies the single node whichCrepresents a cube in RGB space containing the pixel's color.1It updates the following data for each such node:





n1:?
Number of pixels whose color is contained in the RGB cube which this node represents;



n2:8
Number of pixels whose color is not represented in a8node at lower depth in the tree; initially, n2=0(for all nodes except leaves of the tree.



Sr,Sg,Sb:N
Sums of the red, green, and blue component values for0all pixels not classified at a lower depth. The7combination of these sums and n2 will ultimately.characterize the mean color of a set of pixelsrepresented by this node.

E:?
The distance squared in RGB space between each pixel4contained within a node and the nodes' center. This-represents the quantization error for a node.



?Reduction repeatedly prunes the tree until the number ofGnodes with n2 > 0 is less than or equal to the maximumNnumber of colors allowed in the output image. On any given iteration over the,tree, it selects those nodes whose E 3value is minimal for pruning and merges their color>statistics upward. It uses a pruning threshold, Ep, to!govern node selection as follows:

      Ep = 0S      while number of nodes with (n2 > 0) > required maximum number of colors-         prune all nodes such that E <= Ep0         Set Ep  to minimum E in remaining nodes


8This has the effect of minimizing any quantization error when merging two nodes together.

3When a node to be pruned has offspring, the pruning:procedure invokes itself recursively in order to prune theOtree from the leaves upward. The values of n2,Sr, Sg and8Sb in a node being pruned are always added to the;corresponding data in that node's parent. This retains the8pruned node's color characteristics for later averaging.

:For each node, n2 pixels exist for which that nodeCrepresents the smallest volume in RGB space containing thoseKpixel's colors. When n2 > 0 the node will uniquely define<a color in the output image. At the beginning of reduction,Gn2 = 0 for all nodes except the leaves of the tree which,represent colors present in the input image.

@The other pixel count, n1, indicates the total number of9colors within the cubic volume which the node represents.IThis includes n1 - n2 pixels whose colors should be defined&by nodes at a lower level in the tree.

BAssignment generates the output image from the pruned tree.(The output image consists of two parts: 

    
  1. A color map,@which is an array of color descriptions (RGB triples) for(each color present in the output image. 
  2. A pixel array,:which represents each pixel as an index into the color maparray.


:First, the assignment phase makes one pass over the pruned:color description tree to establish the image's color map.AFor each node with n2 > 0, it divides Sr,&Sg, and Sb by n2.;This produces the mean color of all pixels that classify no<lower than this node. Each of these colors becomes an entryin the color map.

<Finally, the assignment phase reclassifies each pixel in the7pruned tree to identify the deepest node containing the<pixel's color. The pixel's value in the pixel array becomes5the index of this node's mean color in the color map.

>Empirical evidence suggests that the distances in color spaces@such as YUV, or YIQ correspond to perceptual color?differences more closely than do distances in RGB space.5These color spaces may give better results when color6reducing an image. Here the algorithm is as described:except each pixel is a point in the alternate color space.;For convenience, the color components are normalized to theArange 0 to a maximum value, Cmax. The color reduction canthen proceed as described.


(

Measuring Color Reduction Error

8Depending on the image, the color reduction error may be;obvious or invisible. Images with high spatial frequencies6(such as hair or grass) will show error much less than:pictures with large smoothly shaded areas (such as faces).;This is because the high-frequency contour edges introduced5by the color reduction process are masked by the highfrequencies in the image.

8To measure the difference between the original and color1reduced images (the total color reduction error),@ImageMagick sums over all pixels in an image the distanceAsquared in RGB space between each original pixel value and@its color reduced value. ImageMagick prints several error4measurements including the mean error per pixel, the8normalized mean error, and the normalized maximum error.

7The normalized error measurement can be used to compare9images. In general, the closer the mean error is to zero8the more the quantized image resembles the source image.:Ideally, the error should be perceptually-based, since the5human eye is the final judge of quantization quality.

>These errors are measured and printed when -verbose and8-colors are specified on the command line:




mean error per pixel:8
is the mean error for any single pixel in the image.

(

normalized mean square error:8
is the normalized mean square quantization error forany single pixel in the image.

:

This distance measure is normalized to a range between70 and 1. It is independent of the range of red, green,and blue values in the image.

+

normalized maximum square error:;
is the largest normalized square quantization error forany single pixel in the image.

J

This distance measure is normalized to a range between and blue values in the image.





Copyright

3Copyright 1997 E. I. du Pont de Nemours and Company:Permission to use, copy, modify, distribute, and sell this8software and its documentation for any purpose is hereby6granted without fee, provided that the above copyright8notice appear in all copies and that both that copyright6notice and this permission notice appear in supporting4documentation, and that the name of E. I. du Pont de1Nemours and Company not be used in advertising or4publicity pertaining to distribution of the software:without specific, written prior permission. E. I. du Pont9de Nemours and Company makes no representations about the4suitability of this software for any purpose. It is5provided "as is" without express or implied warranty.

2E. I. du Pont de Nemours and Company disclaims all6warranties with regard to this software, including all8implied warranties of merchantability and fitness, in no:event shall E. I. du Pont de Nemours and Company be liable9for any special, indirect or consequential damages or any6damages whatsoever resulting from loss of use, data or8profits, whether in an action of contract, negligence or6other tortious action, arising out of or in connection-with the use or performance of this software.




Acknowledgements

HPaul Raveling, USC Information Sciences Institute, for the6original idea of using space subdivision for the color;reduction algorithm. With Paul's permission, this document*is an adaptation from a document he wrote.

Authors

:John Cristy, H cristy@sympatico.org, E.I. du Pont de Nemours and Company Incorporated.


4[home page]IImage manipulation software that works like magic.