Simulating brightness levels – Why we use dithering

Dithering

Given a grayscale image, we want to replace each pixel with a set of dither pixels. For example: given a 2×2 dither matrix (4 elements) and a 3×3 pixel image (9 elements), our dithered output would be a 6×6 (36 elements) pixel image. Another example: given a 3×3 (9 elements) dither matrix and an 8×8 (64 elements) pixel image, the dithered output would be a 24×24 (576 elements) pixel image.

Ordered Dithering

Ordered dithering is similar to regular dithering except that we do not increase the size of the original image. So the number of output pixels after dithering is the same as the number of pixels in the original image. To keep the output size the same, we “slide” the dither matrix across the pixel image and only compare a single pixel to a single element of the dither matrix.

Dithering Steps

1. Given a dither matrix, we need to first find the number of “bins” to normalize our pixel values to.

This is the number of elements in a dither matrix + 1. This 2×2 dither matrix has 4 elements, so we will have 4 + 1 bins.  

02
13

Now we evenly distribute our image’s value ranges between the 5 bins. For this example our image is 8 bit grayscale, so there are 256 possible pixel values (0-255). 256/5 = 51.2 where 51.2 will be the bin width.

Bin 0 ∈ [0, 51.2]

Bin 1 ∈ (51.2, 102.4]

Bin 2  ∈ (102.4, 153.6]

Bin 3  ∈ (153.6, 204.8]

Bin 4  ∈ (204.8, 255]

We will now replace each pixel value with its corresponding bin number. An example 6×4 pixel image:

Original image

02552042051021
02530255154153
016600103102
072005152

Normalized image based on the 2×2 dither matrix

043410
040432
030021
010001

2. Now for each normalized pixel, we replace it with a 2×2 matrix following this rule: if the pixel value is greater than the element in the dither matrix, it is 1/ON/black. Otherwise it is 0/OFF/white.

For example: a normalized pixel value of 1:

Dither matrix from before

02
13

After applying dither matrix comparison

10
00

Another example: a normalized pixel value of 3:

After dither matrix comparison

11
10

After applying to all pixels, our original image has increased by a factor of the dither matrix’s size. This would be the final output:

Ordered Dithering Steps

1. Like with dithering, we will normalize the pixel values the exact same way.

Dither matrix

02
13

Original image (same as previous example)

02552042051021
02530255154153
016600103102
072005152

Normalized image based on the 2×2 dither matrix

043410
040432
030021
010001

2. Compare the dither matrix with the same number of elements from the normalized image. If the normalized pixel is greater than the dither matrix element it is 1/ON/black. Otherwise it is 0/OFF/white.

Let’s look at the first comparison:

Looking at the elements in yellow because our dither matrix is 2×2

Dither matrix

02
13

Output for this first section:

Element (0,0) – 0 (normalized matrix) is not greater than 0 (dither matrix). Output 0

Element (0,1) – 0 (normalized matrix) is not greater than 1 (dither matrix). Output 0

Element (1,0) – 4 (normalized matrix) is greater than 2 (dither matrix). Output 1

Element (1,1) – 4 (normalized matrix) is not greater than 3 (dither matrix). Output 1

01
01

The next comparison:

Output for this second section:

Element (0,0) – 3 (normalized matrix) is greater than 0 (dither matrix). Output 1

Element (0,1) – 0 (normalized matrix) is not greater than 1 (dither matrix). Output 0

Element (1,0) – 4 (normalized matrix) is greater than 2 (dither matrix). Output 1

Element (1,1) – 4 (normalized matrix) is not greater than 3 (dither matrix). Output 1

11
01

Third comparison

Output:

10
10

3. Continue sliding the dither matrix to the right until reaching the end, then move down to the next row and repeat. Each image pixel is only compared once with a dither matrix element.

Since we already compared everything element in the first 2 rows, our dither matrix slides to start on row 3

Output:

01
00

After sliding the dither matrix across our image, this is the final output:

011110
010110
010010
000000

3.5 It’s not always guaranteed that the dither matrix will be a multiple of the dimensions of the image. If the dither matrix does not perfectly overlap the image at its right or bottom edges, we can choose to compare just a portion of the dither matrix to the edge

Eg: the red is where we could potentially do a comparison with just a portion of the dither matrix

Compares with

or

And the same with the bottom edge.

or

Another example:

Image matrix

Dither matrix

or

Thinking furhter questions

Does a larger dithering matrix mean more grayscale levels can be displayed?

Do dither matrices need to be an n x n square matrix?

Can a dither matrix have repeating values? 

01
02

Eg: 

Given a color image, is dithering still possible? What would be the result?