Exercise: segmentation as energy minimisation
Before this unit: Day 12: Thresholding.
See also: Day 13: Morphology; Day 15: Watershed.
Goal
By the end of this exercise you should be able to:
- compute the energy of a labelling: data term plus \lambda times the boundary,
- explain what \lambda trades off, and predict when a structure appears or disappears,
- derive the threshold from a two-Gaussian intensity model,
- find the best labelling of a chain by dynamic programming.
Do the pen-and-paper part first, then the interactive part.
Background
| Idea | What it means |
|---|---|
| Labelling | L_p = 1 (object) or 0 (background) for every pixel p |
| Data term | D_p(\ell): the cost of label \ell at pixel p; here D_p(\ell) = (I_p - \mu_\ell)^2 / 2s^2, i.e. -\log of a Gaussian |
| Smoothness term | \lambda \sum_{(p,q)} w_{pq}[L_p \ne L_q]; in this exercise w_{pq} = 1, so it is \lambda times the number of boundaries |
| Energy | E(L) = data term + smoothness term; the segmentation is the labelling with the smallest E |
| Thresholding | the minimum for \lambda = 0: every pixel takes its cheaper label |
| Dynamic programming | on a chain: C_k(\ell) = D_k(\ell) + \min\big(C_{k-1}(\ell),\, C_{k-1}(1-\ell) + \lambda\big) |
Labellings of a row are written as strings, for example 11010.
By hand
A 1 \times 5 image I = (3, 3, 1, 3, 0) with \mu_0 = 0 and \mu_1 = 3. For B1–B3 take D_p(\ell) = (I_p - \mu_\ell)^2 (that is, 2s^2 = 1).
B1. Write down D_p(0) and D_p(1) for the five pixels. Which labelling does thresholding give?
B2. Compute the data term, the number of boundaries and the energy of the four labellings (a) 11010, (b) 11110, (c) 11111, (d) 00000, for \lambda = 1 and for \lambda = 5. Which of the four is best in each case? Find the values of \lambda at which the winner changes.
B3. Fill in the dynamic-programming table C_k(0), C_k(1), k = 1, \dots, 5, for \lambda = 2. What is the minimum energy, and which labelling achieves it? (Keep track of which choice wins at each step, and trace back from the end.)
B4. The demo below uses \mu_0 = 60, \mu_1 = 160 and s = 30, so D_p(\ell) = (I_p - \mu_\ell)^2 / 1800. (a) At which grey level is the threshold? (b) Its thin structure consists of two samples with grey levels 140 and 221, with background on both sides. Compute D(0) and D(1) for both. Above which \lambda does the optimal labelling drop the structure?
B5. In a 2D image (4-neighbours, w_{pq} = 1), a single pixel of grey level 120 is surrounded by background pixels that are clearly background. Use the model of B4. What does thresholding do with it? Above which \lambda does the optimal labelling call it background?
B6. Predict: suppose we know the object is rare, only 20% of the pixels, and include that in the data term, D_p(\ell) = -\log\big(\pi_\ell\, P(I_p \mid \ell)\big) with \pi_1 = 0.2, \pi_0 = 0.8. Does the threshold for \lambda = 0 move up or down? Why does that make sense?
B7 (math). Show that with the prior of B6 the threshold for \lambda = 0 is \tfrac12(\mu_0 + \mu_1) + \dfrac{s^2 \log(\pi_0/\pi_1)}{\mu_1 - \mu_0}, and evaluate it for the model of B4.
B1. D(0) = I^2 = 9, 9, 1, 9, 0 and D(1) = (3 - I)^2 = 0, 0, 4, 0, 9. Thresholding takes the cheaper label at every pixel: 11010 (the dark pixel 3 and pixel 5 are background).
B2.
| labelling | data | boundaries | E at \lambda = 1 | E at \lambda = 5 |
|---|---|---|---|---|
| (a) 11010 | 0+0+1+0+0 = 1 | 3 | 4 | 16 |
| (b) 11110 | 0+0+4+0+0 = 4 | 1 | 5 | 9 |
| (c) 11111 | 0+0+4+0+9 = 13 | 0 | 13 | 13 |
| (d) 00000 | 9+9+1+9+0 = 28 | 0 | 28 | 28 |
At \lambda = 1 the thresholded labelling (a) is best; at \lambda = 5, (b): the dark pixel is filled in. (a) and (b) tie when 1 + 3\lambda = 4 + \lambda, at \lambda = 1.5; (b) and (c) tie when 4 + \lambda = 13, at \lambda = 9. So: thresholding for \lambda < 1.5, 11110 for 1.5 < \lambda < 9, all object above 9. (Checking all 32 labellings confirms that no other labelling does better at \lambda = 1 or 5.)
B3. C_1 = (9, 0). Then, writing C_k = \big(C_k(0), C_k(1)\big): C_2(0) = 9 + \min(9, 0 + 2) = 11, C_2(1) = 0 + \min(0, 9 + 2) = 0; C_3(0) = 1 + \min(11, 0 + 2) = 3, C_3(1) = 4 + \min(0, 11 + 2) = 4; C_4(0) = 9 + \min(3, 4 + 2) = 12, C_4(1) = 0 + \min(4, 3 + 2) = 4; C_5(0) = 0 + \min(12, 4 + 2) = 6, C_5(1) = 9 + \min(4, 12 + 2) = 13. The minimum is C_5(0) = 6. Trace back: C_5(0) came from C_4(1); C_4(1) from C_3(1); C_3(1) from C_2(1); C_2(1) from C_1(1). The labelling is 11110, with data 4 and one boundary: 4 + 2 = 6, as in B2.
B4. (a) \tfrac12(60 + 160) = 110. (b) Grey level 140: D(0) = 80^2/1800 = 3.556, D(1) = 20^2/1800 = 0.222. Grey level 221: D(0) = 161^2/1800 = 14.401, D(1) = 61^2/1800 = 2.067. Labelling both as object instead of background saves (3.556 + 14.401) - (0.222 + 2.067) = 15.667 in data, and costs 2 boundaries. The structure is dropped when 2\lambda > 15.667, i.e. for \lambda > 7.83.
B5. D(0) = 60^2/1800 = 2, D(1) = 40^2/1800 = 0.889: 120 > 110, so thresholding calls it object, a speck. Calling it background instead costs 2 - 0.889 = 1.111 more data, and removes 4 boundaries, 4\lambda. The optimal labelling makes it background for \lambda > 1.111/4 = 0.278.
B6. The threshold moves up, towards the brighter, rarer class: the object label now carries an extra cost -\log 0.2 > -\log 0.8, so a pixel must look more clearly like an object before we call it one. For the model of B4 it moves from 110 to 122.5 (B7).
B7. Set D_p(0) = D_p(1): \frac{(I - \mu_0)^2}{2s^2} - \log\pi_0 = \frac{(I - \mu_1)^2}{2s^2} - \log\pi_1. Multiply by 2s^2 and use (I - \mu_0)^2 - (I - \mu_1)^2 = 2I(\mu_1 - \mu_0) - (\mu_1^2 - \mu_0^2): then 2I(\mu_1 - \mu_0) = (\mu_1^2 - \mu_0^2) + 2s^2\log(\pi_0/\pi_1), and dividing by 2(\mu_1 - \mu_0) gives the formula. For B4: 110 + 900 \cdot \log 4 / 100 = 110 + 12.5 = 122.5.
Explore
The demo shows a noisy line profile of 80 samples across a cell (samples 13–50) and a thin bright structure (samples 65–66). The dashed lines are the two levels \mu_0 and \mu_1, the dotted line the threshold halfway between them. Below the plot, three strips: the true labels, thresholding (\lambda = 0), and the labelling with the smallest energy for the chosen \lambda, computed exactly by dynamic programming. Black dots mark wrong samples. The table gives the energy of both labellings. The \lambda slider is finer at small values.
E1. Press λ = 0: threshold. How many samples are wrong, and how many boundaries are there? Then press λ = 3. Compare the data terms of the two labellings: which is larger, and why is the optimal labelling still the better one?
E2. Starting from \lambda = 0, move the slider up slowly. At which \lambda do all wrong samples disappear? At which \lambda is the thin structure lost? Compare with B4.
E3. Press too smooth (\lambda = 100). How many boundaries are left, and which samples are now wrong? Why does the background on the left become object, rather than the cell becoming background?
E4. Set the noise to 60 (with \lambda = 3). How many samples are wrong with thresholding, and with the optimal labelling? Try other values of \lambda: can you find one that gets most of the profile right? What do you lose?
E5. Reset, then lower \mu_1 to 110 (keep \lambda = 3). What happens to the threshold and to the errors? What does this say about the intensity model?
E1. Thresholding: 3 wrong samples (sample 45, a dark sample inside the cell, becomes background; samples 52 and 74, bright noise in the background, become object), 10 boundaries, energy 40.4 (all data, since \lambda = 0). At \lambda = 3: data 47.3 for the optimal labelling against 40.4 for thresholding: it pays more in data, because some samples now take their more expensive label. But it has only 4 boundaries instead of 10, so its total is 47.3 + 12 = 59.3, against 40.4 + 30 = 70.4 for thresholding. And it has 0 wrong samples.
E2. On the slider: at \lambda = 0.5 one error is gone (2 wrong, 8 boundaries), at \lambda = 1.5 only 1 is wrong, and from \lambda = 1.75 there are no wrong samples (4 boundaries). The thin structure survives up to \lambda = 7.75 and is lost at \lambda = 8 (2 boundaries, 2 wrong samples): B4 predicted the switch at 7.83.
E3. One boundary is left, between samples 50 and 51, and 14 samples are wrong: the 12 background samples on the left and the 2 samples of the thin structure. One boundary must stay, because labelling everything background (data 258.0) or everything object (248.9) costs more than 122.7 + 100. With one boundary, the cheaper way is to give the 12 dark samples on the left the object label (they cost 59.7 more data) and save the boundary at the left edge of the cell (100). This happens for \lambda above about 59.7 (on the slider from 59.75).
E4. With noise 60, thresholding gets 21 samples wrong (31 boundaries); the optimal labelling at \lambda = 3 gets 9 wrong (9 boundaries). The best on the slider is \lambda = 6.25 to 8.75: 5 wrong samples and 4 boundaries. The cell and the thin structure are both found, but the boundaries are off: the cell starts one sample early and ends three samples late, and the thin structure is one sample too wide. From \lambda = 9 the thin structure is gone (6 wrong). With this much noise, the smoothness term can remove the specks, but it cannot place the boundaries exactly: the data near a boundary are too noisy to say where it is.
E5. The threshold moves to \tfrac12(60 + 110) = 85. Thresholding now gets 10 samples wrong (bright noise in the background passes the low threshold), and the optimal labelling at \lambda = 3 still gets 5 wrong: the cell and the thin structure come out too wide, because background samples next to them now look like object. The smoothness term helps, but it cannot fix a wrong intensity model: the data term says the wrong thing everywhere. The model (\mu_0, \mu_1, s) has to fit the image; in the graph-cut unit it is estimated from seed pixels.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in graphcuts/src/seg_energy/exercise.rs.
C1. data_costs(img, mu0, mu1, s) and energy(labels, d0, d1, lambda): the data costs D_p(\ell) = (I_p - \mu_\ell)^2/2s^2 of both labels, and the energy of a labelling with 4-neighbours and w_{pq} = 1:
E = 0
for every pixel p:
E = E + D_p(L_p)
if p has a neighbour q to the right and L_p ≠ L_q: E = E + λ
if p has a neighbour q below and L_p ≠ L_q: E = E + λ
(Looking only right and down counts every pair of neighbours once.) The checks are your answers to B1, B2 and B5.
C2. threshold(d0, d1): the minimum for \lambda = 0, every pixel its cheaper label. The check is B1.
C3. chain_dp(d0, d1, lambda): the exact minimum on a chain by dynamic programming:
C(1, ℓ) = D_1(ℓ) for ℓ = 0, 1
for k = 2, ..., n and ℓ = 0, 1:
C(k, ℓ) = D_k(ℓ) + min( C(k−1, ℓ), C(k−1, 1−ℓ) + λ )
from(k, ℓ) = the label of pixel k−1 that gave the minimum
ℓ = the label with the smaller C(n, ℓ) # the minimum energy
for k = n down to 1: L_k = ℓ; ℓ = from(k, ℓ) # trace back
The checks are your table of B3, the labellings of B2, the thin structure of B4 (kept at λ = 7.7, dropped at 7.9), and a comparison with trying all 1024 labellings of random chains of 10 pixels.
C4. rows_dp(d0, d1, lambda): chain_dp on every row of an image separately.
Then cargo run -p graphcuts -- seg-energy and open the pictures in out/seg-energy/. The program makes a 128 \times 128 image of a disk with a thin diagonal arm (grey levels 80 and 170 of 255, noise 50), and compares thresholding with dynamic programming along the rows for λ = 1.5, 4 and 10. For every labelling it prints the wrong pixels, the number of boundaries (both directions), the data term and the 2D energy at λ = 1.5. Then it treats row 70, through the disk, as a chain.
C5. Look at rows-dp-lambda*.png. What is wrong with them? Compare the 2D energy at λ = 1.5 of the rows-by-DP result (λ = 1.5) with that of the truth. Is the result the minimum of the 2D energy? Why not?
C6. At λ = 10 the arm is gone from the rows-by-DP result, while the disk survives. Use B4 to explain why. (How wide is the arm within one row?)
C7. Count the cost: how many additions and comparisons does chain_dp need for a row of n pixels? How many labellings would you have to try instead for a row of 128 pixels?
The model solution is graphcuts/src/seg_energy/solution.rs in the download.
C5. The results are made of horizontal streaks: specks have become short horizontal dashes, and the left and right edges of the disk are ragged. Each row is smoothed on its own, and a boundary between two rows costs nothing in the row energy. The 2D energy (λ = 1.5) of the rows-by-DP result is 10057.3, with 1721 boundaries, against 8900.4 for the truth (400 boundaries): the truth is better, so the row result is not the minimum of the 2D energy. It minimises each row, ignoring the vertical boundaries. On a grid the rows are tied to each other through loops, and dynamic programming no longer applies: the next units use graph cuts. (The row result is still much better than thresholding: 591 wrong pixels at λ = 1.5 and 213 at λ = 4, against 3024.)
C6. The arm runs diagonally, so in each row it is a piece about 4 pixels wide (3/sin 45°) with two boundaries. As in B4, a short piece survives only if its data gain is more than the 2λ its two boundaries cost. On average an object pixel gains (170 - 80)^2/(2 \cdot 50^2) = 1.6, so the 4 pixels gain about 6.5: far less than 20 at λ = 10, and already less than 8 at λ = 4, where the arm is broken into dashes. The disk is up to 61 pixels wide in a row; its gain is far above 20. (Row 70 as a chain: 13 wrong samples at λ = 0, 4 at 1.5, none at 4 and 10; at λ = 100 all 61 disk samples are wrong, because one label for the whole row is cheaper than two boundaries.)
C7. For every pixel and both labels one minimum of two sums: about 4n additions and 2n comparisons, plus n steps back: proportional to n. Trying all labellings of 128 pixels means 2^{128} \approx 3 \cdot 10^{38} labellings.
Where this leads
On a chain, dynamic programming finds the exact minimum. On a 2D image, the same energy with two labels can still be minimised exactly, by turning it into a minimum cut of a graph: the next two units, maximum flow and graph-cut segmentation. The weights w_{pq} then depend on the image, so that boundaries prefer to follow edges.