Quantisation and bit depth

Image analysis

About this lecture

  • A digital image loses information twice: where it is measured (sampling) and which values it can store (quantisation).
  • How many grey levels b bits give, and what rounding to them does: banding, and an error of predictable size.
  • Dithering: why adding noise on purpose makes an image look better.
  • How the error shrinks with each extra bit: about 6 dB per bit.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

Two ways to lose information

A camera measures a continuous brightness f(x, y) and stores a matrix of whole numbers:

Sampling Quantisation
what is cut positions: f is read only at grid points values: each reading is rounded to one of a few levels
controlled by pixel size, optics bit depth b
typical damage lost fine detail, aliasing banding in smooth regions, lost faint differences
treated in the Fourier unit this lecture
One brightness profile, grey. Left: sampling keeps its values only at grid points (blue stems). Right: quantisation keeps every position but rounds each value to one of 8 levels, giving a red staircase.

Bits and levels

With b bits a pixel can take 2^b values, 0, 1, \dots, 2^b - 1:

bits b 1 2 8 10 12 14 16
levels 2^b 2 4 256 1024 4096 16 384 65 536
The cell micrograph rounded to 1, 2, 3, 4 and 8 bits. With 1 and 2 bits only a few flat grey regions remain; at 4 bits smooth areas show contour steps; 8 bits looks continuous.
  • Spread over the same range (say black to white), the step between neighbouring levels is \Delta = \text{range}/(2^b - 1).
  • One more bit → twice as many levels → half the step.
  • 8 bits: the step is 1/255 of the range. 12 bits: 1/4095, a 16 times finer step.

Try it: quantise an image

What the rounding error looks like

  • Rounding moves each value to the nearest level, so the error is between −Δ/2 and +Δ/2.
  • In a smooth region the error is a sawtooth: the image jumps in steps of \Delta where it should change gradually. The eye sees the jumps as contour lines: banding (posterisation).
  • In a busy or noisy region, the error looks like random noise, evenly spread between −Δ/2 and +Δ/2.
  • Its typical size is about 0.29\,\Delta (root mean square). The demo’s readout shows it: at 4 bits, RMS error 0.0192 of the range.
  • One more bit halves the error. In decibels: about 6 dB per bit.

Top: a smooth ramp (grey) and its 3-bit rounding, a red staircase. Bottom: the error, a blue sawtooth between minus and plus half a step; its histogram on the right is flat.

The math: the error of rounding

Model the error e as uniform on [-\Delta/2, \Delta/2], with density 1/\Delta. Then

\mathbb E[e] = 0, \qquad \operatorname{Var}(e) = \int_{-\Delta/2}^{\Delta/2} e^2\,\frac{1}{\Delta}\,de = \frac{1}{\Delta}\left[\frac{e^3}{3}\right]_{-\Delta/2}^{\Delta/2} = \frac{\Delta^2}{12}.

So \text{RMS} = \Delta/\sqrt{12} \approx 0.289\,\Delta. Here the signal-to-noise ratio in decibels compares variances: \text{SNR} = 10\log_{10}(\sigma^2_\text{signal}/\sigma^2_\text{noise}) (SNR and PSNR in general: camera-noise unit). For a signal spread evenly over the full range R = (2^b - 1)\Delta, \sigma^2_\text{signal} = R^2/12, so

\text{SNR} = 10\log_{10}\,(2^b - 1)^2 \approx 20\,b\log_{10}2 \approx 6.02\,b \text{ dB}.

(For a full-scale sine wave, the textbook value is 6.02\,b + 1.76 dB.) The model holds when the signal changes by many steps across the image; on a perfectly flat region the error is one constant number, not noise.

Dithering: add noise before rounding

Rounding a smooth gradient gives bands, because many neighbouring pixels round the same way.

  • Random dither: add a little noise (up to ±Δ/2) to every pixel before rounding. A pixel 75 % of the way from one level to the next now rounds up 75 % of the time. On average, the grey value is right.
  • The bands turn into fine grain. The total error is a bit larger (a triangle instead of a box, RMS \times\sqrt2), but it no longer follows the image, and the eye averages grain away.
  • Error diffusion (Floyd–Steinberg): round a pixel, then pass its error on to the neighbours not yet visited. The local average stays right. This is how a printer shows grey with only black ink.
Smooth blobs, then the same image at 3 bits: plain rounding shows contour bands; random dither replaces them by grain; error diffusion gives a fine, even dot pattern.

Error diffusion as pseudocode

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)                 # the rounding error
    pass e on to the pixels not yet visited:
        7/16 to the right,  3/16 down-left,  5/16 down,  1/16 down-right
  • The weights sum to 1: no error is lost, so the local average stays right.
  • Random dither is one line instead: round v + \Delta\,(u - \tfrac12) with u uniform in [0, 1).

The math: why random dither is unbiased

Measure in units of \Delta, write a value as x = k + f with k an integer and 0 \le f < 1, and add u uniform on [-\tfrac12, \tfrac12). Then x + u rounds up to k + 1 exactly when u \ge \tfrac12 - f, which has probability f:

\mathbb E[\operatorname{round}(x + u)] = (1 - f)\,k + f\,(k + 1) = k + f = x.

No bias, for every x: the expected output is a straight line, not a staircase. The price: the error is 1 - f with probability f and -f with probability 1 - f, so \mathbb E[e^2] = f(1-f); averaged over f, \int_0^1 f(1-f)\,df = \tfrac16. So the error variance is \Delta^2/6, twice that of plain rounding.

Summary

  • b bits give 2^b levels; rounding to them makes an error of at most half a step (typically 0.29 steps): about 6 dB per bit.
  • In smooth regions the error shows as banding; dithering trades the bands for fine, unbiased noise.
  • Next: where the numbers come from. A camera counts electrons, and the count is noisy: how many bits are worth storing?