Exercise: quantisation and bit depth
Before this unit: Day 1: Images, filters and frequencies.
Goal
By the end of this exercise you should be able to:
- compute the number of levels and the step for a given bit depth, and quantise values by hand,
- predict where banding appears and explain what dithering changes,
- predict how the rounding error and the SNR change with the bit depth (about 6 dB per bit).
Do the pen-and-paper part first, then the interactive part. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
By hand
B1. How many grey levels do a 10-bit and a 14-bit sensor have? By about what factor is the step of a 12-bit image finer than that of an 8-bit image, if both cover black to white?
B2. Quantise the row of values (as fractions of the full range) 0.10,\quad 0.22,\quad 0.37,\quad 0.55,\quad 0.81 to 2 bits, i.e. to the levels 0, \tfrac13, \tfrac23, 1. Give the codes k \in \{0, 1, 2, 3\}, the stored values and the errors. What is the largest error that rounding can ever make here?
B3. A flat region has the value 0.25 everywhere and is quantised to 2 bits. (a) Without dither, what does every pixel become, and what is the error? (b) With random dither, a pixel rounds up to \tfrac13 with some probability and down to 0 otherwise. Predict that probability, and the average over many pixels.
B4. A smooth ramp from black to white is quantised to 6 bits and to 10 bits. Use the rule “about 6 dB per bit” to predict the SNR of the rounding in each case, and give the RMS error \Delta/\sqrt{12} as a fraction of the range. How many times smaller is the 10-bit error?
B1. 2^{10} = 1024 and 2^{14} = 16\,384 levels. The steps are 1/255 and 1/4095 of the range: the 12-bit step is 4095/255 \approx 16 times finer.
B2. Multiply by 3 and round: 0.30 \to 0, 0.66 \to 1, 1.11 \to 1, 1.65 \to 2, 2.43 \to 2. Stored values 0, \tfrac13, \tfrac13, \tfrac23, \tfrac23. Errors (stored − true): -0.100, +0.113, -0.037, +0.117, -0.143. Rounding never moves a value by more than half a step, \Delta/2 = \tfrac16 \approx 0.167.
B3. (a) 0.25 \times 3 = 0.75 rounds to 1: every pixel becomes \tfrac13, an error of +0.083 everywhere. The whole region is too bright by the same amount: no average can fix that. (b) The value is 75 % of the way from level 0 to level 1, so a pixel rounds up with probability 0.75: three quarters of the pixels become \tfrac13, one quarter 0. The average is 0.75 \cdot \tfrac13 = 0.25, the true value.
B4. About 6 \cdot 6 = 36 dB and 6 \cdot 10 = 60 dB (exactly 20\log_{10}63 = 36.0 and 20\log_{10}1023 = 60.2 dB). RMS error \frac{1}{63\sqrt{12}} = 0.0046 and \frac{1}{1023\sqrt{12}} = 0.00028 of the range: 1023/63 \approx 16 times smaller, 2^4 for 4 more bits.
Math questions
B5. (math) Let the rounding error e be uniform on [-\Delta/2, \Delta/2]. Show that \operatorname{Var}(e) = \Delta^2/12. A signal spread evenly over the full range R = (2^b - 1)\Delta has variance R^2/12: show that the SNR is 20\log_{10}(2^b - 1) \approx 6.02\,b dB, and evaluate it for b = 4.
B6. (math) Random dither: in units of \Delta, x = k + f with 0 \le f < 1 and u uniform on [-\tfrac12, \tfrac12). Show that \mathbb E[\operatorname{round}(x + u)] = x, and that the mean squared error, averaged over f uniform on [0, 1), is \Delta^2/6. By what factor is the RMS error larger than without dither?
B5. \mathbb E[e] = 0 by symmetry, so \operatorname{Var}(e) = \int_{-\Delta/2}^{\Delta/2} e^2 \tfrac{1}{\Delta}\,de = \tfrac{1}{\Delta}\cdot\tfrac{2}{3}\left(\tfrac{\Delta}{2}\right)^3 = \tfrac{\Delta^2}{12}. Then \text{SNR} = 10\log_{10}\frac{R^2/12}{\Delta^2/12} = 20\log_{10}(2^b - 1) \approx 20\,b\log_{10}2 = 6.02\,b dB. For b = 4: 20\log_{10}15 \approx 23.5 dB (6.02 · 4 = 24.1 is the large-b approximation). The demo’s ramp at 4 bits shows exactly 23.5 dB.
B6. x + u reaches the rounding threshold k + \tfrac12 when u \ge \tfrac12 - f, with probability f; then the output is k + 1, otherwise k. So \mathbb E = (1-f)k + f(k+1) = x. The error is 1 - f (probability f) or -f (probability 1-f): \mathbb E[e^2] = f(1-f)^2 + (1-f)f^2 = f(1-f), and \int_0^1 f(1-f)\,df = \tfrac12 - \tfrac13 = \tfrac16. In units of \Delta^2: \Delta^2/6, twice \Delta^2/12, so the RMS error is \sqrt2 \approx 1.41 times larger. (The demo: 0.0272 vs 0.0192 on the ramp at 4 bits, a ratio of 1.42.)
Explore
Quantising an image
The demo rounds an image to b bits and shows the rounding error (0 = grey, \pm\Delta = white/black) and its histogram. Images: a ramp, smooth blobs, and the 8-bit micrograph.
E1. On the ramp, step the bits from 8 down to 1. Compare the RMS error with \Delta/\sqrt{12} in the readout. How much does the SNR change per bit?
E2. Click 4 bits: banding, then 4 bits + dither. What happens to the contour lines, to the error histogram, and to the RMS error? Which picture looks closer to the original?
E3. Micrograph at 2 bits: compare no dither and error diffusion. Which has the larger RMS error? Which shows the cell better? What does that tell you about RMS error as a measure of image quality?
E4. Micrograph at 8 bits: the RMS error is 0. Why?
E1. They agree to the 4th decimal at every bit depth (e.g. 0.0192 at 4 bits, 0.0046 at 6 bits): on a ramp the error is exactly uniform. The SNR goes 23.5, 29.8, 36.0 dB for 4, 5, 6 bits: about 6 dB per bit.
E2. The contour lines disappear and become fine grain. The histogram changes from a box on \pm\Delta/2 to a triangle on \pm\Delta, and the RMS error grows from 0.0192 to 0.0273, about \sqrt2 times. Yet the dithered image looks closer to the original from a distance: its error has no structure, and the eye averages it away.
E3. Error diffusion has the larger RMS error (0.1200 against 0.0968), but it shows far more of the cell: with only 4 grey levels, the local average of the diffused pixels follows the true grey value. The RMS error compares pixels one by one and ignores what the eye does, which is to average neighbours. A small number is not always a better picture.
E4. The micrograph is stored with 8 bits already, so every value is exactly one of the 256 levels and rounding changes nothing. Quantising can only lose information; it cannot add the bits that were never measured.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in bitdepth/src/quantisation/exercise.rs.
C1. quantise(v, bits) (the code k), level(k, bits) (the stored value) and quantise_image(img, bits):
Δ = 1 / (2^b − 1)
k = round(v / Δ), kept between 0 and 2^b − 1
stored value = k · Δ
The check is B2.
C2. rms_error(a, b) and snr_db(clean, noisy): the root mean square difference, and 10\log_{10} of the variance of the clean image over the mean squared error. The check is B4, on a ramp with 65 536 different values.
C3. dither_random(img, bits, seed):
for every pixel: stored = level( quantise( v + Δ · (u − ½), bits ) ) # u uniform in [0, 1)
The checks are B3 (a flat 0.25 at 2 bits: three quarters round up, the mean stays 0.25) and B6 (the error is \sqrt2 times larger).
C4. error_diffusion(img, bits) (Floyd–Steinberg):
w = a copy of the image
for every pixel (x, y), row by row:
out(x, y) = nearest level to w(x, y); e = w(x, y) − out(x, y)
w(x+1, y) += 7/16 · e
w(x−1, y+1) += 3/16 · e
w(x, y+1) += 5/16 · e
w(x+1, y+1) += 1/16 · e # only where these pixels exist
The check: a flat 0.25 at 2 bits uses only 0 and ⅓, and every 8 × 8 block has a mean close to 0.25.
Then cargo run -p bitdepth -- quantisation and open the pictures in out/quantisation/.
C5. Compare the printed RMS error with \Delta/\sqrt{12} for 1 to 8 bits on the ramp. By how many dB does the SNR grow per bit?
C6. Look at blobs-4bit-plain.png, -dither.png and -diffusion.png. Which has the smallest RMS error? The program also compares the images after averaging each 5 × 5 neighbourhood (roughly what the eye does from a distance): which is best then?
C7. Do the same for the micrograph at 2 bits (cell-2bit-*.png). Which shows the cell best, and which has the smallest RMS error?
The model solution is bitdepth/src/quantisation/solution.rs in the download.
C5. They agree to the 5th decimal at every bit depth (0.01925 at 4 bits, 0.00458 at 6 bits): the ramp’s error is exactly uniform. The SNR goes 23.5, 29.8, 36.0, 42.1, 48.1 dB from 4 to 8 bits: about 6 dB per bit. At 1 bit it is 0.0 dB: the rounding error is as large as the signal.
C6. Plain rounding has the smallest RMS error (0.0194, against 0.0272 with dither and 0.0262 with error diffusion), but it shows clear contour bands. The dithered image has fine grain instead, and error diffusion an even finer, regular dot pattern. After a 5 × 5 average the order turns around: plain 0.0139, dither 0.0054, error diffusion 0.0023. The bands survive averaging; the grain does not.
C7. Plain rounding: RMS error 0.0968, flat patches of four greys, and the fine texture of the background is lost. Random dither: 0.1370, grainy but more of the texture shows. Error diffusion: 0.1200, the most detail; the cell and the nuclear envelope look closest to the original. The smallest RMS error belongs to the picture that looks worst, as in demo question E3.
Where this leads
Rounding is only one source of error in a pixel value. The next unit, camera noise, asks where the numbers come from: a camera counts electrons, the count is noisy, and that noise, not the rounding, usually decides how many bits are worth storing. It also defines the SNR and PSNR used in the rest of the course.