Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

203 строки
8.2 KiB

  1. <?php header("Content-Type: text/html; charset=utf-8"); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="GENERATOR" content="vim" />
  8. <meta name="Author" content="sam@zoy.org (Sam Hocevar)" />
  9. <meta name="Description" content="Libcaca study - 1. Colour quantisation" />
  10. <meta name="Keywords" content="libcaca, ASCII, ASCII ART, console, text mode, ncurses, slang, AAlib, dithering, thresholding" />
  11. <title>Libcaca study - 1. Colour quantisation</title>
  12. <link rel="icon" type="image/x-icon" href="/favicon.ico" />
  13. <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
  14. <link rel="stylesheet" type="text/css" href="/main.css" />
  15. </head>
  16. <body>
  17. <?php include($_SERVER["DOCUMENT_ROOT"]."/header.inc"); ?>
  18. <p> <span style="color: #aa0000; font-weight: bold;">Warning</span>: this
  19. document is still work in progress. Feel free to send comments but do not
  20. consider it final material. </p>
  21. <div style="float: right;">
  22. <a href="part2.html">&gt;&gt;&gt; Halftoning</a>
  23. </div>
  24. <div style="text-align: center;">
  25. <a href="index.html">^^^ Index</a>
  26. </div>
  27. <h2> 1. Colour quantisation </h2>
  28. <p> The process of reducing the number of colours used in an image is called
  29. <b>colour quantisation</b>. It is a very old and common computer graphics
  30. problem. Many methods exist to do the task, and their efficiency depends on
  31. several parameters: </p>
  32. <ul>
  33. <li> the input image: is it a photograph? a vector drawing? a composition
  34. of both? </li>
  35. <li> the target media: is it a computer screen? if so, what are the size
  36. and the position of the pixels? is it a printed document? if so,
  37. what kind of paper? what kind of ink? or maybe the conversion should
  38. be optimised for both targets? </li>
  39. <li> the quality requirements: for instance, can contrast be raised for
  40. a more appealing result at the expense of accuracy?
  41. <li> the allowed computation time: do we need 50fps or can we afford to
  42. wait 10 seconds for a better result? </li>
  43. </ul>
  44. <h3> 1.1. Black and white thresholding </h3>
  45. <p> Since a greyscale pixel has a value between 0 and 1, a fast method
  46. to convert the image to black and white is to set all pixels below 0.5
  47. to black and all pixels above 0.5 to white. This method is called
  48. <b>thresholding</b> and, in our case, results in the following image: </p>
  49. <p style="text-align: center;">
  50. <img src="out/lena1-1-1.png" width="256" height="256"
  51. class="inline" alt="50% threshold" />
  52. <img src="out/grad1-1-1.png" width="32" height="256"
  53. class="inline" alt="50% threshold gradient" />
  54. </p>
  55. <p> Not that bad, but we were pretty lucky: the original image’s brightness
  56. was rather well balanced. A lot of detail is lost, though. Different results
  57. can be obtained by choosing “threshold values” other than 0.5, for instance
  58. 0.4 or 0.6, resulting in a much brighter or darker image: </p>
  59. <p style="text-align: center;">
  60. <img src="out/lena1-1-2.png" width="256" height="256"
  61. class="inline" alt="40% threshold" />
  62. <img src="out/grad1-1-2.png" width="32" height="256"
  63. class="inline" alt="40% threshold gradient" />
  64. <img src="out/lena1-1-3.png" width="256" height="256"
  65. class="inline" alt="60% threshold" />
  66. <img src="out/grad1-1-3.png" width="32" height="256"
  67. class="inline" alt="60% threshold gradient" />
  68. </p>
  69. <p> Choosing the best thresholding value for a given image is called
  70. <b>average dithering</b>. But even with the best value, the results will
  71. not improve tremendously. </p>
  72. <h3> 1.2. Greyscale thresholding </h3>
  73. <p> Better results can be achieved with a slightly bigger palette. Here is
  74. thresholding applied to a 3-colour and to a 5-colour palette: </p>
  75. <p style="text-align: center;">
  76. <img src="out/lena1-2-1.png" width="256" height="256"
  77. class="inline" alt="3-colour threshold" />
  78. <img src="out/grad1-2-1.png" width="32" height="256"
  79. class="inline" alt="3-colour threshold gradient" />
  80. <img src="out/lena1-2-2.png" width="256" height="256"
  81. class="inline" alt="5-colour threshold" />
  82. <img src="out/grad1-2-2.png" width="32" height="256"
  83. class="inline" alt="5-colour threshold gradient" />
  84. </p>
  85. <p> Using this method, shades of grey are evenly used. However, the global
  86. error is far from optimal, as the following graphs show: </p>
  87. <p style="text-align: center;">
  88. <img src="fig1-2-1.png" width="256" height="256"
  89. alt="mean error 0.138" />
  90. <img src="fig1-2-2.png" width="256" height="256"
  91. alt="mean error 0.075" />
  92. </p>
  93. <p> The following thresholding method minimises the error, at the cost of
  94. underusage of pure black and white colours: </p>
  95. <p style="text-align: center;">
  96. <img src="out/lena1-2-3.png" width="256" height="256"
  97. class="inline" alt="3-colour threshold, minimal error" />
  98. <img src="out/grad1-2-3.png" width="32" height="256"
  99. class="inline" alt="3-colour threshold gradient, minimal error" />
  100. <img src="out/lena1-2-4.png" width="256" height="256"
  101. class="inline" alt="5-colour threshold, minimal error" />
  102. <img src="out/grad1-2-4.png" width="32" height="256"
  103. class="inline" alt="5-colour threshold gradient, minimal error" />
  104. </p>
  105. <p style="text-align: center;">
  106. <img src="fig1-2-3.png" width="256" height="256"
  107. alt="mean error 0.125" />
  108. <img src="fig1-2-4.png" width="256" height="256"
  109. alt="mean error 0.0625" />
  110. </p>
  111. <p> This is a perfect example of a situation where colour accuracy does not
  112. help achieve a better result. </p>
  113. <h3> 1.3. Dynamic thresholding </h3>
  114. <p> Dynamic thresholding consists in studying the image before selecting
  115. the threshold values. One strategy, for instance, is to choose the median
  116. pixel value. This is done simply by computing a histogram of the image. </p>
  117. <p style="text-align: center;">
  118. <img src="out/lena1-3-1.png" width="256" height="256"
  119. class="inline" alt="2-colour dynamic threshold" />
  120. <img src="out/grad1-3-1.png" width="32" height="256"
  121. class="inline" alt="2-colour dynamic threshold gradient" />
  122. <img src="out/lena1-3-2.png" width="256" height="256"
  123. class="inline" alt="5-colour dynamic threshold" />
  124. <img src="out/grad1-3-2.png" width="32" height="256"
  125. class="inline" alt="5-colour dynamic threshold gradient" />
  126. </p>
  127. <h3> 1.4. Random dithering </h3>
  128. <p> Instead of constantly using the same threshold value, one can use a
  129. different random value for each pixel in the image. This technique is simply
  130. called <b>random dithering</b>. </p>
  131. <p> Here are two simple examples. On the left, threshold values are uniformly
  132. chosen between 0 and 1. On the right, random dithering with threshold values
  133. chosen with a <b>gaussian distribution</b> (mean 0.5, standard deviation 0.15):
  134. </p>
  135. <p style="text-align: center;">
  136. <img src="out/lena1-4-1.png" width="256" height="256"
  137. class="inline" alt="random dithering" />
  138. <img src="out/grad1-4-1.png" width="32" height="256"
  139. class="inline" alt="random dithering gradient" />
  140. <img src="out/lena1-4-2.png" width="256" height="256"
  141. class="inline" alt="gaussian (0.5, 0.15) dithering" />
  142. <img src="out/grad1-4-2.png" width="32" height="256"
  143. class="inline" alt="gaussian (0.5, 0.15) dithering gradient" />
  144. </p>
  145. <p> The images look very noisy, but they are arguably an improvement over
  146. standard constant thresholding. </p>
  147. <p> Finally, this is dynamic thresholding with 4 colours where threshold values
  148. at every pixel are computed as usual, but then perturbated using a gaussian
  149. distribution (mean 0, standard deviation 0.08): </p>
  150. <p style="text-align: center;">
  151. <img src="out/lena1-4-3.png" width="256" height="256"
  152. class="inline" alt="4-colour dynamic thresholding, gaussian (0, 0.08)" />
  153. <img src="out/grad1-4-3.png" width="32" height="256"
  154. class="inline" alt="4-colour dynamic thresholding, gaussian (0, 0.08) gradient" />
  155. </p>
  156. <div style="float: right;">
  157. <a href="part2.html">&gt;&gt;&gt; Halftoning</a>
  158. </div>
  159. <div style="text-align: center;">
  160. <a href="index.html">^^^ Index</a>
  161. </div>
  162. <?php $rev = '$Id$';
  163. include($_SERVER['DOCUMENT_ROOT'].'/footer.inc'); ?>
  164. </body>
  165. </html>