Exercise: images as landscapes
Before this unit: Day 14: Labelling.
See also: Day 2: Edges, blobs and ridges.
Goal
By the end of this exercise you should be able to:
- find the regional minima of a signal, flood it level by level, and write down the basin of every sample and the dams with their heights,
- explain why the dams fall on edges when you flood the gradient magnitude, and on bright lines when you flood the intensity,
- predict how noise, h-minima and markers change the number of regions,
- (math) describe basins by the steepest-descent flow and dams by minimax passes.
Do the pen-and-paper part first, then the demos.
Background
| Idea | What it means |
|---|---|
| Landscape | grey level f = height; dark = low |
| Regional minimum | a connected plateau whose neighbours are all higher (in 1D: the samples left and right of the run) |
| Catchment basin | the points from which water running downhill ends in that minimum |
| Flooding | water rises from every minimum at the same level; where two lakes meet, a dam is built |
| Dam height (pass) | the level at which two basins first touch = the highest point on the lowest way between them |
| Depth of a basin | its bottom subtracted from the lowest pass to a deeper basin |
| h-minima | fill every basin by h first (the morphology day); basins of depth \le h disappear |
| Markers | the only sources of water; unmarked minima are drowned |
In 1D, a sample on a pass that both lakes reach at the same level is either a dam sample (watershed line) or given to the lake that reached it first. When a question asks for “the basin of every sample”, give it to the basin on its left if both reach it at the same time (this is what the demo and the algorithm of the next unit do here).
By hand
The signal (samples 0–11), the same as on the morphology day: f = 7\,1\,4\,3\,4\,6\,5\,1\,6\,4\,2\,3 .
B1. Regional minima and flooding. (a) Mark the regional minima. Name their basins A, B, C, D from left to right. (b) Flood level by level, L = 1, 2, \dots, 7. At each level, which samples are under water, and in which basin? At which levels do two lakes meet, and where? (c) Write the basin of every sample, and the position and height of every dam.
B2. h-minima with h = 2. (a) The h-minima transform (morphology day) is \mathrm{HMIN}_2(f) = 7\,3\,4\,4\,4\,6\,5\,3\,6\,4\,4\,4. Which minima survive? What is the depth of each of the four basins of B1? (b) Flood f again, but only from the surviving minima. Write the basin of every sample and the dams. (c) Which h is the smallest that removes the basin of sample 3? Does h = 1 already do it?
B3. Markers only at samples 3 and 10 (labels A and B). (a) Flood from these markers only. In which order are the samples flooded? (Hint: the water of A spills into the minimum at sample 1 as soon as it reaches sample 2.) (b) Write the basin of every sample and the dam. What happened to the minima at samples 1 and 7?
B4. The step profile f = 1\,1\,1\,1\,5\,5\,5\,2\,2\,2 (samples 0–9). (a) Compute |f'| by central differences, |f(i+1) - f(i-1)|/2, repeating the end samples (f(-1) = f(0), f(10) = f(9)). (b) How many regional minima do f and |f'| have? Flood both. Where are the boundaries in each case? (c) Which of the two do you want if the three flat parts are three different materials?
B5. A noisy flat profile f = 3\,2\,3\,2\,3\,2\,3. (a) How many regional minima, how many regions after flooding? (b) What is the depth of each basin? Which h removes them, and how many regions are left?
B6. A drop of water. On the signal of B1, follow a drop placed on sample 5 (height 6): at each step it moves to the lower of its two neighbours, if that is lower than where it is. Where does it end? Do the same from sample 8. Compare with the basins of B1(c).
B1. (a) Minima at samples 1 (height 1), 3 (3), 7 (1) and 10 (2): basins A, B, C, D. (b)
| level | under water |
|---|---|
| 1 | A: sample 1; C: sample 7 |
| 2 | + D: sample 10 |
| 3 | + B: sample 3; D grows to sample 11 |
| 4 | A reaches sample 2 (4), B reaches samples 2 and 4, D reaches sample 9: A and B meet over sample 2 → dam, height 4 |
| 5 | C reaches sample 6 |
| 6 | B and C meet over sample 5, C and D over sample 8 → two dams, height 6 |
| 7 | A reaches sample 0 |
- Basins A\,A\,A\,B\,B\,B\,C\,C\,C\,D\,D\,D (samples 2, 5 and 8 sit on the passes; with the rule above they go to the basin on their left). Dams on samples 2, 5 and 8, heights 4, 6, 6. With a watershed line: A\,A\,|\,B\,B\,|\,C\,C\,|\,D\,D\,D.
B2. (a) \mathrm{HMIN}_2 has regional minima at sample 1 (3), sample 7 (3) and the plateau 9–11 (4): three survive. Depths: A: the lowest pass to a deeper basin is 6 (sample 5, towards C; C is as deep as A) and to B 4, but B is shallower; A’s depth is 6 - 1 = 5. B: 4 - 3 = 1 (it overflows into A at 4). C: 5 (pass 6 to A’s side and to D; A is as deep). D: 6 - 2 = 4. Only B has depth \le 2. (b) A\,A\,A\,A\,A\,A\,B\,B\,B\,C\,C\,C (three basins, now called A, B, C): B’s old samples 3 and 4 are taken by A’s water when it passes sample 2 at level 4; sample 5 (6) is reached by A before C. Dams on samples 5 and 8, both height 6. With a line: A\,A\,A\,A\,A\,|\,B\,B\,|\,C\,C\,C. (c) The basin of sample 3 has depth 1: it disappears for every h \ge 1 (a basin survives only if its depth is larger than h). So h = 1 already removes it; \mathrm{HMIN}_1(f) = 7\,2\,4\,4\,4\,6\,5\,2\,6\,4\,3\,3.
B3. (a) The queue starts with the neighbours of the markers: samples 2 (4), 4 (4), 9 (4) and 11 (3). Order of flooding: 11 (3, B), 2 (4, A), then 1 (height 1, A): lower than the current water level 4, so it is flooded immediately: the puddle at sample 1 is drowned; then 4 (A), 9 (B); at level 6: 5 (A), then 6 (5, A) and 7 (1, A), again a drowned puddle; then 8 (6), which both A and B reach: the dam; finally 0 (7, A). (b) A\,A\,A\,A\,A\,A\,A\,A\,B\,B\,B\,B, dam on sample 8 (height 6). The minima at samples 1 and 7 are not sources: they are filled from marker A, so A’s region contains three of the original basins. Two markers, two regions.
B4. (a) |f'| = 0\,0\,0\,2\,2\,0\,1.5\,1.5\,0\,0. (b) f has 2 regional minima (the plateau of 1s, samples 0–3, and the plateau of 2s, samples 7–9); flooding f gives 2 regions, and the bright middle (samples 4–6) is shared between them (A\,A\,A\,A\,A\,A\,B\,B\,B\,B; with a line, the line is on sample 5): the boundary falls in the middle of the bright plateau, not on an edge. |f'| has 3 minima (samples 0–2, sample 5, samples 8–9), so 3 regions; the boundaries are on samples 3–4 and 6–7, where the ridges of |f'| are: on the edges. Without line: A\,A\,A\,A\,B\,B\,B\,C\,C\,C; with line: A\,A\,A\,A\,|\,B\,B\,|\,C\,C. (c) The gradient: it gives one region per flat part, with boundaries on the edges.
B5. (a) Three regional minima (the 2s at samples 1, 3, 5): three regions. (b) Each basin has depth 1 (bottom 2, pass 3). Any h \ge 1 fills all three to 3: the whole signal becomes one plateau at height 3, a single minimum, one region. With h = 0.5 all three survive.
B6. From sample 5 (6): neighbours 4 (4) and 6 (5); the lower is sample 4, then sample 3 (3), whose neighbours (4 and 4) are both higher: it ends in the minimum at sample 3: basin B, as in B1. From sample 8 (6): neighbours 7 (1) and 9 (4): it goes to sample 7, the minimum of basin C, as in B1. On a pass the drop chooses the steeper side; flooding chooses the side that reaches the sample first. Here both agree.
Math questions
B7 (math). In 1D, take two consecutive regional minima a < b (no other minimum between them). Show that flooding builds exactly one dam between them, at the highest sample between a and b, and that its height is \max_{a \le i \le b} f(i).
B8 (math). Let g be strictly increasing. Show that flooding g(f) from the same markers gives the same basins as flooding f (with the same tie-breaking). Is the same true for the h-minima markers with a fixed h? Give an example.
B9 (math). For f(x, y) = (x^2 - 1)^2 + y^2: (a) find all critical points and classify them; (b) solve the gradient flow \dot{\mathbf x} = -\nabla f for the y component and show that the sign of x is preserved for the x component; (c) conclude what the two catchment basins and the watershed line are, and give the height of the pass between the two minima.
B7. Between a and b the signal rises from a to its maximum M (at sample t; take the first if several) and falls to b; otherwise there would be another regional minimum between them. Water from a can only move right by flooding samples in order, and it reaches a sample i \in (a, t] when the level is \max_{a \le j \le i} f(j); likewise from b leftwards. Both lakes are below M until the level reaches M, so they cannot meet before; at level M both reach t (or the plateau of Ms), and they meet there: one dam, of height M = \max_{a \le i \le b} f(i), the pass of the minimax formula with a single path.
B8. Flooding only ever compares heights: the queue pops the smallest f (ties by arrival order), and the labels are decided by which neighbours are already labelled. A strictly increasing g preserves every comparison f(p) < f(q) and every tie f(p) = f(q), so the pops come in the same order, with the same decisions: the same basins. h-minima are not invariant: they compare differences of heights (\text{depth} \le h), and g changes differences. Example: f = 3\,2\,3 (depth 1) and g(v) = v^2: 9\,4\,9 (depth 5). With h = 2, the minimum disappears for f but survives for g(f). (Contrast stretching changes which h is right.)
B9. (a) \nabla f = (4x(x^2 - 1),\ 2y), zero at (0, 0) and (\pm 1, 0). Hessian \mathrm{diag}(12x^2 - 4,\ 2): at (\pm 1, 0) it is \mathrm{diag}(8, 2), two minima (f = 0); at (0, 0) it is \mathrm{diag}(-4, 2), a saddle (f = 1). (b) \dot y = -2y gives y(t) = y_0 e^{-2t} \to 0. \dot x = -4x(x^2 - 1): the right-hand side is zero at x = 0, so by uniqueness a solution starting with x_0 > 0 can never cross x = 0; it converges to the stable point x = 1 (for 0 < x < 1, \dot x > 0; for x > 1, \dot x < 0). Likewise x_0 < 0 goes to -1. (c) Basins: the half-planes x > 0 (minimum (1, 0)) and x < 0 (minimum (-1, 0)); watershed line x = 0 (from there the flow runs into the saddle). The pass between the minima is the saddle height f(0, 0) = 1: along x = 0 the function is 1 + y^2 \ge 1, so every path from one minimum to the other has a highest point of at least 1, and the straight path along y = 0 achieves 1.
Explore: flooding a profile
The profile is drawn as a landscape (grey). The water level slider floods it from below; water has one colour per basin, and a dark bar with its height is drawn where two basins meet (a dam). ▲ marks the sources. The coloured strip under the plot is the final segmentation (every basin, after full flooding). h fills shallow basins first (hatched). flood |f′| instead of f draws the input on top, coloured by the basins of |f'| (drawn below and flooded). markers: click “sets a marker” and click samples to make them the only sources. Drag on the plot to redraw the profile.
F1. Preset 12 samples (exercise). Raise the water level from 0 to 7 and check your table from B1(b). At which level does the first dam appear? Set h = 2 and compare with B2. Then tick markers, click “sets a marker”, put markers on samples 3 and 10 and compare with B3.
F2. Preset two cells (a line across two dark nuclei). How many minima? At which water level is the dam built, and where is it?
F3. Preset noisy: one basin per wiggle. How many regional minima and regions now? Raise h: at which value are there 2 regions? What is left at h = 0.5?
F4. Preset edges: flood the derivative. With h = 0.3, how many regions, and where are the dams? Set h = 0 and h = 0.1. Then untick flood |f′|: where are the boundaries now?
F5. Preset plateau. Where is the dam? Why there? Then draw a small dip into the middle of the plateau and look again.
F6. Preset two cells, tick markers and put a single marker in the left nucleus. What happens to the right nucleus? Add a second marker in the right nucleus, then a third on the bright background at the far right.
F1. As in B1: basins appear at levels 1 (A, C), 2 (D), 3 (B); the first dam appears at level 4 (between samples 2 and 3, height 4), the other two at level 6 (height 6). With h = 2: “minima: 4, after filling by h: 3”, the basin of sample 3 is hatched (filled), and the strip shows three regions as in B2(b). With markers on 3 and 10: two regions, the boundary between samples 7 and 8 (dam height 6) as in B3.
F2. 2 regional minima (the two nuclei, heights 2.4 and 3.4). The dam is built at level 6.5, between samples 29 and 30: the brightest point between the nuclei.
F3. 11 regional minima, 11 regions. At h = 0.5 4 sources remain; from h = 1 on there are 2 (one per nucleus): the noise wiggles are all less than 1 deep, the nuclei more than 2.
F4. 3 regions (the three flat levels), dams at heights 1.5 and 0.8, between samples 20 and 21 and between 40 and 41: on the two edges. At h = 0: 15 minima of |f'|, 15 regions (the noise in the flat parts); at h = 0.1: 7; from h = 0.2: 3. Flooding f itself, the minima are in the dark flat parts, and the boundaries fall on bright places, not on the edges.
F5. Between samples 17 and 18, in the middle of the flat ridge (samples 13–22, height 6). Both lakes reach the ridge at level 6 at its two ends and spread over it at the same speed (first in, first out), so they meet in the middle. A dip in the middle of the plateau is a new regional minimum: it gets its own basin, and two dams appear on either side of it.
F6. With one marker the whole profile is one region: the right nucleus is drowned when the water from the left marker passes the dam at 6.5. Two markers give the two nuclei back, with the boundary at the same place as without markers. A third marker on the background makes a third region at the right end; its boundary with the right nucleus sits on the highest point between them, the bright plain.
Explore: h-minima in 1D
The morphology day’s demo in h-minima mode: the marker f + h is eroded step by step and clipped above f (step); blue = water poured into the basins. At stability, blue dots mark the regional minima of the result (the surviving basins). The rows under the plot give f and the current marker for every sample.
R1. With h = 2, step to stability. How many iterations? Compare the result with \mathrm{HMIN}_2(f) in B2. Then set h = 1 and h = 3: how many regional minima remain?
R2. Which basin is filled first as h grows, and why that one?
R1. 3 iterations; the result is 7\,3\,4\,4\,4\,6\,5\,3\,6\,4\,4\,4, as in B2, with 3 regional minima (the signal has 4). At h = 1 also 3 remain (the basin of sample 3 has depth 1, so it is already filled); at h = 3 still 3 (the next shallowest basin, D, has depth 4).
R2. The basin of sample 3: it is the shallowest (depth 1). As h grows past each basin’s depth, that basin overflows into its neighbour; the order is the order of the depths (B: 1, D: 4, A and C: 5).
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 watershed/src/ws_landscape/exercise.rs. Everything here is 1D: a signal f(0), \dots, f(n-1), and one label per sample (0 = none).
C1. regional_minima(f): label the runs of equal samples whose neighbours (just left and just right of the run) are higher, 1, 2, 3, … from left to right. The checks are B1 (a), B4 (b) (for f and |f'|; |f'| is computed by the given abs_derivative) and B5 (a).
C2. drop_of_water(f, i): move to the lower neighbour while it is lower. The check is B6.
C3. flood(f, sources): flood level by level from the labelled sources:
for every level L, the distinct heights from low to high:
repeat until nothing changes:
every unlabelled sample with f ≤ L next to a lake takes its label
(all at once: every lake grows by one sample; left neighbour first)
every unlabelled sample with f < L next to a lake takes its label
straight away, again and again (water runs downhill into a puddle)
The checks are B1 (c), B2 (b), B3 (b) (markers: the puddles are drowned) and B4 (b).
C4. dams(f, labels): where two neighbouring samples have different labels, the dam is the higher of the two, with its height. The checks are B1 (c) and B3 (b).
C5. h_minima(f, h): fill every basin by h:
g = f + h
repeat until g does not change:
g(i) = max( f(i), min(g(i−1), g(i), g(i+1)) ) # for all i at once; ends repeated
The checks are \mathrm{HMIN}_2 and \mathrm{HMIN}_1 of B2, and B5 (b).
Then cargo run -p watershed -- ws-landscape and open the pictures in out/ws-landscape/. The program floods a “two cells” profile (two dark dips on a bright background, 64 samples), the same profile with noise (standard deviation 0.25), and a noisy step profile with three flat levels. In the pictures every sample is a column in the colour of its basin, dams are black, and sources are white marks at the bottom.
C6. How many minima and regions does the clean profile have, and the noisy one? How many minima remain for h = 0.25, 0.5, 1, 2, 4? Which range of h gives one region per cell, and why does h = 4 give only one?
C7. Compare edges-flood-f.png and edges-flood-derivative.png. Where are the dams in each? Which one finds the three flat parts, and why (B4)?
The model solution is watershed/src/ws_landscape/solution.rs in the download.
C6. Clean: 2 minima, 2 regions, one dam on sample 31 (height 6.5), at the bright gap between the cells. Noisy: 17 minima, 17 regions (two-cells-noisy.png): every wiggle of the noise is a basin. After h-minima: 10 (h = 0.25), 5 (0.5), 2 (h = 1 to 3), 1 (h = 4). From h = 1 to 3 the result is the two cells, with the dam again on sample 31 (two-cells-noisy-h1.png): the noise basins are shallower than 1, the cells deeper than 3. The depth of the right cell’s basin is measured from the pass, not from the bright background: on the clean profile its bottom is at 3.4 and the pass at 6.5, a depth of only 3.1 (with the noise, between 3 and 4). At h = 4 it overflows into the deeper left cell, and one region is left.
C7. Flooding f gives 2 regions with the dam on sample 26, in the middle of the bright plateau, not on an edge: f has minima only in the two dark parts, and the bright part is shared between them. Flooding |f'| gives 3 regions with dams on samples 19 and 40, on the two edges: each flat part is a valley of the derivative, each edge a ridge. This is B4 with noise; h-minima (h = 0.3) removes the small minima the noise makes in |f'|.
Where this leads
The flooding picture gives one region per minimum, with boundaries on the ridges. The next unit turns it into an algorithm on a pixel grid (a priority queue that always floods the lowest pixel next) that you can run by hand. The unit after that floods the gradient of a real image and fights oversegmentation with smoothing, h-minima and markers; the last unit of the day floods the distance transform to split touching cells.