Contrast and intensity transforms

Image analysis

About this lecture

The last unit showed a 12-bit image on an 8-bit screen with a window: a straight line from raw values to grey levels. This unit bends that line.

  • Point operations: one curve, applied to every pixel; stored as a lookup table.
  • Shaped curves: gamma and log. What the slope of a curve does to contrast and noise.
  • Curves chosen by the data: histogram equalisation, and why its result is only roughly flat.
  • Local curves: CLAHE. Curves chosen by another image: histogram matching.
  • What all of this does to measurements: transform for looking, measure on the raw data.

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

Recall: a window is a curve

From 12- and 16-bit images: a window [\ell, h] maps raw values to the 256 display levels by a straight line, clipped at black and white.

  • Draw it as a curve: raw value on the x axis, display level on the y axis, the histogram behind it.
  • Raw 1653 (the middle of the window) becomes display level 128.

Every transform in this unit is another curve on this plot.

Histogram of the synthetic 12-bit fluorescence image (log counts, grey) with the window from 130 to 3176 drawn on top as a blue line: zero below 130, a straight ramp, 255 above 3176. An orange dashed path takes raw value 1653 up to the line and across to display level 128.

Point operations

A point operation computes each new value from the old value of the same pixel only: s = T(v), the same function T everywhere. Position and neighbours do not matter: the whole operation is its curve.

  • Examples: the window, the negative (s = 255 - v for 8 bits), contrast stretching, clipping, a threshold, adding a constant.
  • Not point operations: blurring, the median filter, anything that looks at neighbours.

A 3 by 3 patch of grey values with only the centre pixel, 44, highlighted; an arrow to the curve s equals 255 times the square root of v over 255, where 44 maps to 106; an arrow to the output patch with the centre 106. The eight neighbours are greyed out because they are not used.

Lookup tables

An integer image has only L possible values: 256 for 8 bits, 4096 for 12 bits. So compute the curve once per value and look the result up.

raw v 0 1 2 … 44 … 255
T(v) 0 16 23 … 106 … 255
  • Cost: L evaluations of T (256 or 4096), then one table read per pixel, for any number of pixels.
  • A display program changes the table, never the image: that is why a window can be dragged live.
  • Two curves in a row are one table: T_2(T_1(v)) is again a table of L entries.
  • Image libraries call this a LUT (lookup table).

Reading a curve: the slope is the contrast

Two neighbouring raw values v and v + 1 end up T'(v) apart: the slope is the contrast gain.

  • Slope > 1: values move apart. More contrast, and the noise is amplified by the same factor.
  • Slope < 1: values move together, detail is compressed.
  • Slope 0 (clipping, flat parts): values merge; the information is gone.
  • A curve that rises everywhere (monotone) keeps the order: brighter stays brighter. A falling part swaps it.

The curve s equals the square root of v, with eight equal-width coloured strips of input values below it and their images as strips on the output axis to the left. Near zero the curve is steep and the output strips are wide, so the output density is thin; at larger v the curve is flat, the strips are narrow and the density piles up.

The math: a curve reshapes the histogram

Let the intensity be continuous with density p_X, and T rising and differentiable. The pixels in [v, v + dv] land in [s, s + ds] with ds = T'(v)\,dv; their number does not change:

p_S(s)\,ds = p_X(v)\,dv \quad\Rightarrow\quad p_S(s) = \frac{p_X(v)}{T'(v)}, \qquad s = T(v).

  • Slope > 1 spreads the density thin; slope < 1 piles it up (the figure on the previous slide).
  • Composition: (T_2 \circ T_1)' = T_2'(T_1(v))\,T_1'(v): gains multiply.
  • A point operation can be undone exactly when it is one-to-one on the values that occur (strictly rising or strictly falling there). A flat part merges values; a curve that rises and then falls sends two inputs to one output.

Gamma

Scale the values to [0, 1] and raise them to a power: s = v^\gamma.

  • γ < 1 brightens the darks: it stretches them and compresses the brights. γ > 1 does the reverse; γ = 1 is the identity. Black and white stay fixed.
  • γ = 0.5: 0.04 → 0.2 and 0.81 → 0.9. Slopes there: 2.5 and 0.56.
  • Screens and image files also use a power law, for storage (colour day, linear RGB): a different reason, same shape.

Left: the curves v to the power gamma for gamma 0.4 (above the diagonal), 1 (the diagonal) and 2.5 (below). Right: the synthetic 12-bit fluorescence image shown with each: at gamma 0.4 the dim cytoplasm and the dim cell at the lower right are clearly visible and the background is grey; at gamma 2.5 only the bright nuclei remain.

Log: equal ratios, equal steps

s = c \log(1 + v): a ratio becomes a step. Every factor of 10 gets the same number of display levels.

  • 12 bits on 8: c = 255/\ln 4096 \approx 30.7. Then 10 → 74, 100 → 141, 1000 → 212, 4095 → 255: about 71 levels per decade.
  • Uses: images spanning orders of magnitude (dim cells next to bright spots, astronomy), and Fourier spectra, always shown as \log(1 + |X|) (Fourier unit).
  • Too much log makes the background noise as prominent as the objects.

Left: the curve c log of 1 plus v from 12 bits to 8 bits on a logarithmic raw axis, with 10, 100 and 1000 marked at display levels 74, 141 and 212, about 71 levels per decade. Right: the Fourier magnitude of the micrograph shown linearly (black except one dot in the centre) and as log of 1 plus the magnitude (a grey cloud with structure).

The math: gamma, log and noise

  • Slope of gamma: \dfrac{d}{dv}v^\gamma = \gamma\,v^{\gamma - 1}. For γ < 1 it goes to ∞ as v \to 0: gamma curves amplify the dark noise most.
  • Log: \log(a v) = \log a + \log v. A multiplicative factor (exposure time, illumination) becomes an additive shift.
  • Noise after a point operation (the delta method, first-order Taylor around the mean μ): T(X) \approx T(\mu) + T'(\mu)(X - \mu) \quad\Rightarrow\quad \operatorname{SD}\big(T(X)\big) \approx |T'(\mu)|\,\operatorname{SD}(X).
  • Poisson counts have \operatorname{SD}(X) = \sqrt\mu. With T(x) = 2\sqrt x: T'(\mu) = 1/\sqrt\mu, so \operatorname{SD} \approx 1 whatever the brightness: the variance-stabilising transform pointed to in camera noise. It is a gamma of ½.

Try it: tone curves

Stretching 8 bits leaves a comb

A curve maps each input value to one output value. It can merge levels, but it can never split them.

  • 8-bit data using values 6 … 70, stretched ×4 to fill 0 … 255: only 64 output levels are used, every 4th. The histogram is a comb; smooth gradients show bands (recall quantisation).
  • The same stretch on the 12-bit data: 1025 raw values for 256 levels, 253 used. Smooth.

Rule: do contrast work on the 12/16-bit or float data; convert to 8 bits last, for display.

Four histograms. Top: 8-bit data with values 6 to 70, and after a times-4 stretch, a comb in which only every fourth display level is used, 64 in all. Bottom: the same image as 12-bit data, values 96 to 1120, and after the same stretch, a smooth histogram using 253 levels.

Let the histogram choose the curve

Where should a curve be steep? Where the pixels are: give the most contrast to the values that occur most often, and none to values that do not occur.

  • Make the slope proportional to the histogram: steep at the peaks, flat in the empty tails.
  • The curve whose slope is the histogram is its running sum: the cumulative histogram.

This is histogram equalisation.

Histogram of the synthetic foggy scene (grey), concentrated between about 80 and 175, with the equalisation curve on top: flat at zero below the histogram, steep where the histogram is high, flat at 255 above it; the dashed diagonal for comparison.

The cumulative histogram is the mapping

The cumulative histogram P(v): the fraction of pixels with value \le v. It rises from 0 to 1, steeply where there are many pixels.

Equalisation uses it as the curve: s = \operatorname{round}\big((L - 1)\,P(v)\big).

  • Micrograph, L = 256: 8 % of the pixels are ≤ 80, so 80 → level 21; 40 % are ≤ 150, so 150 → level 101.
  • A pixel’s new value says “brighter than this fraction of the image”.
  • Result: about the same number of pixels at every output level.

The cumulative histogram of the micrograph as a red S-shaped curve from 0 to 1, with a second axis from 0 to 255 on the right. Two arrows: input 80 goes up to P equals 0.08 and across to output level 21; input 150 goes up to P equals 0.40 and across to level 101.

Worked example: 8 levels by hand

A 64-pixel image with 8 levels (L = 8):

level v 0 1 2 3 4 5 6 7
count 6 22 18 8 5 3 1 1
cumulative 6 28 46 54 59 62 63 64
7c/64 0.66 3.06 5.03 5.91 6.45 6.78 6.89 7
new level 1 3 5 6 6 7 7 7

Output counts at levels 0 … 7: 0, 6, 0, 22, 0, 18, 13, 5. A flat histogram would have 8 at every level.

Why only approximately flat

All pixels with the same input value get the same output value. The 22 pixels at level 1 stay together: they can be moved, not split.

  • Levels can merge (3 and 4 here, by rounding), and output levels stay empty (0, 2, 4).
  • The output has at most as many distinct values as the input.
  • What is close to straight is the output’s cumulative histogram.
  • The micrograph: 256 levels in, 195 used out.

Left: the output histogram of the worked example, bars 0, 6, 0, 22, 0, 18, 13, 5 against a dashed flat line at 8. Middle: its cumulative histogram as a red staircase close to the dashed diagonal. Right: the histogram of the equalised micrograph: spiky bars with gaps around the flat level, 195 of 256 levels used.

The math: equalisation

  • Continuous case. If X has a continuous, strictly rising cumulative distribution F, then Y = F(X) is uniform on [0, 1]: \;P(Y \le y) = P\big(X \le F^{-1}(y)\big) = F\big(F^{-1}(y)\big) = y (the probability integral transform). With T = F the slope is T' = p_X, which is the “slope ∝ histogram” rule; the output density is p_X / T' = 1.
  • Discrete case. s_k = \operatorname{round}\big((L - 1)F(k)\big) with F(k) = c_k / N. The output count at a level is a sum of input counts, so a level with h(k) > N/L stays above N/L; used output levels ≤ used input levels. Variant: s_k = \operatorname{round}\big((L - 1)\frac{c_k - c_{\min}}{N - c_{\min}}\big) maps the darkest occurring value to 0.
  • Only ranks matter. For a strictly rising g: F_{g(X)}(g(v)) = P(g(X) \le g(v)) = F_X(v). Equalising g(X) gives the same image as equalising X.
  • Exact equalisation: order all N pixels, break ties (at random, or by the local mean), give ranks r the level \lfloor rL/N \rfloor. Exactly flat, at the price of an invented order among equal pixels.

Try it: equalisation

Where global equalisation fails

One curve for the whole image, steep where most pixels are. Usually that is the background.

  • The 12-bit image: the flat background is about half of all pixels, so it gets about half of the output levels. Its noise is amplified: SD 0.8 → 27.5 grey levels in the green box.
  • Small objects with few pixels get little contrast; bright and dark regions compete for one curve.
  • Rough rule: 40 % of the pixels in 20 of 256 levels gives a slope \approx 255 \cdot 0.4/20 \approx 5; noise of SD 3 becomes about 15.

The synthetic 12-bit fluorescence image with the window 130 to 3176 (dark background, four cells), the same image after global equalisation (grainy, noisy grey background, cells washed out to white), and the equalisation curve over the histogram: it jumps from 0 to about 170 within the narrow background peak and then rises slowly. Title: noise SD in the green box 0.8 to 27.5.

One curve per tile

Adaptive equalisation: cut the image into tiles (say 8 × 8) and equalise each tile with its own histogram. Each tile on its own gives seams at the tile borders.

  • Instead, each pixel blends the curves of the four nearest tile centres, weighted by distance (bilinear interpolation, images unit).
  • The curve depends on where the pixel is: the same raw value can become different display values in different places.

Left: the 12-bit fluorescence image with a 4 by 4 tile grid and one pixel joined to its four nearest tile centres, with bilinear weights 0.26, 0.05, 0.59 and 0.10. Middle: each tile equalised with its own curve: visible square seams at the tile borders. Right: the blend of the four nearest curves with clipping at beta 3: no seams, every cell has local contrast.

CLAHE: clip the histogram first

In a flat tile the histogram is one narrow peak, so its curve is nearly a step: noise explodes. Fix: clip every bin at β times the average count, spread the clipped excess evenly over all bins, then accumulate.

  • The slope is the histogram, so the clip limit is the largest contrast gain: about β×.
  • β = 1: a straight line (no change). β → ∞: plain adaptive equalisation.
  • Two knobs: tile size (larger than the objects) and clip limit (how much contrast, and noise; typical β = 2 … 4).
  • Contrast-Limited Adaptive Histogram Equalisation: image libraries call it CLAHE.

Left: the histogram of a background-only tile of the faint-cell scene on a log axis: a narrow peak of a few bins up to about 1400 counts, a dashed red clip line at 3 times the average count, and the clipped histogram: the peak cut at the line and the excess spread as a constant level over all 64 bins. Right: the two resulting curves: unclipped, a near-step from 0 to 255 within a few bins; clipped, a gentle curve close to the straight line.

CLAHE as pseudocode

for each tile:
    h = histogram of the tile
    repeat: cut every bin at β · N / L, spread the cut-off part evenly over all bins
    curve(v) = (L − 1) / N · (h(0) + h(1) + … + h(v))     # equalisation of the tile
for every pixel (x, y) with value v:
    find the four nearest tile centres and their bilinear weights w1 … w4
    out(x, y) = w1 · curve1(v) + w2 · curve2(v) + w3 · curve3(v) + w4 · curve4(v)
  • With one tile and no clipping, this is plain histogram equalisation.
  • With β = 1 every clipped histogram is flat and the curve is a straight line: nothing happens.

The math: CLAHE

For one tile with N pixels and B bins, histogram h:

  • Clipped histogram: \tilde h(k) = \min\big(h(k),\, \beta N/B\big) + E/B, with E the clipped excess.
  • Tile curve: T(v) = \dfrac{L - 1}{N}\sum_{k \le v}\tilde h(k).
  • Slope (per bin, relative to the straight line, whose slope is (L - 1)/B): \dfrac{B\,\tilde h(v)}{N} \le \beta + \dfrac{E}{N}. After one spreading a bin can end slightly above the limit; repeating clip-and-spread until no bin exceeds it makes the bound exactly β.
  • Interpolation: T(v; \mathbf x) = \sum_{i=1}^{4} w_i(\mathbf x)\,T_i(v) with bilinear weights w_i \ge 0, \sum w_i = 1. Each T_i rises in v, so the blend rises: at a fixed position the order is kept. Between positions \mathbf x and \mathbf y nothing relates T(\cdot; \mathbf x) to T(\cdot; \mathbf y).

Try it: CLAHE

Histogram matching

Make an image’s histogram look like a target: another image, a reference, a chosen shape. For each input value v, read its fraction F(v) in the source’s cumulative histogram; find the target value with the same fraction in G.

  • Foggy scene to micrograph: v = 130 has F = 0.60; the micrograph value with 60 % of its pixels below is 197.
  • Equalisation is matching to a flat target.
  • Uses: tiles of a mosaic or frames of a time series that should look alike; input for a model trained on another instrument.
  • It also erases real differences: match a treated image to a control and the treatment is gone.

Left: the cumulative histogram F of the foggy scene, rising steeply between about 90 and 170; at v = 130 it is 0.60. Right: the cumulative histogram G of the micrograph, rising slowly; an arrow at height 0.60 goes across to the curve and down to value 197.

The math: matching

  • The matching curve is T = G^{-1} \circ F, with the generalised inverse G^{-1}(u) = \min\{v' : G(v') \ge u\} (needed when G has flat parts or jumps).
  • Continuous case: U = F(X) is uniform (previous math slide), and for uniform U, G^{-1}(U) has distribution G: P(G^{-1}(U) \le t) = P(U \le G(t)) = G(t). So T(X) has exactly the target distribution.
  • Discrete case: the same rounding problem as equalisation, plus jumps of G: a whole input level lands on one target level, so the match is approximate.
  • Equalisation: G uniform on \{0, \dots, L - 1\}, G^{-1}(u) \approx (L - 1)u.
  • Worked: counts 6, 22, 18, 8, 5, 3, 1, 1 to the target 0, 0, 8, 8, 16, 16, 8, 8: cumulative 6, 28, 46, \dots against 0, 0, 8, 16, 32, 48, 56, 64 gives the mapping 2, 4, 5, 6, 7, 7, 7, 7.

What each transform does to measurements

transform order of grey values kept? differences, ratios kept? same raw → same output everywhere? comparable between images?
linear window yes yes (after removing the offset; not where clipped) yes yes, with the same window
gamma, log yes no (log: ratios become differences) yes yes, with the same curve
global equalisation yes no yes no: the curve depends on the image
CLAHE only locally no no no
histogram matching yes no yes no: differences removed by construction

Two cells, raw means 300 and 600 on a background of 100 (12 bit): background-subtracted ratio \frac{600 - 100}{300 - 100} = 2.5. After γ = 0.5 on v/4095, the same computation gives 1.98.

Try it: display vs data

The math: measurements under a point operation

  • Only affine maps commute with averaging: \overline{T(v)} = T(\bar v) for all images only if T(v) = av + b.
  • For a concave T (γ < 1, log), Jensen’s inequality: \overline{T(v)} \le T(\bar v). Pixels 100 and 900: \sqrt{500} = 22.4 but (\sqrt{100} + \sqrt{900})/2 = 20.
  • Ratios after background subtraction, \frac{\bar a - \bar b}{\bar c - \bar b}, are kept by every affine T(v) = \alpha v + \delta (α > 0): \delta cancels in the differences, \alpha in the ratio. A window is affine only where nothing clips; a curve with T'' \ne 0 changes the ratio.
  • Rank statistics commute with any rising T: \operatorname{median}(T(X)) = T(\operatorname{median}X), percentiles, the Spearman correlation (recall colocalisation), and any threshold chosen by a percentile (“the brightest 10 %”: the same mask).
  • Otsu’s threshold does not simply move with T: it depends on class means and variances, which a non-linear T changes (exercise).

What to do

  1. Segment or detect on whatever helps: a gamma, a log, CLAHE. Then measure on the raw (linear) data inside the masks.
  2. Show images you compare with the same display settings, and say so in the caption.
  3. Report non-linear display adjustments (gamma, log, equalisation, CLAHE) in the figure legend. Journal image-integrity guidelines ask for the same.
  4. Normalise measurements (per well, per batch, against controls: intensity features), not pixels.
  5. CLAHE makes an image look evenly lit; it does not make it measurably even. Uneven illumination is corrected by dividing or subtracting a background (local thresholds).

The denoising unit asks the same question for restored images.

Summary

  • A point operation is a curve s = T(v), applied to every pixel; stored as a lookup table.
  • The slope is the contrast gain, for signal and noise alike; flat parts merge values; a rising curve keeps the order. A curve cannot create levels: work on high-bit data, convert to 8 bits last.
  • Gamma brightens (γ < 1) or darkens (γ > 1); log gives every decade the same number of levels.
  • Equalisation: the cumulative histogram is the curve. Only roughly flat for images, because equal values move together.
  • CLAHE: one clipped curve per tile, blended between tiles; the clip limit caps the gain.
  • Matching: G^{-1}\circ F; equalisation is matching to flat.
  • Transform for looking and for finding objects; measure on the raw data. Never measure intensities on an equalised, CLAHE or matched image.