Last unit: flooding a landscape, in pictures. Now the algorithm that does it on a pixel grid, small enough to run by hand (Meyer, 1994).
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Keep a priority queue of pixels, ordered by height f(p); among equal heights, first in, first out.
Each pixel enters the queue once and leaves it once. Neighbours are pushed in reading order (up, left, right, down); the arrival number #k records the order.
The 5 \times 5 grid of the exercise, markers A (height 1, row 1, column 1) and B (height 0, corner), 4 neighbours:
| pop | pixel | height, # | labelled neighbours | → |
|---|---|---|---|---|
| start | queue: (3,4) 1 #4, (4,3) 1 #5, (0,1) 2 #0, (1,0) 2 #1, (1,2) 2 #2, (2,1) 2 #3 | |||
| 1 | (3,4) | 1, #4 | B | B |
| 2 | (4,3) | 1, #5 | B | B |
| 3 | (0,1) | 2, #0 | A | A |
| 4 | (1,0) | 2, #1 | A | A |
| … | ||||
| 15 | (1,3) | 4, #12 | A and B | line |
Height first, then arrival number: B’s two height-1 pixels go before A’s height-2 pixels, although A queued its pixels first.
The exercise grid at the end of water levels 1, 2 and 3 and at the end: both basins grow at the same level; black = watershed line.
Give a path from a marker to p the cost “its highest point” (excluding the marker), and let d(p) be the smallest such cost: the minimax (bottleneck) distance from the markers.
1D: 0\,5\,5\,5\,5\,5\,0 with markers A, B at the ends. FIFO: A\,A\,A\,|\,B\,B\,B. LIFO: A\,|\,B\,B\,B\,B\,B.
The 5 \times 5 example with a watershed line (black): one pixel wide with 4 neighbours, thicker with 8.
Bucket queue: an array of L FIFO lists, list v holds the queued pixels of height v, and a pointer c to the lowest non-empty list.
Vincent and Soille (1991), the first fast watershed: