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 depthb
typical damage
lost fine detail, aliasing
banding in smooth regions, lost faint differences
treated in
the Fourier unit
this lecture
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
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.
The math: the error of rounding
Model the error e as uniform on [-\Delta/2, \Delta/2], with density 1/\Delta. Then
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
(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.
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?