daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Nov 17th, 2018 82 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. (Written as a way to stop forgetting these things.
  2. May be wrong sometimes, as it's just the result of my research here and there.
  3. If it helps you, give me a shout!)  
  4.  
  5. Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
  6. Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image
  7.  
  8. ----------------------------------------------------------------
  9. INTRODUCTION
  10.  
  11. The idea behind it is simple : given two available values a and b, let's say black and white,
  12. the value x between a and b - that should be grayish - is simulated by mixing pixels of colors a and b.
  13. To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.
  14. By turning the pixel on in a very specified order, the matrix creates the perception of
  15. continuous variation of color.
  16.  
  17. Here an example of a variaton for a 2x2 matrix :
  18.  
  19. ■ ■ | □ ■ | □ ■ | □ □ | □ □
  20. ■ ■ | ■ ■ | ■ □ | ■ □ | □ □
  21.  
  22. ----------------------------------------------------------------
  23. BAYER MATRIX
  24.  
  25. Dither matrix are power-of-2 matrix whose elements can be considered as threshold.
  26. In these matrices, consecutive threshold values are located far apart spatially,
  27. which gives the perception of a progressive variation.
  28. The values inside the matrix indicate how likely a pixel will be turned on.
  29. You can rotate, mirror, transpose these matrices and it won't bring much change to the dithered image.
  30.  
  31. ----------------------------------------------------------------
  32. HOW TO CONSTRUCT A DITHER MATRIX ?
  33.  
  34.       - By hand --> become quickly fastidious but is perfect to understand how the matrix works
  35.  
  36. The simplest - yet useless - dither matrix is the zero matrix  of dimension 1x1,
  37. this one doesn't apply any transformation. So yeah, useless.
  38.  
  39. On the oher hand, the 2x2 matrix is fundamental :
  40. | 0 2 |
  41. | 3 1 |
  42. Given that this matrix contains 4 differents values, we will see only 4 kinds of pattern in the dithered image.
  43. To reach better image quality, we need to use bigger matrices. Let's compute the 4x4 matrix recursively :
  44.  
  45. Step1 4x4:
  46. | 0  -  2  - |
  47. | -  -  -  - |
  48. | 3  -  1  - |
  49. | -  -  -  - |
  50.  
  51. Step2 4x4:
  52. | 0  -  2  - |
  53. | -  4  -  6 |
  54. | 3  -  1  - |
  55. | -  7  -  5 |
  56.  
  57. ...and so on... we just follow the pattern on the 2x2 case.
  58. The first step to obtain the 8x8 is the following :
  59.  
  60. Step1 8x8:
  61. | 0  -  8  -  2  - 10  - |
  62. | -  -  -  -  -  -  -  - |
  63. |12  -  4  - 14  -  6  - |
  64. | -  -  -  -  -  -  -  - |
  65. | 3  - 11  -  1  -  9  - |
  66. | -  -  -  -  -  -  -  - |
  67. |15  -  7  - 13  -  5  - |
  68.  
  69.       - Using the recursive formula :
  70.  
  71. Now that we done this little prep work, it is easy to deduce the induction relation defining these matrices
  72.  
  73. M(2n) = | 4 * M(n) + 0      4 * M(n) + 2 |
  74.         | 4 * M(n) + 3      4 * M(n) + 1 |
  75. for all n>=1
  76.  
  77. ----------------------------------------------------------------
  78. HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?
  79.  
  80. Here we are gonna focus on black&white ordered dithering.
  81. The dither matrix is tiled across the image. The bigger the matrix is, the better it will look.
  82. For each pixel of the image to modify, look up the member of the matrix covering it.
  83. Then we use the matrix as a threshold mask :
  84. if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.
  85.  
  86. ----------------------------------------------------------------
  87. Done !
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top