Tryag File Manager
Home
-
Turbo Force
Current Path :
/
proc
/
self
/
root
/
usr
/
share
/
doc
/
ImageMagick-6.2.8
/
www
/
Upload File :
New :
File
Dir
//proc/self/root/usr/share/doc/ImageMagick-6.2.8/www/quantize.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <style type="text/css"><!-- @import url("../www/magick.css"); //--></style> <title>ImageMagick: Color Reduction Algorithm</title> <meta http-equiv="Content-Language" content="en-US"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Reply-to" content="magick-users@imagemagick.org"/> <meta name="Generator" content="PHP"/> <meta name="Keywords" content="color, reduction, algorithm, ImageMagick, PerlMagick, Magick++, Image, Magick"/> <meta name="Description" content="ImageMagick (TM) is a free software suite to create, edit and compose bitmap images. It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and Bézier curves can be added to images and stretched and rotated. ImageMagick is free software: it is delivered with full source code and can be freely used, copied, modified and distributed. Its license is compatible with the GPL. It runs on all major operating systems. Most of the functionality of ImageMagick can be used interactively from the command line; more often, however, the features are used from programs written in the programming languages Perl, C, C++, Python, PHP, Ruby or Java, for which ready-made ImageMagick interfaces (PerlMagick, Magick++, PythonMagick, MagickWand for PHP, RubyMagick, and JMagick) are available. This makes it possible to modify or create images automatically and dynamically. ImageMagick supports many image formats (over 90 major formats) including popular formats like TIFF, JPEG, PNG, PDF, PhotoCD, and GIF."/> <meta name="Rating" content="GENERAL"/> <meta name="Robots" content="INDEX, FOLLOW"/> <meta name="Generator" content="ImageMagick Studio LLC"/> <meta name="Author" content="ImageMagick Studio LLC"/> <meta name="Revisit-after" content="2 DAYS"/> <meta name="Resource-type" content="document"/> <meta name="Copyright" content="Copyright (c) 1999-2006 ImageMagick Studio LLC"/> <meta name="Distribution" content="Global"/> <link rel="shortcut icon" href="/../images/wand.ico" type="image/x-icon"/> </head> <body id="www-imagemagick-org"> <table id="titlebar" style="width: 100%;background-color: #f5f5f5" cellpadding="0" cellspacing="0" border="0" summary="ImageMagick"> <tbody> <tr valign="top"> <td align="left"><a href="../index.html"><img id="titlebar-west" src="../images/script.png" alt="[ImageMagick]" width="350" height="60" border="0" vspace="28" name="titlebar-west" /></a></td> <td width="35%"><br /></td> <td align="left"><a href="http://www.imagemagick.org/" target="1743319505"><img id="titlebar-west" src="../images/sponsor.jpg" alt="[sponsor]" border="0" vspace="28" name="titlebar-west" /></a></td> <td width="65%"><br /></td> <td style="background-color: white" align="right"><a href="../index.html"><img src="../images/sprite.jpg" alt="" width="114" height="118" border="0" name="titlebar-east" /></a></td> <td style="background-color: white" align="right"><a href="../index.html"><img id="titlebar-east" src="../images/logo.jpg" alt="" width="114" height="118" border="0" name="titlebar-east" /></a></td> </tr> </tbody> </table> <table style="width: 100%" border="0" cellpadding="0" cellspacing="0" summary=""> <tbody> <tr valign="top" style="height: 100%;"> <td id="menu" width="1%" height="100%"> <p><a href="#main">Skip to page contents</a></p> <span>[</span> <a href="../index.html">About ImageMagick</a> <a href="../www/command-line-tools.html">Command-line Tools</a> <a href="../www/command-line-processing.html" class="sub">Processing</a> <a href="../www/command-line-options.html" class="sub">Options</a> <a href="../www/api.html">Program Interfaces</a> <a href="../www/magick-wand.html" class="sub">MagickWand</a> <a href="../www/magick-core.html" class="sub">MagickCore</a> <a href="../www/perl-magick.html" class="sub">PerlMagick</a> <a href="../www/architecture.html">Architecture</a> <span>]</span><br /><span>[</span> <a href="../www/install-source.html">Install from Source</a> <a href="../www/install-source.html#unix" class="sub">Unix</a> <a href="../www/install-source.html#windows" class="sub">Windows</a> <a href="../www/binary-releases.html">Binary Releases</a> <a href="../www/binary-releases.html#unix" class="sub">Unix</a> <a href="../www/binary-releases.html#windows" class="sub">Windows</a> <a href="../www/resources.html">Resources</a> <span>]</span><br /><span>[</span> <a href="../www/download.html">Download</a> <span>]</span><br /><span>[</span> <a href="../www/links.html">Links</a> <span>]</span><br /><br /><span>[</span> <a href="../www/sponsors.html">Sponsors</a> <a href="http://www.webservertalk.com" class="sponsor" target="sponsor">Server Administration<br />Support Forum</a><!-- 2007051536 pdt --> <span>]</span> </td> <td id="main" valign="top"> <p class="navigation-index">[<a href="#describe">Algorithm Description</a> | <a href="#measure">Measuring Color Reduction Error</a>]</p> <p>This document describes how ImageMagick 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> <div style="margin: auto;"> <h2><a name="describe"></a>Algorithm Description</h2> </div> <p>For purposes of color allocation, an image is a set of <em>n</em> pixels, where each pixel is a point in RGB space. RGB space is a 3-dimensional vector space, and each pixel, <em>p(i)</em>, is defined by an ordered triple of red, green, and blue coordinates, (<em>r(i)</em>, <em>g(i)</em>, <em>b(i)</em>).</p> <p>Each primary color component (<em>red</em>, <em>green</em>, or <em>blue</em>) represents an intensity which varies linearly from 0 to a maximum value, <em>Cmax</em>, which corresponds to full saturation of that color. Color allocation is defined over a domain consisting of the cube in RGB space with opposite vertices at (0, 0, 0) and (<em>Cmax</em>, <em>Cmax</em>, <em>Cmax</em>). ImageMagick requires <em>Cmax</em>= <em>255</em>.</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 RGB space and the vertex farthest from the origin.</p> <p>The tree's root node represents the the entire domain, (0,0,0) through (<em>Cmax</em>, <em>Cmax</em>, <em>Cmax</em>). 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> <ol> <li>Classification</li> <li>Reduction</li> <li>Assignment</li> </ol> <p><b>Classification</b><p> <p>Classification builds a color description tree for the image. Reduction collapses the tree until the number it represents, at most, is 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. <em>Our goal is to minimize the numerical discrepancies between the original colors and quantized colors</em>. To learn more about quantization error, see <a href="#measure">Measuring Color Reduction Error</a>.</p> <p>Classification 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 <em>Cmax</em>. If color components in the input image are quantized to <em>k</em>-bit precision, so that <em>Cmax</em> = <em>2^k-1</em>, the tree would need <em>k</em> levels below the root node to allow representing each possible input color in a leaf. This becomes prohibitive because the tree's total number of nodes:</p> <pre class="text"> total nodes = 1+Sum(8^i), i=1,k For k=8, nodes = 1 + (8^1+8^2+....+8^8) 8^8 - 1 = 1 + 8.----------- 8 - 1 = 19,173,961 </pre> <p>Therefore, to avoid building a fully populated tree, ImageMagick:</p> <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 (currently <em>based-two</em> logarithm of <em>Cmax</em>).</li> </ol> <pre class="text"> For Cmax=255, Maximum tree depth = log (256) 2 = log (256) / log (2) e e = 8 </pre> <p>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> <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 RGB space containing the pixels' color. It updates the following data for each such node:</p> <ul> <dt>n1</dt> <dd>number of pixels whose color is contained in the RGB cube which this node represents;</dd><br /> <dt>n2</dt> <dd>number of pixels whose color is not represented in a node at lower depth in the tree; initially, <em>n2=0</em> for all nodes except leaves of the tree.</dd><br /> <dt>Sr,Sg,Sb</dt> <dd>sums of the <em>red</em>, <em>green</em>, and <em>blue</em> component values for all pixels not classified at a lower depth. The combination of these sums and <em>n2</em> will ultimately characterize the mean color of a set of pixels represented by this node.</dd><br /> <dt>E</dt> <dd>the distance squared in RGB space between each pixel contained within a node and the nodes' center. This represents the quantization error for a node.</dd> </ul> <p><b>Reduction</b><p> <p>Reduction repeatedly prunes the tree until the number of nodes with <em>n2</em> > <em>0</em> 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 <em>E</em> value is minimal for pruning and merges their color statistics upward. It uses a pruning threshold, <em>Ep</em>, to govern node selection as follows:</p> <pre class="text"> 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> <p>This has the effect of minimizing any quantization error when merging two nodes together.</p> <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 <em>n2</em>, <em>Sr</em>, <em>Sg</em>, and <em>Sb</em> 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, <em>n2</em> pixels exist for which that node represents the smallest volume in RGB space containing those pixel's colors. When <em>n2</em> > <em>0</em> the node will uniquely define a color in the output image. At the beginning of reduction, <em>n2</em> = <em>0</em> for all nodes except the leaves of the tree which represent colors present in the input image.</p> <p>The other pixel count, <em>n1</em>, indicates the total number of colors within the cubic volume which the node represents. This includes <em>n1</em> - <em>n2</em> pixels whose colors should be defined by nodes at a lower level in the tree.</p> <p><b>Assignment</b><p> <p>Assignment 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 (RGB 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 <em>n2</em> > <em>0</em>, it divides <em>Sr</em>, <em>Sg</em>, and <em>Sb</em> by <em>n2</em>. 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 YUV, or YIQ correspond to perceptual color differences more closely than do distances in RGB 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, <em>Cmax</em>. The color reduction can then proceed as described.</p> <div style="margin: auto;"> <h2><a name="measure"></a>Measuring Color Reduction Error</h2> </div> <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> <p>To measure the difference between the original and color reduced images (the total color reduction error), ImageMagick sums over all pixels in an image the distance squared in RGB space between each original pixel value and its color reduced value. ImageMagick 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 the <a href="../www/command-line-options.html#colors">-colors</a> and <a href="../www/command-line-options.html#verbose">-verbose</a> options are specified on the <a href="../www/convert.html">convert</a> command line:</p> <ul> <dt>mean error per pixel</dt> <dd>is the mean error for any single pixel in the image.</dd><br /> <dt>normalized mean square error</dt> <dd>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.</dd><br /> <dt>normalized maximum square error</dt> <dd>is the largest normalized square quantization error for any single pixel in the image. This distance measure is normalized to a range between of red, green, and blue values in the image.</dd> </ul> </td> <td id="margin" width="1%" height="100%" valign="top" align="right"> </td> </tr> </tbody> </table> <div id="linkbar"> <a href="http://redux.imagemagick.org/discussion-server" target="1116416235">Discourse Server</a> | <a href="../www/mailing-list.html">Mailing Lists</a> | <a href="http://redux.imagemagick.org/gallery" target="1762610744">Image Gallery</a> | <a href="http://studio.webbyland.com/MagickStudio/scripts/MagickStudio.cgi" target="951754344">ImageMagick Studio</a> </div> <div> <span id="footer-west">© 1999-2006 ImageMagick Studio LLC</span> </div> <div style="clear: both; margin: 0; width: 100%; "></div> </body> </html>