Histograms and a global threshold

Image analysis

About this lecture

Part I treated images as arrays of numbers. Part III asks which pixels belong to an object. The simplest answer: the bright ones.

  • A threshold turns an image into a binary mask: the first segmentation.
  • The histogram counts how many pixels have each grey level; it is a probability distribution.
  • The cumulative histogram gives the fraction of pixels below t, and the percentile threshold.
  • Bins, and what makes a threshold good: a deep valley between two peaks.
  • Noise fills the valley; smoothing first brings it back, at a price.

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

Which pixels are object?

Left: a synthetic image of bright blobs on a dark background. Right: the pixels brighter than t = 120, in white.

  • A segmentation splits the pixels into object (foreground) and background.
  • The simplest rule: object where I(x) > t. The result is a binary mask B(x) \in \{0, 1\}.
  • The ground truth is the true mask: known by construction for a synthetic image, drawn by hand for a real one.
  • The rule looks at each pixel alone: its grey level, nothing else.

The histogram

  • Sort the pixels into piles by grey level and count: h_i = number of pixels with grey level i.
  • Two peaks: background around 60, objects around 160. A threshold in the valley between them separates the two.
  • Divide by the number of pixels N: the normalised histogram p_i = h_i / N, the fraction of pixels with grey level i. The fractions add up to 1: it is a probability distribution of the grey level of a random pixel.

By hand: a 5 \times 5 image with grey levels 0–7 has counts h = (3, 8, 4, 1, 1, 1, 5, 2), so p_0 = 3/25 = 0.12.

The histogram as pseudocode

h = (0, 0, …, 0)                       # one count per grey level
for every pixel x:
    h[I(x)] = h[I(x)] + 1
running = 0
for t = 0, 1, …, L − 1:
    running = running + h[t]
    P(t) = running / N                 # cumulative histogram
  • One pass over the N pixels, then one over the L grey levels. Everything else in this unit (mask area, percentile threshold) is read off h and P.
  • The mask is then B(x) = 1 where I(x) > t: one comparison per pixel.

The math: the histogram as a distribution

With L grey levels i = 0, \dots, L-1 and N pixels:

p_i = \frac{h_i}{N}, \qquad \sum_{i} p_i = 1, \qquad \mu = \sum_i i\, p_i, \qquad \sigma^2 = \sum_i (i - \mu)^2 p_i = \sum_i i^2 p_i - \mu^2 .

These are the mean and variance of the grey level of a pixel chosen at random.

Two running sums will be used again and again:

\begin{aligned} P(t) &= \textstyle\sum_{i \le t} p_i && \text{(cumulative histogram)},\\ M(t) &= \textstyle\sum_{i \le t} i\, p_i && \text{(first moment)}. \end{aligned}

The 5 \times 5 example: \mu = 2.88, \sigma^2 = 5.79. One pass over the image (O(N)) builds the histogram; every quantity on the next slides then costs O(L).

The 5 × 5 example image with grey levels 0 to 7, and its normalised histogram with the mean 2.88 and one standard deviation (2.41) either side marked.

The histogram forgets where pixels are

Right: the same pixels, shuffled to random positions. Same grey levels, so the same histogram, and the same threshold.

  • A histogram method cannot tell an object from scattered bright noise: shape, size and neighbours are invisible to it.
  • Everything in this unit and the next uses the histogram only. Neighbours come back with local thresholds and hysteresis (two units on), and with graph cuts (day 27).

The cumulative histogram and the percentile threshold

Recall the cumulative histogram, the equalisation curve of contrast (day 3): P(t) = fraction of pixels with grey level \le t. Here it is the fraction that a threshold at t calls background.

  • The mask area is 1 - P(t): read it off for any t, without thresholding the image.
  • Percentile threshold: if you know that objects cover a fraction q of the image (say 2 % of the pixels are fluorescent spots), choose t with P(t) = 1 - q: “keep the brightest 2 %”.
  • A robust baseline when the object fraction is known and stable; useless when it varies from image to image.
  • A percentile threshold is a fixed threshold on the equalised image: equalisation maps t to P(t).

Example: in a 512 \times 512 image, “the brightest 2 %” means about 5243 pixels, wherever the valley is.

The cumulative histogram P(t) of the blob image rises in two steps, one per peak. A dashed line at height 0.75 meets the curve at t = 109: the threshold that keeps the brightest 25 %.

The blob image: objects are 25 % of the pixels, so P(t) = 0.75 gives t = 109, in the valley.

Bins

  • 8-bit images: 256 natural bins, one per grey level.
  • 16-bit containers (bit depth, day 3: a 12-bit camera uses only 0–4095 of 0–65535): 65 536 bins are mostly empty. Bin over the range of the data instead.
  • Too few bins merge the peaks and hide the valley; too many bins give a spiky histogram with few pixels per bin.

Try it: histogram and threshold

What makes a threshold good?

A bimodal histogram with a deep valley: every t in the valley gives nearly the same mask.

  • The mask area changes by the number of pixels at the threshold: 1 - P(t) drops by p_t when t moves up by one. In a deep valley p_t \approx 0: the area curve is flat, and the choice of t hardly matters.
  • A shallow valley: the area curve is steep, every grey level of t moves many pixels, and the result depends on a choice nobody can justify.
  • In the demo: the clean blobs lose 1.2 pixels per thousand per grey level in the valley, the noisy ones 4.4: almost four times as sensitive.

The mask area 1 − P(t) against t for the clean blobs (solid) and the noisy blobs (dashed). In the valley from 100 to 130 the clean curve is nearly flat; the noisy one keeps falling.

“Good” gets two precise meanings today: a criterion on the histogram (next unit), and agreement with the ground truth (the evaluation unit).

The math: the slope of the area curve

The mask area as a function of t is A(t) = 1 - P(t) = \sum_{i > t} p_i, so

A(t) - A(t+1) = p_{t+1}.

For continuous grey levels with density p(x): A'(t) = -p(t). The sensitivity of the mask to the threshold is the histogram at the threshold.

  • A threshold in a valley where p(t) = 0 is locally insensitive: small errors in t change nothing.
  • Uncertainty \delta t in the threshold changes the area by about p(t)\,\delta t.

Noise fills the valley

  • Recall camera noise (day 3): each pixel = true value + noise of standard deviation \sigma_n.
  • Each peak gets wider by the noise; the two peaks overlap, and the valley fills up (left, \sigma_n = 35).
  • Pixels in the overlap are on the wrong side of any t: isolated wrong pixels, salt-and-pepper in the mask.
  • Numbers: background 50, objects 110, \sigma_n = 20, t = 80 in the middle: 6.7\,\% of the pixels of each class land on the wrong side.

Smoothing first

Average each pixel with its neighbours (mean or Gaussian filter, from filtering, day 1) before thresholding.

  • Noise is independent from pixel to pixel: an average of n pixels has noise \sigma_n / \sqrt n. A 3 \times 3 mean divides it by 3.
  • Each peak narrows, and the valley comes back (right, same image after a 3 \times 3 mean).
  • The example above: \sigma_n = 20 \to 6.7, the wrong fraction drops from 6.7\,\% to 0.0003\,\%.
  • The median of the window is the non-linear alternative: it ignores a single outlier completely (median of 50, 52, 48, 51, 250, 49, 50, 53, 47 is 50; the mean is 72.2). Denoisers are compared on the deconvolution day; rank filters come back on the morphology day.

The price: edges blur. Pixels at the boundary become a mix of object and background (partial volume): they fill the valley again, and small or thin objects lose contrast.

Top: the blob image with noise of standard deviation 35 and its mask at t = 110, full of isolated wrong pixels. Bottom: the same image after a 3 × 3 mean, and its clean mask at the same threshold.

Top: noise \sigma_n = 35, t = 110: 10 % of the pixels wrong. Bottom: after a 3 \times 3 mean: 1.5 %.

The math: averaging noise

Let the noise values n_1, \dots, n_n be independent with variance \sigma^2. A weighted average \bar n = \sum_k w_k n_k with \sum_k w_k = 1 has

\operatorname{Var}(\bar n) = \sum_k w_k^2\, \sigma^2 .

  • Mean filter, w_k = 1/n: \operatorname{Var} = \sigma^2/n, the standard deviation drops by \sqrt n (by 3 for 3 \times 3).
  • \sum w_k^2 is smallest for equal weights: for a given number of pixels the mean removes the most noise. A Gaussian of width s pixels behaves like a mean over about 4\pi s^2 pixels.
  • The tail fraction: with class means \mu_0 < \mu_1, equal noise \sigma and t in the middle, the wrong fraction per class is 1 - \Phi\!\left(\tfrac{\mu_1 - \mu_0}{2\sigma}\right); smoothing by \sqrt n multiplies the argument by \sqrt n.

Try it: noise and smoothing

Summary

  • A threshold turns an image into a binary mask: object where I(x) > t. Each pixel is judged alone.
  • The histogram h_i counts pixels per grey level; p_i = h_i/N is a probability distribution. It forgets where the pixels are.
  • The cumulative histogram P(t) is the background fraction; 1 - P(t) is the mask area. Percentile threshold: P(t) = 1 - q.
  • Bins: 256 for 8-bit; for 16-bit data bin over the data range.
  • A good threshold sits in a deep valley: the mask area hardly changes with t.
  • Noise widens the peaks and fills the valley; smoothing narrows them (noise /\sqrt n) but blurs the edges.