1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title>ImageMagick - Reduce the Number of Unique Colors in an
Image</title>
<meta name="Description"
content="ImageMagick is a robust collection of tools and libraries to read, write, and manipulate an image in many image formats including popular formats like TIFF, JPEG, PNG, PDF, PhotoCD, and GIF. With ImageMagick you can create images dynamically, making it suitable for Web applications. You can also resize, rotate, sharpen, color reduce, or add special effects to an image or image sequence and save your completed work in the same or differing image format. Image processing operations are available from the command line, as well as through C, C++, Perl, or Java programming interfaces." />
<meta name="Keywords"
content="ImageMagick,PerlMagick,Magick++,Image,Magick,Magic,Pixel,Graphics,Perl,Wand" />
<meta name="Resource-type" content="document" />
<meta name="Robots" content="ALL" />
<link rel="stylesheet" type="text/css" href="../www/magick.css" />
</head>
<body marginheight="1" marginwidth="1" topmargin="1"
leftmargin="1">
<a name="top" id="top"></a>
<table border="0" cellpadding="0" cellspacing="0" summary="Masthead" width="100%">
<tbody>
<tr>
<td bgcolor="#003399" width="25%" height="118" background="../images/background.gif"><a href="http://www.imagemagick.org/"><img src="../images/script.gif" width="278" height="118" border="0" alt="" /></a></td>
<td bgcolor="#003399" width="60%" height="118" background="../images/background.gif"><a href="http://www.networkeleven.com/direct.php?magick_all"><img src="../images/promote.png" border="0" width="186" height="52" vspace="29" alt="Powered by NetworkEleven" /></a></td>
<td bgcolor="#003399" width="114" height="118" align="right"><img src="../images/sprite.png" width="114" height="118" alt="" /></td>
<td bgcolor="#003399" width="114" height="118" align="right"><a href="http://www.imagemagick.net"><img src="../images/logo.png" width="114" height="118" border="0" alt="ImageMagick logo" /></a></td>
</tr></tbody></table>
<table align="left" border="0" cellpadding="2" cellspacing="2"
summary="Navigation buttons" width="20%">
<tr>
<td>
<form target="_self" action="../index.html"><input type="submit"
title="ImageMagick Home" value=" Home"
style="background-color: #1947A3; background-image:url('../images/background.gif'); color:#fbc713; font-weight:bold" />
</form></td>
<td>
<form target="_self" action="../www/apis.html"><input type="submit"
title="ImageMagick API " value=" API "
style="background-color: #1947A3; background-image:url('../images/background.gif'); color:#fbc713; font-weight:bold" />
</form></td>
<td>
<form target="_self" action="../www/download.html">
<input type="submit" title="ImageMagick Download" value="Download"
style="background-color: #1947A3; background-image:url('../images/background.gif'); color:#fbc713; font-weight:bold" />
</form></td></tr></table>
<div align="right" style="margin-top:3px; padding-right:4px">
<form action="http://studio.imagemagick.org/Sage/scripts/Sage.cgi">
<input type="TEXT" name="query" size="32" maxlength="255" />
<input type="SUBMIT" name="sa" value="Search"
style="background-color: #1947A3; background-image:url('../images/background.gif'); bgcolor:#003399; color:#fbc713; font-weight:bold" />
</form></div>
<table align="left" border="0" cellpadding="10" cellspacing="0"
style="margin-top:-17px" width="100%">
<tr>
<br />
<td>
<p>This document describes how
<a href="../index.html">ImageMagick</a> performs 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.</p>
<table border="0" width="100%">
<tr>
<td align="left" bgcolor="#003399"
background="../images/background.gif">
<img src="../images/right_triangle.png" alt=">" border="0"
height="14"
width="15" /><b><font face="Helvetica, Arial"><font color="#FBC713">
<font size="+1">Description</font></font></font></b></td></tr></table>
<p>For purposes of color allocation, an image is a set of <i>n</i>
pixels, where each pixel is a point in <b>RGB</b> space. <b>RGB</b>
space is a 3-dimensional vector space, and each pixel, <i>p(i)</i>,
is defined by an ordered triple of red, green, and blue
coordinates, (<i>r(i)</i>,<i>g(i)</i>,<i>b(i)</i>).</p></dd>
<dd>
<p>Each primary color component (<i>red</i>, <i>green</i>, or
<i>blue</i>) represents an intensity which varies linearly from 0
to a maximum value, <i>Cmax</i>, which corresponds to full
saturation of that color. Color allocation is defined over a domain
consisting of the cube in <b>RGB</b> space with opposite vertices
at (0,0,0) and (<i>Cmax</i>,<i>Cmax</i>,<i>Cmax</i>).
<b>ImageMagick</b> requires <i>Cmax</i>= <i>255</i>.</p>
<p>The 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 <b>RGB</b> space and the vertex
farthest from the origin.</p>
<p>The tree's root node represents the the entire domain, (0,0,0)
through (<i>Cmax</i>,<i>Cmax</i>,<i>Cmax</i>). 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.</p>
<p>The basic algorithm operates in three phases:</p>
<ul>
<li><b>Classification,</b></li>
<li><b>Reduction</b>, and</li>
<li><b>Assignment</b>.</li></ul>
<p><b>Classification</b> builds a color description tree for the
image. <b>Reduction</b> collapses the tree until the number it
represents, at most, is the number of colors desired in the output
image. <b>Assignment</b> defines the output image's color map and
sets each pixel's color by reclassification in the reduced tree.
<i>Our goal is to minimize the numerical discrepancies between the
original colors and quantized colors</i>. To learn more about
quantization error, see <b>Measuring Color Reduction Error</b>
later in this document.</p>
<p><b>Classification</b> begins by initializing a color description
tree of sufficient depth to represent each possible input color in
a leaf. However, it is impractical to generate a fully-formed color
description tree in the classification phase for realistic values
of <i>Cmax</i>. If color components in the input image are
quantized to <i>k</i>-bit precision, so that <i>Cmax</i> =
<i>2^k-1</i>, the tree would need <i>k</i> levels below the root
node to allow representing each possible input color in a leaf.
This becomes <b>prohibitive</b> because the tree's:</p>
<pre>
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
</pre>
Therefore, to avoid building a fully populated tree,
<b>ImageMagick</b>:
<ol>
<li>Initializes data structures for nodes only as they are
needed;</li>
<li>Chooses a maximum depth for the tree as a function of the
desired number of colors in the output image (<b>currently
<i>based-two</i> logarithm of <i>Cmax</i></b>).</li></ol>
<pre>
For Cmax=255,
Maximum tree depth = log (256)
2
= log (256) / log (2)
e e
= 8
</pre>
A tree of this depth generally allows the best representation of
the source image with the fastest computational speed and the least
amount of memory. However, the default depth is inappropriate for
some images. Therefore, the caller can request a specific tree
depth.
<p>For each pixel in the input image, classification scans downward
from the root of the color description tree. At each level of the
tree, it identifies the single node which represents a cube in
<b>RGB</b> space containing the pixel's color. It updates the
following data for each such node:</p>
<dl>
<dt><b>n1:</b></dt>
<dt>Number of pixels whose color is contained in the <b>RGB</b>
cube which this node represents;</dt></dl></dd>
<dt><b>n2:</b></dt>
<dt>Number of pixels whose color is not represented in a node at
lower depth in the tree; initially, <b>n2=0</b> for all nodes
except leaves of the tree.</dt>
<dt><b>Sr,Sg,Sb:</b></dt>
<dt>Sums of the <i>red</i>, <i>green</i>, and <i>blue</i> component
values for all pixels not classified at a lower depth. The
combination of these sums and <i>n2</i> will ultimately
characterize the mean color of a set of pixels represented by this
node.</dt>
<dt><b>E:</b></dt>
<dt>The distance squared in <b>RGB</b> space between each pixel
contained within a node and the nodes' center. This represents the
quantization error for a node.</dt></dl>
<b>Reduction</b> repeatedly prunes the tree until the number of
nodes with <i>n2</i> > <i>0</i> is less than or equal to the
maximum number of colors allowed in the output image. On any given
iteration over the tree, it selects those nodes whose <i>E</i>
value is minimal for pruning and merges their color statistics
upward. It uses a pruning threshold, <i>Ep</i>, to govern node
selection as follows:
<pre>
Ep = 0
while number of nodes with (n2 > 0) > required maximum number of colors
prune all nodes such that E <= Ep
Set Ep to minimum E in remaining nodes
</pre>
This has the effect of minimizing any quantization error when
merging two nodes together.
<p>When a node to be pruned has offspring, the pruning procedure
invokes itself recursively in order to prune the tree from the
leaves upward. The values of <i>n2</i>,<i>Sr</i>, <i>Sg</i> and
<i>Sb</i> in a node being pruned are always added to the
corresponding data in that node's parent. This retains the pruned
node's color characteristics for later averaging.</p>
<p>For each node, <i>n2</i> pixels exist for which that node
represents the smallest volume in <b>RGB</b> space containing those
pixel's colors. When <i>n2</i> > <i>0</i> the node will uniquely
define a color in the output image. At the beginning of reduction,
<i>n2</i> = <i>0</i> for all nodes except the leaves of the tree
which represent colors present in the input image.</p>
<p>The other pixel count, <i>n1</i>, indicates the total number of
colors within the cubic volume which the node represents. This
includes <i>n1</i> - <i>n2</i> pixels whose colors should be
defined by nodes at a lower level in the tree.</p>
<p><b>Assignment</b> generates the output image from the pruned
tree. The output image consists of two parts:</p>
<ol>
<li>A color map, which is an array of color descriptions
(<b>RGB</b> triples) for each color present in the output
image.</li>
<li>A pixel array, which represents each pixel as an index into the
color map array.</li></ol>
<p>First, the assignment phase makes one pass over the pruned color
description tree to establish the image's color map. For each node
with <i>n2</i> > <i>0</i>, it divides <i>Sr</i>, <i>Sg</i>, and
<i>Sb</i> by <i>n2</i>. This produces the mean color of all pixels
that classify no lower than this node. Each of these colors becomes
an entry in the color map.</p>
<p>Finally, the assignment phase reclassifies each pixel in the
pruned tree to identify the deepest node containing the pixel's
color. The pixel's value in the pixel array becomes the index of
this node's mean color in the color map.</p>
<p>Empirical evidence suggests that the distances in color spaces
such as <b>YUV</b>, or <b>YIQ</b> correspond to perceptual color
differences more closely than do distances in <b>RGB</b> space.
These color spaces may give better results when color reducing 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 the range 0 to a maximum value,
<i>Cmax</i>. The color reduction can then proceed as described.</p>
<table border="0" width="100%">
<tr>
<td align="left" bgcolor="#003399"
background="../images/background.gif">
<img src="../images/right_triangle.png" alt=">" border="0"
height="14"
width="15" /><b><font face="Helvetica, Arial"><font color="#FBC713">
<font size="+1">Measuring Color Reduction
Error</font></font></font></b></td></tr></table>
<p>Depending on the image, the color reduction error may be obvious
or invisible. Images with high spatial frequencies (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 introduced by the color reduction process are masked
by the high frequencies in the image.</p></dd>
<dd>
<p>To measure the difference between the original and color reduced
images (the total color reduction error), <b>ImageMagick</b> sums
over all pixels in an image the distance squared in <b>RGB</b>
space between each original pixel value and its color reduced
value. <b>ImageMagick</b> prints several error measurements
including the mean error per pixel, the normalized mean error, and
the normalized maximum error.</p>
<p>The normalized error measurement can be used to compare images.
In general, the closer the mean error is to zero the more the
quantized image resembles the source image. Ideally, the error
should be perceptually-based, since the human eye is the final
judge of quantization quality.</p>
<p>These errors are measured and printed when <b>-verbose</b> and
<b>-colors</b><i>are specified on the command line</i>:</p>
<dl>
<dt><b>mean error per pixel:</b></dt>
<dt>is the mean error for any single pixel in the
image.</dt></dl></dd>
<dt><b>normalized mean square error:</b></dt>
<dt>is the normalized mean square quantization error for any single
pixel in the image. This distance measure is normalized to a range
between 0 and 1. It is independent of the range of red, green, and
blue values in the image.</dt>
<dt><b>normalized maximum square error:</b></dt>
<dt>is the largest normalized square quantization error for any
single pixel in the image. This distance measure is normalized to a
range between and blue values in the image.</dt></dl>
<table border="0" width="100%">
<tr>
<td align="left" bgcolor="#003399"
background="../images/background.gif">
<img src="../images/right_triangle.png" alt=">" border="0"
height="14"
width="15" /><b><font face="Helvetica, Arial"><font color="#FBC713">
<font size="+1">Authors</font></font></font></b></td></tr></table>
<p><i>John Cristy</i>,
<a href="mailto:magick-users@imagemagick.org"><i>magick-users@imagemagick.org</i>,</a><b>
, ImageMagick Studio, LLC</b>.</p></dd></dl>
<table border="0" width="100%">
<tr>
<td align="left" bgcolor="#003399"
background="../images/background.gif">
<img src="../images/right_triangle.png" alt=">" border="0"
height="14"
width="15" /><b><font face="Helvetica, Arial"><font color="#FBC713">
<font size="+1">Acknowledgements</font></font></font></b></td></tr></table>
<p><i>Paul Raveling</i>, <b>USC Information Sciences Institute</b>,
for the original idea of using space subdivision for the color
reduction algorithm. With Paul's permission, this document is an
adaptation from a document he wrote.</p></dd></dl>
<table border="0" width="100%">
<tr>
<td align="left" bgcolor="#003399"
background="../images/background.gif">
<img src="../images/right_triangle.png" alt=">" border="0"
height="14"
width="15" /><b><font face="Helvetica, Arial"><font color="#FBC713">
<font size="+1">Copyright</font></font></font></b></td></tr></table>
<p><strong>Copyright (C) 1999-2004 ImageMagick Studio LLC.
Additional copyrights and licenses apply to this software, see
http://www.imagemagick.org/www/Copyright.html</strong></p>
<hr />
<a href="#top"><img src="../images/top.gif" border="0" width="42"
height="42" align="right" alt="Top of page" /></a>
<form action="http://studio.imagemagick.org/magick/"
style="margin-top:5px"><input type="submit" title="Help!"
value="Help!"
style="background-color: #1947A3; background-image:url('../images/background.gif'); color:#fbc713; font-weight:bold" />
<small>"Image manipulation software that works like
magick"</small></form></dd></dl></td></tr></table>
</body>
</html>
|