Segmentation: decide for every pixel whether it belongs to the object or to the background.
Recall thresholding (the thresholding day), and why noise breaks it.
A segmentation as a labellingL, and a number E(L) that says how good it is.
Two terms: pixels should match an intensity model, and agree with their neighbours.
One weight \lambda trades the two off.
On a 1D line the best labelling can be found exactly by dynamic programming. On an image we need more: graph cuts, the next units (maximum flow, graph-cut segmentation).
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Thresholding (the thresholding day; choosing \tau: Otsu): every pixel brighter than a grey level \tau is object, L_p = 1 if I_p > \tau, else L_p = 0.
Each pixel is decided on its own. With noise the two histogram peaks overlap, and pixels in the overlap land on the wrong side: isolated specks of object in the background, holes in the object.
Real objects are mostly connected: a pixel usually has the same label as its neighbours. Thresholding cannot use that; this lecture adds it.
A synthetic disk with a thin arm (grey levels 80 and 170, noise SD 50), thresholded halfway at 125: specks everywhere.
A labelling and its energy
A segmentation is a labelling L = (L_1, \dots, L_n), L_p \in \{0, 1\}. Give each one a cost:
Likely intensities are cheap, unlikely ones expensive. The constant is the same for both labels, so it does not change which labelling wins.
With \lambda = 0 every pixel takes its cheaper label: L_p = 1 exactly when (I_p - \mu_1)^2 < (I_p - \mu_0)^2, that is when I_p > \tfrac12(\mu_0 + \mu_1).
So \lambda = 0 is thresholding, at the grey level where the two Gaussians cross.
Other models (a histogram, unequal widths, colour) change only D_p, not the method.
The two costs for \mu_0 = 80, \mu_1 = 170, s = 50 (constant dropped). They cross halfway, at 125.
The smoothness term
\lambda \sum_{(p,q)} w_{pq}\,[L_p \neq L_q]
An isolated wrong pixel has 4 disagreeing neighbours: flipping it saves 4\lambda of boundary and costs only its data difference |D_p(1) - D_p(0)|.
Small \lambda: close to thresholding; noise survives.
Large \lambda: boundaries are expensive; specks and holes vanish, but so do thin real structures, and corners get rounded.
Very large \lambda: no boundary at all, so everything gets one label.
The weights w_{pq} can depend on the image: make a boundary cheap where the image has an edge (next units).
The noisy disk, energy minimised for four values of λ. Orange: the true outline.
P(I \mid L) = \prod_p P(I_p \mid L_p), the intensity model, gives the data term.
P(L) \propto \exp\!\big(-\lambda \sum w_{pq} [L_p \ne L_q]\big), a prior that prefers few boundaries. This is a Markov random field: each label depends directly only on its neighbours.
Taking -\log turns the product into the sum E(L).
How do we find the minimum?
There are 2^n labellings. A 100\times100 image has 2^{10000} \approx 10^{3010}: trying them all is hopeless.
But the energy has structure: each term involves one pixel or two neighbours.
On a 1D chain (a line of pixels), that structure is enough for dynamic programming: walk along the line once, keeping for each pixel only the best energy so far for “this pixel object” and “this pixel background”. Exact, and fast. (The dynamic programming day makes this a general tool.)
On a 2D grid the neighbours form loops, and dynamic programming no longer works.
For two labels, E can still be minimised exactly: as a minimum cut in a graph. That is the next two units.
Pixels as nodes, neighbours joined by edges. A chain has no loops; a grid is full of them (orange).
Dynamic programming as pseudocode
best(1, ℓ) = D_1(ℓ) for ℓ = 0 and ℓ = 1
for k = 2, ..., n, for both labels ℓ:
stay = best(k − 1, ℓ)
switch = best(k − 1, the other label) + λ
best(k, ℓ) = D_k(ℓ) + the smaller of stay and switch (remember which)
L_n = the label with the smaller best(n, ℓ)
walk back from pixel n, following the remembered choices
Two numbers per pixel: work proportional to n, instead of trying 2ⁿ labellings.
Exact on a chain; on a grid the neighbours form loops.
The math: dynamic programming on a chain
On a chain, E(L) = \sum_k D_k(L_k) + \lambda \sum_k [L_k \ne L_{k+1}]. Let C_k(\ell) be the smallest energy of pixels 1..k with L_k = \ell: