Skip to content

Instantly share code, notes, and snippets.

@prakharaditya
Forked from MehdiNS/ordered_dithering.txt
Created December 30, 2021 18:51
Show Gist options
  • Save prakharaditya/e7606b075e23a90fb387f32dba5b18f9 to your computer and use it in GitHub Desktop.
Save prakharaditya/e7606b075e23a90fb387f32dba5b18f9 to your computer and use it in GitHub Desktop.
Ordered dithering explanation
Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image
INTRODUCTION
The idea behind it is simple : given two available values a and b, let's say black and white, the value x - what should be grayish - between a and b is simulated by mixing pixels of colors a and b.
To apply some ordered dithering on an image, we apply the same logic but in 2D by using a bayer matrix.
By turning the pixel on in a very specified order, the matrix creates the perception of continuous variation of color.
■ ■ | □ ■ | □ ■ | □ □ | □ □
■ ■ | ■ ■ | ■ □ | ■ □ | □ □
HOW TO CONSTRUCT A BAYER MATRIX ?
A bayer matrix is a dither matrix
Consecutive threshold are located far apart spatially, which gives the perception of smooth/continuous variation.
The values inside the matrix indicate how likely a pixel will be turned on.
0 --> most likely to turn on,
3 --> least likely to turn on
The whole idea behind ordered dithering is to distribute evenly the
expected range of gray levels throughout a matrix
The real trick is to come up with a way to design the matrix so that
each gray level will produce an even pattern in the output. For example,
a 50% gray level should come out as a checkerboard pattern. If we adopt
a recursive definition of the matrix, we get this for free.
Recursive
- By hand --> become quickly fastidious but is perfect to understand how the matrix works
- Using the
HOW TO APPLY ORDERED DITHERING ON AN IMAGE ?
Here we are gonna focus on black&white ordered dithering.
The dither matrix is repeated across the image. The bigger it is, the better it will look.
For each pixel of the image to modify, look up the member of the matrix covering it.
Then we use the matrix as a threshold mask : if the value of the pixel is superior to the one given by the matrix, the pixel is lit, if not it becomes black.
PSEUDO-CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment