1quantize(5)                   File Formats Manual                  quantize(5)
2
3
4

NAME

6       Quantize - ImageMagick's color reduction algorithm.
7

SYNOPSIS

9       #include <magick.h>
10

DESCRIPTION

12       This document describes how ImageMagick performs color reduction on an
13       image.  To fully understand this document, you should have a knowledge
14       of basic imaging techniques and the tree data structure and terminol‐
15       ogy.
16
17       For purposes of color allocation, an image is a set of n pixels, where
18       each pixel is a point in RGB space.  RGB space is a 3-dimensional vec‐
19       tor space, and each pixel, pi,  is defined by an ordered triple of red,
20       green, and blue coordinates, (ri, gi, bi).
21
22       Each primary color component (red, green, or blue) represents an inten‐
23       sity which varies linearly from 0 to a maximum value, cmax, which cor‐
24       responds to full saturation of that color.  Color allocation is defined
25       over a domain consisting of the cube in RGB space with opposite ver‐
26       tices at (0,0,0) and (cmax,cmax,cmax).  ImageMagick requires cmax =
27       255.
28
29       The algorithm maps this domain onto a tree in which each node repre‐
30       sents a cube within that domain.  In the following discussion, these
31       cubes are defined by the coordinate of two opposite vertices: The ver‐
32       tex nearest the origin in RGB space and the vertex farthest from the
33       origin.
34
35       The tree's root node represents the the entire domain, (0,0,0) through
36       (cmax,cmax,cmax).  Each lower level in the tree is generated by subdi‐
37       viding one node's cube into eight smaller cubes of equal size.  This
38       corresponds to bisecting the parent cube with planes passing through
39       the midpoints of each edge.
40
41       The basic algorithm operates in three phases:  Classification, Reduc‐
42       tion, and Assignment.  Classification builds a color description tree
43       for the image.  Reduction collapses the tree until the number it repre‐
44       sents, at most, is the number of colors desired in the output image.
45       Assignment defines the output image's color map and sets each pixel's
46       color by reclassification in the reduced tree. Our goal is to minimize
47       the numerical discrepancies between the original colors and quantized
48       colors.  To learn more about quantization error, see MEASURING COLOR
49       REDUCTION ERROR later in this document.
50
51       Classification begins by initializing a color description tree of suf‐
52       ficient depth to represent each possible input color in a leaf.  How‐
53       ever, it is impractical to generate a fully-formed color description
54       tree in the classification phase for realistic values of cmax.  If
55       color components in the input image are quantized to k-bit precision,
56       so that cmax = 2k-1, the tree would need k levels below the root node
57       to allow representing each possible input color in a leaf.  This
58       becomes prohibitive because the tree's total number of nodes is
59
60               Σ ki=1 8k
61
62       A complete tree would require 19,173,961 nodes for k = 8, cmax = 255.
63       Therefore, to avoid building a fully populated tree, ImageMagick: (1)
64       Initializes data structures for nodes only as they are needed; (2)
65       Chooses a maximum depth for the tree as a function of the desired num‐
66       ber of colors in the output image (currently log4(colormap size)+2).  A
67       tree of this depth generally allows the best representation of the
68       source image with the fastest computational speed and the least amount
69       of memory.  However, the default depth is inappropriate for some
70       images.  Therefore, the caller can request a specific tree depth.
71
72       For each pixel in the input image, classification scans downward from
73       the root of the color description tree.  At each level of the tree, it
74       identifies the single node which represents a cube in RGB space con‐
75       taining the pixel's color.  It updates the following data for each such
76       node:
77
78       n1:    Number of pixels whose color is contained in the RGB cube which
79              this node represents;
80
81       n2:    Number of pixels whose color is not represented in a node at
82              lower depth in the tree;  initially,  n2 = 0 for all nodes
83              except leaves of the tree.
84
85       Sr, Sg, Sb:
86              Sums of the red, green, and blue component values for all pixels
87              not classified at a lower depth.  The combination of these sums
88              and n2 will ultimately characterize the mean color of a set of
89              pixels represented by this node.
90
91       E:     The distance squared in RGB space between each pixel contained
92              within a node and the nodes' center.  This represents the quan‐
93              tization error for a node.
94
95       Reduction repeatedly prunes the tree until the number of nodes with n2
96       > 0 is less than or equal to the maximum number of colors allowed in
97       the output image.  On any given iteration over the tree, it selects
98       those nodes whose E value is minimal for pruning and merges their color
99       statistics upward.  It uses a pruning threshold, Ep, to govern node
100       selection as follows:
101
102         Ep = 0
103         while number of nodes with (n2 > 0) > required maximum number of col‐
104       ors
105             prune all nodes such that E <= Ep
106             Set Ep  to minimum E in remaining nodes
107
108       This has the effect of minimizing any quantization error when merging
109       two nodes together.
110
111       When a node to be pruned has offspring, the pruning procedure invokes
112       itself recursively in order to prune the tree from the leaves upward.
113       The values of n2  Sr, Sg,  and Sb in a node being pruned are always
114       added to the corresponding data in that node's parent.  This retains
115       the pruned node's color characteristics for later averaging.
116
117       For each node,  n2 pixels exist for which that node represents the
118       smallest volume in RGB space containing those pixel's colors.  When n2
119       > 0 the node will uniquely define a color in the output image.  At the
120       beginning of reduction, n2 = 0  for all nodes except the leaves of the
121       tree which represent colors present in the input image.
122
123       The other pixel count, n1,  indicates the total number of colors within
124       the cubic volume which the node represents.  This includes n1 - n2 pix‐
125       els whose colors should be defined by nodes at a lower level in the
126       tree.
127
128       Assignment generates the output image from the pruned tree.  The output
129       image consists of two parts:  (1)  A color map, which is an array of
130       color descriptions (RGB triples) for each color present in the output
131       image; (2)  A pixel array, which represents each pixel as an index into
132       the color map array.
133
134       First, the assignment phase makes one pass over the pruned color
135       description tree to establish the image's color map.  For each node
136       with n2 > 0, it divides Sr, Sg, and Sb by n2.  This produces the mean
137       color of all pixels that classify no lower than this node.  Each of
138       these colors becomes an entry in the color map.
139
140       Finally, the assignment phase reclassifies each pixel in the pruned
141       tree to identify the deepest node containing the pixel's color.  The
142       pixel's value in the pixel array becomes the index of this node's mean
143       color in the color map.
144
145       Empirical evidence suggests that distances in color spaces such as YUV,
146       or YIQ correspond to perceptual color differences more closely than do
147       distances in RGB space.  These color spaces may give better results
148       when color reducing an image.  Here the algorithm is as described
149       except each pixel is a point in the alternate color space.  For conve‐
150       nience, the color components are normalized to the range 0 to a maximum
151       value, cmax.  The color reduction can then proceed as described.
152

MEASURING COLOR REDUCTION ERROR

154       Depending on the image, the color reduction error may be obvious or
155       invisible.  Images with high spatial frequencies (such as hair or
156       grass) will show error much less than pictures with large smoothly
157       shaded areas (such as faces).  This is because the high-frequency con‐
158       tour edges introduced by the color reduction process are masked by the
159       high frequencies in the image.
160
161       To measure the difference between the original and color reduced images
162       (the total color reduction error), ImageMagick sums over all pixels in
163       an image the distance squared in RGB space between each original pixel
164       value and its color reduced value. ImageMagick prints several error
165       measurements including the mean error per pixel, the normalized mean
166       error, and the normalized maximum error.
167
168       The normalized error measurement can be used to compare images.  In
169       general, the closer the mean error is to zero the more the quantized
170       image resembles the source image.  Ideally, the error should be percep‐
171       tually-based, since the human eye is the final judge of quantization
172       quality.
173
174       These errors are measured and printed when -verbose and -colors are
175       specified on the command line:
176
177       mean error per pixel:
178              is the mean error for any single pixel in the image.
179
180       normalized mean square error:
181              is the normalized mean square quantization error for any single
182              pixel in the image.
183
184              This distance measure is normalized to a range between 0 and 1.
185              It is independent of the range of red, green, and blue values in
186              the image.
187
188       normalized maximum square error:
189              is the largest normalized square quantization error for any sin‐
190              gle pixel in the image.
191
192              This distance measure is normalized to a range between 0 and 1.
193              It is independent of the range of red, green, and blue values in
194              the image.
195

SEE ALSO

197       display(1), animate(1), mogrify(1), import(1), miff(5)
198
200       Copyright (C) 2002 ImageMagick Studio, a non-profit organization dedi‐
201       cated to making software imaging solutions freely available.
202
203       Permission is hereby granted, free of charge, to any person obtaining a
204       copy of this software and associated documentation files ("ImageMag‐
205       ick"), to deal in ImageMagick without restriction, including without
206       limitation the rights to use, copy, modify, merge, publish, distribute,
207       sublicense, and/or sell copies of ImageMagick, and to permit persons to
208       whom the ImageMagick is furnished to do so, subject to the following
209       conditions:
210
211       The above copyright notice and this permission notice shall be included
212       in all copies or substantial portions of ImageMagick.
213
214       The software is provided "as is", without warranty of any kind, express
215       or implied, including but not limited to the warranties of mer‐
216       chantability, fitness for a particular purpose and noninfringement.  In
217       no event shall ImageMagick Studio be liable for any claim, damages or
218       other liability, whether in an action of contract, tort or otherwise,
219       arising from, out of or in connection with ImageMagick or the use or
220       other dealings in ImageMagick.
221
222       Except as contained in this notice, the name of the ImageMagick Studio
223       shall not be used in advertising or otherwise to promote the sale, use
224       or other dealings in ImageMagick without prior written authorization
225       from the ImageMagick Studio.
226

ACKNOWLEDGEMENTS

228       Paul Raveling, USC Information Sciences Institute, for the original
229       idea of using space subdivision for the color reduction algorithm.
230       With Paul's permission, this document is an adaptation from a document
231       he wrote.
232

AUTHORS

234       John Cristy, ImageMagick Studio
235
236
237
238ImageMagick              $Date: 2001/12/07 18:43:53 $              quantize(5)
Impressum