You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

478 regels
19 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 - 2. Halftoning" />
  10. <meta name="Keywords" content="libcaca, ASCII, ASCII ART, console, text mode, ncurses, slang, AAlib, dithering, thresholding" />
  11. <title>Libcaca study - 2. Halftoning</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: left;">
  22. <a href="part1.html">Colour quantisation &lt;&lt;&lt;</a>
  23. </div>
  24. <div style="float: right;">
  25. <a href="part3.html">&gt;&gt;&gt; Error diffusion</a>
  26. </div>
  27. <div style="text-align: center;">
  28. <a href="index.html">^^^ Index</a>
  29. </div>
  30. <h2> 2. Halftoning </h2>
  31. <h3> 2.1. Halftoning patterns </h3>
  32. <p> Observe the following patterns. From a certain distance or assuming small
  33. enough pixels, they look like shades of grey despite being made of only black
  34. and white pixels: </p>
  35. <p style="text-align: center;">
  36. <img src="out/pat2-1-1.png" width="320" height="80"
  37. class="inline" alt="50% pattern" />
  38. </p>
  39. <p> We can do even better using additional patterns such as these 25% and
  40. 75% halftone patterns: </p>
  41. <p style="text-align: center;">
  42. <img src="out/pat2-1-2.png" width="320" height="80"
  43. class="inline" alt="25% and 75% patterns" />
  44. </p>
  45. <p> This looks promising. Let’s try immediately on Lena: we will use the
  46. 5-colour thresholding picture and replace the 0.25, 0.5 and 0.75 grey values
  47. with the above patterns: </p>
  48. <p style="text-align: center;">
  49. <img src="out/lena2-1-1.png" width="256" height="256"
  50. class="inline" alt="25%, 50% and 75% halftoning" />
  51. <img src="out/grad2-1-1.png" width="32" height="256"
  52. class="inline" alt="25%, 50% and 75% halftoning gradient" />
  53. </p>
  54. <p> Not bad for a start. But there is a lot to improve.
  55. By the way, this technique is covered by Apple’s <a
  56. href="http://www.freepatentsonline.com/5761347.html">U.S. patent 5761347</a>
  57. [15]. </p>
  58. <h3> 2.2. Screen artifacts </h3>
  59. <p> If your screen’s quality is not very good, you might experience slightly
  60. different shades of grey for the following patterns, despite being made of 50%
  61. black and 50% white pixels: </p>
  62. <p style="text-align: center;">
  63. <img src="out/pat2-2-1.png" width="240" height="80"
  64. class="inline" alt="screen imperfections" />
  65. </p>
  66. <p> Obviously the middle pattern looks far better to the human eye on a
  67. computer screen. Optimising patterns so that they look good to the human
  68. eye and don't create artifacts is a crucial element of a dithering
  69. algorithm. Here is another example of two patterns that approximate to
  70. the same shade of grey but may look slightly different from a distance: </p>
  71. <p style="text-align: center;">
  72. <img src="out/pat2-2-2.png" width="320" height="80"
  73. class="inline" alt="two different 25% patterns" />
  74. </p>
  75. <h3> 2.3. Ordered dithering </h3>
  76. <p> A generalisation of the dithering technique we just saw that uses a
  77. certain family of patterns is called <b>ordered dithering</b>. It is based on
  78. a <b>dither matrix</b> such as the following one: </p>
  79. <p style="text-align: center;">
  80. <img src="fig2-3-1.png" width="80" height="80" alt="2×2 dither matrix" />
  81. </p>
  82. <p> Using the matrix coefficients as threshold values yield the following
  83. results for black, white and three shades of grey (0.25, 0.5 and 0.75): </p>
  84. <p style="text-align: center;">
  85. <img src="fig2-3-5.png" width="507" height="173"
  86. alt="results of 2×2 dither matrix" />
  87. </p>
  88. <p> The dither matrix is therefore repeated all over the image. The first pixel
  89. will be thresholded with a value of 0.2, the second pixel with a value of 0.8,
  90. then the third pixel with a value of 0.2 again, and so on, resulting in an image
  91. very similar to the one previously seen in 2.1: </p>
  92. <p style="text-align: center;">
  93. <img src="fig2-3-1b.png" width="240" height="160"
  94. class="matrix" alt="tiled dither matrix" />
  95. <img src="out/lena2-3-0.png" width="256" height="256"
  96. class="inline" alt="2×2 Bayer dithering" />
  97. <img src="out/grad2-3-0.png" width="32" height="256"
  98. class="inline" alt="2×2 Bayer dithering gradient" />
  99. </p>
  100. <p> For better readability, the matrix is rewritten as following. The dither
  101. coefficients are trivially computed from the matrix cells and the matrix size:
  102. </p>
  103. <p style="text-align: center;">
  104. <img src="fig2-3-1c.png" width="297" height="80"
  105. alt="normalised 2×2 dither matrix" />
  106. </p>
  107. <p> Different matrices can give very different results. This is a 4×4 <b>Bayer
  108. ordered dither matrix</b> [17], recursively created from the previous 2×2
  109. dither matrix: </p>
  110. <p style="text-align: center;">
  111. <img src="out/fig2-3-2.png" width="161" height="161"
  112. class="matrix" alt="4×4 Bayer matrix" />
  113. <img src="out/lena2-3-1.png" width="256" height="256"
  114. class="inline" alt="4×4 Bayer dithering" />
  115. <img src="out/grad2-3-1.png" width="32" height="256"
  116. class="inline" alt="4×4 Bayer dithering gradient" />
  117. </p>
  118. <p> This is an 8×8 Bayer matrix, recursively created from the 4×4 version: </p>
  119. <p style="text-align: center;">
  120. <img src="out/fig2-3-2b.png" width="240" height="240"
  121. class="matrix" alt="4×4 Bayer matrix" />
  122. <img src="out/lena2-3-1b.png" width="256" height="256"
  123. class="inline" alt="4×4 Bayer dithering" />
  124. <img src="out/grad2-3-1b.png" width="32" height="256"
  125. class="inline" alt="4×4 Bayer dithering gradient" />
  126. </p>
  127. <p> This 4×4 <b>cluster dot matrix</b> creates dot patterns: </p>
  128. <p style="text-align: center;">
  129. <img src="out/fig2-3-3.png" width="161" height="161"
  130. class="matrix" alt="4×4 cluster dot matrix" />
  131. <img src="out/lena2-3-2.png" width="256" height="256"
  132. class="inline" alt="4×4 cluster dot dithering" />
  133. <img src="out/grad2-3-2.png" width="32" height="256"
  134. class="inline" alt="4×4 cluster dot dithering gradient" />
  135. </p>
  136. <p> This 8×8 cluster dot matrix mimics the halftoning techniques used by
  137. newspapers: </p>
  138. <p style="text-align: center;">
  139. <img src="out/fig2-3-3b.png" width="240" height="240"
  140. class="matrix" alt="4×4 cluster dot matrix" />
  141. <img src="out/lena2-3-2b.png" width="256" height="256"
  142. class="inline" alt="4×4 cluster dot dithering" />
  143. <img src="out/grad2-3-2b.png" width="32" height="256"
  144. class="inline" alt="4×4 cluster dot dithering gradient" />
  145. </p>
  146. <p> This unusual 5×3 matrix creates artistic vertical line artifacts: </p>
  147. <p style="text-align: center;">
  148. <img src="out/fig2-3-4.png" width="201" height="121"
  149. class="matrix" alt="4×4 cluster dot matrix" />
  150. <img src="out/lena2-3-3.png" width="256" height="256"
  151. class="inline" alt="4×4 cluster dot dithering" />
  152. <img src="out/grad2-3-3.png" width="32" height="256"
  153. class="inline" alt="4×4 cluster dot dithering gradient" />
  154. </p>
  155. <p> There are two major issues with ordered dithering. First, important
  156. <b>visual artifacts</b> may appear. Even Bayer ordered dithering causes
  157. weird cross-hatch pattern artifacts on some images. Second, dithering
  158. matrices do not depend on the original image and thus <b>do not take input
  159. data into account</b>: high frequency features in the image are often missed
  160. and, in some cases, cause even worse artifacts. </p>
  161. <h3> 2.4. Random ordered dithering </h3>
  162. <p> Random dithering can help reduce the major problem caused by halftoning,
  163. which is the apparition of pattern artifacts. The method is as simple as
  164. <b>slightly perturbating dither matrix coefficients</b> (or pixel values)
  165. during the halftoning step. The difficult part is picking up an adequate
  166. perturbation function: too much perturbation and the result is unrecognisable,
  167. too little and the artifacts stay. </p>
  168. <p> For instance, this is the result of 8×8 Bayer dithering perturbated by a
  169. gaussian distribution (mean 0.0, standard deviation 0.08): </p>
  170. <p style="text-align: center;">
  171. <img src="out/lena2-4-1.png" width="256" height="256"
  172. class="inline" alt="4×4 Bayer dithering, gaussian perturbation" />
  173. <img src="out/grad2-4-1.png" width="32" height="256"
  174. class="inline" alt="4×4 Bayer dithering, gaussian perturbation gradient" />
  175. </p>
  176. <p> Another way to use random number generators to avoid pattern artifacts is
  177. <b>random dither matrix selection</b> [22]. The image space is no longer tiled
  178. with the same matrix over and over again, but with a random selection from a
  179. list of similar dither matrices. </p>
  180. <p> This example shows random matrix selection from a list of six 3×3 dither
  181. matrices: </p>
  182. <p style="text-align: center;">
  183. <img src="fig2-4-1.png" width="290" height="190"
  184. class="matrix" alt="four 3×3 dispersed dot matrices" />
  185. <img src="out/lena2-4-2.png" width="256" height="256"
  186. class="inline" alt="random Bayer matrix dithering" />
  187. <img src="out/grad2-4-2.png" width="32" height="256"
  188. class="inline" alt="random Bayer matrix dithering gradient" />
  189. </p>
  190. <h3> 2.5. Non-rectangular dither tiles </h3>
  191. <p> Another way to avoid disturbing pattern artifacts is to use non-rectangular
  192. dither tiles. Here are several examples, the first one generating slanted
  193. square patterns, the second one hexagonal patterns, then slanted square
  194. patterns again with a slightly different angle, and hexagonal patterns again.
  195. The artifacts usually seen in Bayer dithering do not appear here: </p>
  196. <p style="text-align: center;">
  197. <img src="fig2-5-1.png" width="240" height="240"
  198. class="matrix" alt="cross dither tile" />
  199. <img src="out/lena2-5-1.png" width="256" height="256"
  200. class="inline" alt="cross dithering" />
  201. <img src="out/grad2-5-1.png" width="32" height="256"
  202. class="inline" alt="cross dithering gradient" />
  203. </p>
  204. <p style="text-align: center;">
  205. <img src="fig2-5-2.png" width="270" height="180"
  206. class="matrix" alt="hex dither tile" />
  207. <img src="out/lena2-5-2.png" width="256" height="256"
  208. class="inline" alt="hex dithering" />
  209. <img src="out/grad2-5-2.png" width="32" height="256"
  210. class="inline" alt="hex dithering gradient" />
  211. </p>
  212. <p style="text-align: center;">
  213. <img src="fig2-5-3.png" width="270" height="210"
  214. class="matrix" alt="square dither tile" />
  215. <img src="out/lena2-5-3.png" width="256" height="256"
  216. class="inline" alt="square dithering" />
  217. <img src="out/grad2-5-3.png" width="32" height="256"
  218. class="inline" alt="square dithering gradient" />
  219. </p>
  220. <p style="text-align: center;">
  221. <img src="fig2-5-4.png" width="240" height="210"
  222. class="matrix" alt="hex2 dither tile" />
  223. <img src="out/lena2-5-4.png" width="256" height="256"
  224. class="inline" alt="hex2 dithering" />
  225. <img src="out/grad2-5-4.png" width="32" height="256"
  226. class="inline" alt="hex2 dithering gradient" />
  227. </p>
  228. <h3> 2.6. Supercell dithering </h3>
  229. <p> Supercell dithering consists in creating bigger dithering tiles
  230. (supercells) from base tiles. One example is Victor Ostromoukhov’s
  231. <b>CombiScreen</b> method [3]. </p>
  232. <p> Just like Bayer matrices, non-rectangular tiles can be used to recursively
  233. create bigger patterns, giving finer results. The amount of shades of grey
  234. that can be rendered using a given tile is the number of cells in the tile
  235. plus one. Here are a few examples using tiles seen previously: </p>
  236. <p style="text-align: center;">
  237. <img src="fig2-6-1.png" width="270" height="240"
  238. class="matrix" alt="4-wise cross dither tile" />
  239. <img src="out/lena2-6-1.png" width="256" height="256"
  240. class="inline" alt="4-wise cross dithering" />
  241. <img src="out/grad2-6-1.png" width="32" height="256"
  242. class="inline" alt="4-wise cross dithering gradient" />
  243. </p>
  244. <p style="text-align: center;">
  245. <img src="fig2-6-2.png" width="270" height="240"
  246. class="matrix" alt="3-wise hex dither tile" />
  247. <img src="out/lena2-6-2.png" width="256" height="256"
  248. class="inline" alt="3-wise hex dithering" />
  249. <img src="out/grad2-6-2.png" width="32" height="256"
  250. class="inline" alt="3-wise hex dithering gradient" />
  251. </p>
  252. <p style="text-align: center;">
  253. <img src="fig2-6-3.png" width="300" height="250"
  254. class="matrix" alt="4-wise square dither tile" />
  255. <img src="out/lena2-6-3.png" width="256" height="256"
  256. class="inline" alt="4-wise square dithering" />
  257. <img src="out/grad2-6-3.png" width="32" height="256"
  258. class="inline" alt="4-wise square dithering gradient" />
  259. </p>
  260. <p> This example shows a tile resembling a Davis-Knuth dragon curve. Though
  261. the tile itself is beautiful, it is in reality only a reorganisation of an
  262. 8×8 Bayer dither matrix. Therefore the resulting image is exactly the same
  263. as for classical Bayer dithering: </p>
  264. <p style="text-align: center;">
  265. <img src="out/fig2-6-4.png" width="325" height="250"
  266. class="matrix" alt="twin dragon dither tile" />
  267. <img src="out/lena2-6-4.png" width="256" height="256"
  268. class="inline" alt="twin dragon dithering" />
  269. <img src="out/grad2-6-4.png" width="32" height="256"
  270. class="inline" alt="twin dragon dithering gradient" />
  271. </p>
  272. <p> Here are two consecutive iterations of the hexagonal tiling shown
  273. above. Since the area of the original tile is 10 cells, the first iteration
  274. could display 11 different shades of grey. These iterations can display
  275. respectively 31 and 91 shades: </p>
  276. <p style="text-align: center;">
  277. <img src="fig2-6-5.png" width="240" height="210"
  278. class="matrix" alt="3-way hex2 dither tile" />
  279. <img src="out/lena2-6-5.png" width="256" height="256"
  280. class="inline" alt="3-way hex2 dithering" />
  281. <img src="out/grad2-6-5.png" width="32" height="256"
  282. class="inline" alt="3-way hex2 dithering gradient" />
  283. </p>
  284. <p style="text-align: center;">
  285. <img src="fig2-6-6.png" width="280" height="240"
  286. class="matrix" alt="9-way hex2 dither tile" />
  287. <img src="out/lena2-6-6.png" width="256" height="256"
  288. class="inline" alt="9-way hex2 dithering" />
  289. <img src="out/grad2-6-6.png" width="32" height="256"
  290. class="inline" alt="9-way hex2 dithering gradient" />
  291. </p>
  292. <h3> 2.7. Void and cluster method </h3>
  293. <p> Robert A. Ulichney’s <b>void and cluster method</b> [18] is a very generic
  294. method for dither array generation. It mainly targets huge matrices in order
  295. to reduce artifacts caused by tiling. </p>
  296. <p> The process goes through many steps. First, a working pattern matrix
  297. needs to be created: </p>
  298. <ul>
  299. <li> Generate an empty <i>w×h</i> matrix full of 0s </li>
  300. <li> Set <i>n</i> cells to 1 </li>
  301. <li> The working matrix is uniformly distributed:
  302. <ul>
  303. <li> Find the 1 with the most neighbours set to 1 and set that cell
  304. to 0 </li>
  305. <li> Find the 0 with the fewest neighbours set to 1 and set that cell
  306. to 1 </li>
  307. <li> Repeat until the 1 was just put back where it originally was </li>
  308. </ul>
  309. </li>
  310. </ul>
  311. <p> Usually <i>n</i> is about 10% of <i>w×h</i>. It can be generated
  312. randomly, loaded from a known pattern, or created using a more powerful
  313. algorithm (chapter 3 will introduce error diffusion algorithms that may be
  314. used to generate the working pattern matrix). </p>
  315. <p> The dither matrix is then generated from the working pattern in three
  316. steps: </p>
  317. <ul>
  318. <li> Dither indices 0 to <i>n - 1</i> are set using a first copy of the
  319. working matrix </li>
  320. <ul>
  321. <li> <i>n = n - 1</i> </li>
  322. <li> Find the 1 with the most neighbours set to 1 and set that cell
  323. to 0 </li>
  324. <li> In the dither matrix, set the corresponding cell to <i>n</i>
  325. </li>
  326. <li> Repeat until <i>n = 0</i> </li>
  327. </ul>
  328. </li>
  329. <li> Dither indices <i>n</i> to <i>w×h/2</i> are set using a second copy of
  330. the working matrix </li>
  331. <ul>
  332. <li> Find the 0 with the fewest neighbours set to 1 and set that cell
  333. to 1 </li>
  334. <li> In the dither matrix, set the corresponding cell to <i>n</i>
  335. <li> <i>n = n + 1</i> </li>
  336. <li> Repeat until <i>n ≥ w×h/2</i> </li>
  337. </ul>
  338. </li>
  339. <li> Dither indices <i>w×h/2</i> to <i>w×h</i> are set using the same copy
  340. of the working matrix </li>
  341. <ul>
  342. <li> Find the 0 with the fewest neighbours set to 1 and set that cell
  343. to 1 </li>
  344. <li> In the dither matrix, set the corresponding cell to <i>n</i>
  345. <li> <i>n = n + 1</i> </li>
  346. <li> Repeat until <i>n = w×h</i> </li>
  347. </ul>
  348. </li>
  349. </ul>
  350. <p> The key part of the algorithm is the choice of the void finder and the
  351. cluster finder for each step. Best results are achieved using <b>Voronoï
  352. tesselation</b> [19] [22], but simpler methods such as <b>gaussian
  353. convolution</b> [21] give decent results, too. </p>
  354. <p> The following two matrices show the results of the algorithm using
  355. randomly generated initial matrices of size respectively 14×14 and 25×25.
  356. The void and cluster finder uses a simple 7×7 gaussian convolution filter.
  357. Gray cells show the initial uniformly distributed matrix: </p>
  358. <p style="text-align: center;">
  359. <img src="out/fig2-7-1.png" width="350" height="350"
  360. class="matrix" alt="14×14 void-and-cluster matrix" />
  361. </p>
  362. <p style="text-align: center;">
  363. <img src="out/fig2-7-2.png" width="500" height="500"
  364. class="matrix" alt="25×25 void-and-cluster matrix" />
  365. </p>
  366. <p> Dither matrices generated with the void and cluster method give impressive
  367. results. They are pretty close to the best quality that can be achieved
  368. using standard ordered dithering: </p>
  369. <p style="text-align: center;">
  370. <img src="out/lena2-7-1.png" width="256" height="256"
  371. class="inline" alt="14×14 void and cluster dithering" />
  372. <img src="out/grad2-7-1.png" width="32" height="256"
  373. class="inline" alt="14×14 void and cluster dithering gradient" />
  374. <img src="out/lena2-7-2.png" width="256" height="256"
  375. class="inline" alt="25×25 void and cluster dithering" />
  376. <img src="out/grad2-7-2.png" width="32" height="256"
  377. class="inline" alt="25×25 void and cluster dithering gradient" />
  378. </p>
  379. <p> This technique is covered by Ulichney’s <a
  380. href="http://www.freepatentsonline.com/5535020.html">U.S. patent 5535020</a>
  381. and the specific implementation we showed is partly covered by Epson’s <a
  382. href="http://www.freepatentsonline.com/6088512.html">U.S. patent 6088512</a>.
  383. </p>
  384. <div style="float: left;">
  385. <a href="part1.html">Colour quantisation &lt;&lt;&lt;</a>
  386. </div>
  387. <div style="float: right;">
  388. <a href="part3.html">&gt;&gt;&gt; Error diffusion</a>
  389. </div>
  390. <div style="text-align: center;">
  391. <a href="index.html">^^^ Index</a>
  392. </div>
  393. <?php $rev = '$Id$';
  394. include($_SERVER['DOCUMENT_ROOT'].'/footer.inc'); ?>
  395. </body>
  396. </html>