github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/graphics-magick/share/doc/GraphicsMagick/www/quantize.html (about) 1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 <meta name="generator" content="Docutils 0.15.2: http://docutils.sourceforge.net/" /> 7 <title>GraphicsMagick Color Quantization</title> 8 <link rel="stylesheet" href="docutils-articles.css" type="text/css" /> 9 </head> 10 <body> 11 12 <div class="banner"> 13 <img src="images/gm-107x76.png" alt="GraphicMagick logo" width="107" height="76" /> 14 <span class="title">GraphicsMagick</span> 15 <form action="http://www.google.com/search"> 16 <input type="hidden" name="domains" value="www.graphicsmagick.org" /> 17 <input type="hidden" name="sitesearch" value="www.graphicsmagick.org" /> 18 <span class="nowrap"><input type="text" name="q" size="25" maxlength="255" /> <input type="submit" name="sa" value="Search" /></span> 19 </form> 20 </div> 21 22 <div class="navmenu"> 23 <ul> 24 <li><a href="index.html">Home</a></li> 25 <li><a href="project.html">Project</a></li> 26 <li><a href="download.html">Download</a></li> 27 <li><a href="README.html">Install</a></li> 28 <li><a href="Hg.html">Source</a></li> 29 <li><a href="NEWS.html">News</a> </li> 30 <li><a href="utilities.html">Utilities</a></li> 31 <li><a href="programming.html">Programming</a></li> 32 <li><a href="reference.html">Reference</a></li> 33 </ul> 34 </div> 35 <div class="document" id="graphicsmagick-color-quantization"> 36 <h1 class="title">GraphicsMagick Color Quantization</h1> 37 38 <!-- -*- mode: rst -*- --> 39 <!-- This text is in reStucturedText format, so it may look a bit odd. --> 40 <!-- See http://docutils.sourceforge.net/rst.html for details. --> 41 <blockquote> 42 This document describes how GraphicsMagick performs color 43 reduction on an image. To fully understand this document, 44 you should have a knowledge of basic imaging techniques and 45 the tree data structure and terminology.</blockquote> 46 <div class="section" id="description"> 47 <h1>Description</h1> 48 <p>For purposes of color allocation, an image is a set of n pixels, where 49 each pixel is a point in RGB space. RGB space is a 3-dimensional vector 50 space, and each pixel, p(i), is defined by an ordered triple of red, 51 green, and blue coordinates, (r(i),g(i),b(i)).</p> 52 <p>Each primary color component (red, green, or blue) represents an 53 intensity which varies linearly from 0 to a maximum value, Cmax, which 54 corresponds to full saturation of that color. Color allocation is defined 55 over a domain consisting of the cube in RGB space with opposite vertices 56 at (0,0,0) and (Cmax ,Cmax,Cmax). GraphicsMagick requires Cmax= 255.</p> 57 <p>The algorithm maps this domain onto a tree in which each node represents 58 a cube within that domain. In the following discussion, these cubes are 59 defined by the coordinate of two opposite vertices: The vertex nearest 60 the origin in RGB space and the vertex farthest from the origin.</p> 61 <p>The tree's root node represents the the entire domain, (0,0,0) through 62 (Cmax, Cmax,Cmax). Each lower level in the tree is generated by 63 subdividing one node's cube into eight smaller cubes of equal size. This 64 corresponds to bisecting the parent cube with planes passing through the 65 midpoints of each edge.</p> 66 <p>The basic algorithm operates in three phases:</p> 67 <blockquote> 68 <ul class="simple"> 69 <li>Classification,</li> 70 <li>Reduction, and</li> 71 <li>Assignment.</li> 72 </ul> 73 </blockquote> 74 <p>Classification builds a color description tree for the image. Reduction 75 collapses the tree until the number it represents, at most, is the number 76 of colors desired in the output image. Assignment defines the output 77 image's color map and sets each pixel's color by reclassification in the 78 reduced tree. Our goal is to minimize the numerical discrepancies between 79 the original colors and quantized colors. To learn more about 80 quantization error, see Measuring Color Reduction Error later in this 81 document.</p> 82 <p>Classification begins by initializing a color description tree of 83 sufficient depth to represent each possible input color in a leaf. 84 However, it is impractical to generate a fully-formed color description 85 tree in the classification phase for realistic values of Cmax. If color 86 components in the input image are quantized to k-bit precision, so that 87 Cmax = 2^k-1, the tree would need k levels below the root node to allow 88 representing each possible input color in a leaf. This becomes 89 prohibitive because the tree's:</p> 90 <pre class="literal-block"> 91 total number of nodes = 1+Sum(8^i), i=1,k 92 93 For k=8, 94 Number of nodes= 1 + (8^1+8^2+....+8^8) 95 8^8 - 1 96 = 1 + 8.----------- 97 8 - 1 98 = 19,173,961 99 </pre> 100 <p>Therefore, to avoid building a fully populated tree, GraphicsMagick:</p> 101 <ol class="arabic simple"> 102 <li>Initializes data structures for nodes only as they are needed;</li> 103 <li>Chooses a maximum depth for the tree as a function of the desired 104 number of colors in the output image (currently based-two logarithm 105 of Cmax).</li> 106 </ol> 107 <p>For Cmax=255,</p> 108 <pre class="literal-block"> 109 Maximum tree depth = log (256) 110 2 111 112 = log (256) / log (2) 113 e e 114 115 = 8 116 </pre> 117 <p>A tree of this depth generally allows the best representation of the 118 source image with the fastest computational speed and the least amount of 119 memory. However, the default depth is inappropriate for some images. 120 Therefore, the caller can request a specific tree depth.</p> 121 <p>For each pixel in the input image, classification scans downward from the 122 root of the color description tree. At each level of the tree, it 123 identifies the single node which represents a cube in RGB space 124 containing the pixel's color. It updates the following data for each such 125 node:</p> 126 <blockquote> 127 <dl class="docutils"> 128 <dt>n1:</dt> 129 <dd>Number of pixels whose color is contained in the RGB cube which 130 this node represents;</dd> 131 <dt>n2:</dt> 132 <dd>Number of pixels whose color is not represented in a node at lower 133 depth in the tree; initially, n2=0 for all nodes except leaves of 134 the tree.</dd> 135 <dt>Sr,Sg,Sb:</dt> 136 <dd>Sums of the red, green, and blue component values for all pixels 137 not classified at a lower depth. The combination of these sums and 138 n2 will ultimately characterize the mean color of a set of pixels 139 represented by this node.</dd> 140 <dt>E:</dt> 141 <dd>The distance squared in RGB space between each pixel contained 142 within a node and the nodes' center. This represents the 143 quantization error for a node.</dd> 144 </dl> 145 </blockquote> 146 <p>Reduction repeatedly prunes the tree until the number of nodes with n2 > 147 0 is less than or equal to the maximum number of colors allowed in the 148 output image. On any given iteration over the tree, it selects those 149 nodes whose E value is minimal for pruning and merges their color 150 statistics upward. It uses a pruning threshold, Ep, to govern node 151 selection as follows:</p> 152 <pre class="literal-block"> 153 Ep = 0 154 while number of nodes with (n2 > 0) > required maximum number of colors 155 prune all nodes such that E <= Ep 156 Set Ep to minimum E in remaining nodes 157 </pre> 158 <p>This has the effect of minimizing any quantization error when merging two 159 nodes together.</p> 160 <p>When a node to be pruned has offspring, the pruning procedure invokes 161 itself recursively in order to prune the tree from the leaves upward. The 162 values of n2 ,Sr, Sg and Sb in a node being pruned are always added to 163 the corresponding data in that node's parent. This retains the pruned 164 node's color characteristics for later averaging.</p> 165 <p>For each node, n2 pixels exist for which that node represents the 166 smallest volume in RGB space containing those pixel's colors. When n2 > 0 167 the node will uniquely define a color in the output image. At the 168 beginning of reduction, n2 = 0 for all nodes except the leaves of the 169 tree which represent colors present in the input image.</p> 170 <p>The other pixel count, n1, indicates the total number of colors within 171 the cubic volume which the node represents. This includes n1 - n2 pixels 172 whose colors should be defined by nodes at a lower level in the tree.</p> 173 <p>Assignment generates the output image from the pruned tree. The output 174 image consists of two parts:</p> 175 <ol class="arabic simple"> 176 <li>A color map, which is an array of color descriptions (RGB triples) 177 for each color present in the output image.</li> 178 <li>A pixel array, which represents each pixel as an index into the 179 color map array.</li> 180 </ol> 181 <p>First, the assignment phase makes one pass over the pruned color 182 description tree to establish the image's color map. For each node with 183 n2 > 0, it divides Sr, Sg, and Sb by n2. This produces the mean color of 184 all pixels that classify no lower than this node. Each of these colors 185 becomes an entry in the color map.</p> 186 <p>Finally, the assignment phase reclassifies each pixel in the pruned tree 187 to identify the deepest node containing the pixel's color. The pixel's 188 value in the pixel array becomes the index of this node's mean color in 189 the color map.</p> 190 <p>Empirical evidence suggests that the distances in color spaces such as 191 YUV, or YIQ correspond to perceptual color differences more closely than 192 do distances in RGB space. These color spaces may give better results 193 when color reducing an image. Here the algorithm is as described except 194 each pixel is a point in the alternate color space. For convenience, the 195 color components are normalized to the range 0 to a maximum value, Cmax. 196 The color reduction can then proceed as described.</p> 197 </div> 198 <div class="section" id="measuring-color-reduction-error"> 199 <h1>Measuring Color Reduction Error</h1> 200 <p>Depending on the image, the color reduction error may be obvious or 201 invisible. Images with high spatial frequencies (such as hair or grass) 202 will show error much less than pictures with large smoothly shaded areas 203 (such as faces). This is because the high-frequency contour edges 204 introduced by the color reduction process are masked by the high 205 frequencies in the image.</p> 206 <p>To measure the difference between the original and color reduced images 207 (the total color reduction error), GraphicsMagick sums over all pixels in 208 an image the distance squared in RGB space between each original pixel 209 value and its color reduced value. GraphicsMagick prints several error 210 measurements including the mean error per pixel, the normalized mean 211 error, and the normalized maximum error.</p> 212 <p>The normalized error measurement can be used to compare images. In 213 general, the closer the mean error is to zero the more the quantized 214 image resembles the source image. Ideally, the error should be 215 perceptually-based, since the human eye is the final judge of 216 quantization quality.</p> 217 <p>These errors are measured and printed when -verbose and -colorsare 218 specified on the command line:</p> 219 <blockquote> 220 <dl class="docutils"> 221 <dt>mean error per pixel:</dt> 222 <dd>is the mean error for any single pixel in the image.</dd> 223 <dt>normalized mean square error:</dt> 224 <dd>is the normalized mean square quantization error for any single 225 pixel in the image. 226 This distance measure is normalized to a range between 0 and 1. It 227 is independent of the range of red, green, and blue values in the 228 image.</dd> 229 <dt>normalized maximum square error:</dt> 230 <dd>is the largest normalized square quantization error for any single 231 pixel in the image. 232 This distance measure is normalized to a range between and blue 233 values in the image.</dd> 234 </dl> 235 </blockquote> 236 </div> 237 <div class="section" id="authors"> 238 <h1>Authors</h1> 239 <p>John Cristy, <a class="reference external" href="mailto:magick%40imagemagick.org">magick<span>@</span>imagemagick<span>.</span>org</a>, ImageMagick Studio.</p> 240 </div> 241 <div class="section" id="acknowledgements"> 242 <h1>Acknowledgements</h1> 243 <p>Paul Raveling, USC Information Sciences Institute, for the original idea 244 of using space subdivision for the color reduction algorithm. With Paul's 245 permission, this document is an adaptation from a document he wrote.</p> 246 <hr class="docutils" /> 247 <p>Copyright © GraphicsMagick Group 2002 - 2020</p> 248 </div> 249 </div> 250 </body> 251 </html>