github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/graphics-magick/share/man/man5/quantize.5 (about)

     1  .ad l
     2  .nh
     3  .TH quantize 5 "$Date$" "ImageMagick"
     4  .SH NAME
     5  Quantize - ImageMagick's color reduction algorithm.
     6  .SH SYNOPSIS
     7  .B #include <magick.h>
     8  .SH DESCRIPTION
     9  This document describes how \fBImageMagick\fP performs color reduction on an
    10  image.  To fully understand this document, you should have a knowledge
    11  of basic imaging techniques and the tree data structure and terminology.
    12  
    13  For purposes of color allocation, an image is a set of \fIn\fP pixels,
    14  where each pixel is a point in RGB space.  RGB space is a 3-dimensional
    15  vector space, and each pixel, \fIp\d\s-3i\s0\u\fP,  is defined by an
    16  ordered triple of red, green, and blue coordinates, (\fIr\d\s-3i\s0\u,
    17  g\d\s-3i\s0\u, b\d\s-3i\s0\u\fP).
    18  
    19  Each primary color component (red, green, or blue) represents an
    20  intensity which varies linearly from 0 to a maximum value,
    21  \fIc\d\s-3max\s0\u\fP, which corresponds to full saturation of that
    22  color.  Color allocation is defined over a domain consisting of the
    23  cube in RGB space with opposite vertices at (0,0,0) and
    24  (\fIc\d\s-3max\s0\u,c\d\s-3max\s0\u,c\d\s-3max\s0\u\fP).  \fBImageMagick\fP
    25  requires \fIc\d\s-3max\s0\u = 255\fP.
    26  
    27  The algorithm maps this domain onto a tree in which each node
    28  represents a cube within that domain.  In the following discussion,
    29  these cubes are defined by the coordinate of two opposite vertices: The
    30  vertex nearest the origin in RGB space and the vertex farthest from the
    31  origin.
    32  
    33  The tree's root node represents the the entire domain, (0,0,0) through
    34  (\fIc\d\s-3max\s0\u,c\d\s-3max\s0\u,c\d\s-3max\s0\u\fP).  Each lower level in
    35  the tree is generated by subdividing one node's cube into eight smaller
    36  cubes of equal size.  This corresponds to bisecting the parent cube
    37  with planes passing through the midpoints of each edge.
    38  
    39  The basic algorithm operates in three phases:  \fBClassification,
    40  Reduction\fP, and \fBAssignment\fP.  \fBClassification\fP builds a
    41  color description tree for the image.  \fBReduction\fP collapses the
    42  tree until the number it represents, at most, is the number of colors
    43  desired in the output image.  \fBAssignment\fP defines the output
    44  image's color map and sets each pixel's color by reclassification in
    45  the reduced tree. Our goal is to minimize the numerical discrepancies
    46  between the original colors and quantized colors.  To learn more about
    47  quantization error, see MEASURING COLOR REDUCTION ERROR later in this
    48  document.
    49  
    50  \fBClassification\fP begins by initializing a color description tree of
    51  sufficient depth to represent each possible input color in a leaf.
    52  However, it is impractical to generate a fully-formed color description
    53  tree in the classification phase for realistic values of
    54  \fIc\d\s-3max\s0\u\fP.  If color components in the input image are
    55  quantized to \fIk\fP-bit precision, so that \fIc\d\s-3max\s0\u =
    56  2\u\s-3k\s0\d-1\fP, the tree would need \fIk\fP levels below the root
    57  node 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  .nf
    61          \s+6\(*S\u\s-9 k\d\di=1\s0 8k\fP\s0\u
    62  .fi
    63  .PP
    64  A complete tree would require 19,173,961 nodes for \fIk = 8,
    65  c\d\s-3max\s0\u = 255\fP.  Therefore, to avoid building a fully
    66  populated tree, \fBImageMagick\fP: (1) Initializes data structures for
    67  nodes only as they are needed; (2) Chooses a maximum depth for the tree
    68  as a function of the desired number of colors in the output image
    69  (currently \fIlog\d\s-34\s0\u(colormap size)\+2\fP).  A tree of this
    70  depth generally allows the best representation of the source image with
    71  the fastest computational speed and the least amount of memory.
    72  However, the default depth is inappropriate for some images.
    73  Therefore, the caller can request a specific tree depth.
    74  
    75  For each pixel in the input image, classification scans downward from
    76  the root of the color description tree.  At each level of the tree, it
    77  identifies the single node which represents a cube in RGB space
    78  containing the pixel's color.  It updates the following data for each
    79  such node:
    80  .TP
    81  .B n\d\s-31\s0\u:
    82  Number of pixels whose color is contained in the RGB cube which this
    83  node represents;
    84  .TP
    85  .B n\d\s-32\s0\u:
    86  Number of pixels whose color is not represented in a node at lower
    87  depth in the tree;  initially,  \fIn\d\s-32\s0\u = 0\fP for all nodes
    88  except leaves of the tree.
    89  .TP
    90  .B S\d\s-3r\s0\u, S\d\s-3g\s0\u, S\d\s-3b\s0\u:
    91  Sums of the red, green, and blue component values for all pixels not
    92  classified at a lower depth.  The combination of these sums and
    93  \fIn\d\s-32\s0\u\fP will ultimately characterize the mean color of a
    94  set of pixels represented by this node.
    95  .TP
    96  .B E:
    97  The distance squared in RGB space between each pixel contained within a
    98  node and the nodes' center.  This represents the quantization error for
    99  a node.
   100  .PP
   101  \fBReduction\fP repeatedly prunes the tree until the number of nodes with
   102  \fIn\d\s-32\s0\u  > 0\fP is less than or equal to the maximum number of colors
   103  allowed in the output image.  On any given iteration over the tree, it
   104  selects those nodes whose \fIE\fP value is minimal for pruning and
   105  merges their color statistics upward.  It uses a pruning threshold,
   106  \fIE\d\s-3p\s0\u\fP, to govern node selection as follows:
   107  
   108    E\d\s-3p\s0\u = 0
   109    while number of nodes with (n\d\s-32\s0\u > 0) > required maximum number of colors
   110        prune all nodes such that E <= E\d\s-3p\s0\u
   111        Set E\d\s-3p\s0\u  to minimum E in remaining nodes
   112  
   113  This has the effect of minimizing any quantization error when
   114  merging two nodes together.
   115  
   116  When a node to be pruned has offspring, the pruning procedure invokes
   117  itself recursively in order to prune the tree from the leaves upward.
   118  The values of \fIn\d\s-32\s0\u  S\d\s-3r\s0\u, S\d\s-3g\s0\u,\fP  and
   119  \fIS\d\s-3b\s0\u\fP in a node being pruned are always added to the
   120  corresponding data in that node's parent.  This retains the pruned
   121  node's color characteristics for later averaging.
   122  
   123  For each node,  \fIn\d\s-32\s0\u\fP pixels exist for which that node
   124  represents the smallest volume in RGB space containing those pixel's
   125  colors.  When \fIn\d\s-32\s0\u  > 0\fP the node will uniquely define a
   126  color in the output image.  At the beginning of reduction,
   127  \fIn\d\s-32\s0\u = 0\fP  for all nodes except the leaves of the tree
   128  which represent colors present in the input image.
   129  
   130  The other pixel count, \fIn\d\s-31\s0\u\fP,  indicates the total
   131  number of colors within the cubic volume which the node represents.
   132  This includes \fIn\d\s-31\s0\u - n\d\s-32\s0\u\fP pixels whose colors
   133  should be defined by nodes at a lower level in the tree.
   134  
   135  \fBAssignment\fP generates the output image from the pruned tree.  The
   136  output image consists of two parts:  (1)  A color map, which is an
   137  array of color descriptions (RGB triples) for each color present in the
   138  output image; (2)  A pixel array, which represents each pixel as an
   139  index into the color map array.
   140  
   141  First, the assignment phase makes one pass over the pruned color
   142  description tree to establish the image's color map.  For each node
   143  with \fIn\d\s-32\s0\u > 0\fP, it divides \fIS\d\s-3r\s0\u,
   144  S\d\s-3g\s0\u\fP, and \fPS\d\s-3b\s0\u\fP by \fIn\d\s-32\s0\u\fP.  This
   145  produces the mean color of all pixels that classify no lower than this
   146  node.  Each of these colors becomes an entry in the color map.
   147  
   148  Finally, the assignment phase reclassifies each pixel in the pruned
   149  tree to identify the deepest node containing the pixel's color.  The
   150  pixel's value in the pixel array becomes the index of this node's mean
   151  color in the color map.
   152  
   153  Empirical evidence suggests that distances in color spaces such as
   154  YUV, or YIQ correspond to perceptual color differences more closely
   155  than do distances in RGB space.  These color spaces may give better
   156  results when color reducing an image.  Here the algorithm is as described
   157  except each pixel is a point in the alternate color space.  For convenience,
   158  the color components are normalized to the range 0 to a maximum value,
   159  \fIc\d\s-3max\s0\u\fP.  The color reduction can then proceed as described.
   160  .SH "MEASURING COLOR REDUCTION ERROR"
   161  
   162  Depending on the image, the color reduction error may be obvious or
   163  invisible.  Images with high spatial frequencies (such as hair or
   164  grass) will show error much less than pictures with large smoothly
   165  shaded areas (such as faces).  This is because the high-frequency
   166  contour edges introduced by the color reduction process are masked by
   167  the high frequencies in the image.
   168  
   169  To measure the difference between the original and color reduced images
   170  (the total color reduction error), \fBImageMagick\fP sums over all pixels
   171  in an image the distance squared in RGB space between each original
   172  pixel value and its color reduced value. \fBImageMagick\fP prints several error
   173  measurements including the mean error per pixel, the normalized mean error,
   174  and the normalized maximum error.
   175  
   176  The normalized error measurement can be used to compare images.  In
   177  general, the closer the mean error is to zero the more the quantized
   178  image resembles the source image.  Ideally, the error should be
   179  perceptually-based, since the human eye is the final judge of
   180  quantization quality.
   181  
   182  These errors are measured and printed when \fB-verbose\fP and \fB-colors\fI
   183  are specified on the command line:
   184  .TP
   185  .B mean error per pixel:
   186  is the mean error for any single pixel in the image.
   187  .TP
   188  .B normalized mean square error:
   189  is the normalized mean square quantization error for any single pixel in the
   190  image.
   191  
   192  This distance measure is normalized to a range between 0 and 1.  It is
   193  independent of the range of red, green, and blue values in the image.
   194  .TP
   195  .B normalized maximum square error:
   196  is the largest normalized square quantization error for any single
   197  pixel in the image.
   198  
   199  This distance measure is normalized to a range between 0 and 1.  It is
   200  independent of the range of red, green, and blue values in the image.
   201  .SH SEE ALSO
   202  .B
   203  display(1), animate(1), mogrify(1), import(1), miff(5)
   204  .SH COPYRIGHT
   205  Copyright (C) 2002 ImageMagick Studio, a non-profit organization dedicated
   206  to making software imaging solutions freely available.
   207  
   208  Permission is hereby granted, free of charge, to any person obtaining a
   209  copy of this software and associated documentation files ("ImageMagick"),
   210  to deal in ImageMagick without restriction, including without limitation
   211  the rights to use, copy, modify, merge, publish, distribute, sublicense,
   212  and/or sell copies of ImageMagick, and to permit persons to whom the
   213  ImageMagick is furnished to do so, subject to the following conditions:
   214  
   215  The above copyright notice and this permission notice shall be included in
   216  all copies or substantial portions of ImageMagick.
   217  
   218  The software is provided "as is", without warranty of any kind, express or
   219  implied, including but not limited to the warranties of merchantability,
   220  fitness for a particular purpose and noninfringement.  In no event shall
   221  ImageMagick Studio be liable for any claim, damages or other liability,
   222  whether in an action of contract, tort or otherwise, arising from, out of
   223  or in connection with ImageMagick or the use or other dealings in
   224  ImageMagick.
   225  
   226  Except as contained in this notice, the name of the ImageMagick Studio
   227  shall not be used in advertising or otherwise to promote the sale, use or
   228  other dealings in ImageMagick without prior written authorization from the
   229  ImageMagick Studio.
   230  .SH ACKNOWLEDGEMENTS
   231  Paul Raveling, USC Information Sciences Institute, for the original
   232  idea of using space subdivision for the color reduction algorithm.
   233  With Paul's permission, this document is an adaptation from a document he
   234  wrote.
   235  .SH AUTHORS
   236  John Cristy, ImageMagick Studio