The DP table needs an order: row after row, angle after angle. A boundary that may wander in any direction has no such order. Today the algorithm finds the order as it runs.
Pixels as a graph; the same Bellman equations, but no fixed order.
Dijkstra’s algorithm: the unfinished pixel with the smallest tentative cost is final. A priority queue gives the order.
The result is a shortest-path tree: every pixel knows its best predecessor.
Live-wire (intelligent scissors): click a seed, and the cheapest path to the mouse follows in real time.
Same tree, other uses: length along a neurite, distances, watershed.
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Recall the “detour” grid of the first unit: a cheap channel that runs down, right, up and down again.
The best monotone path (one cell per row) costs 24: it has to cross two expensive cells.
Following the channel costs 19, but that path goes up: not one cell per row.
Worse: the cheapest way to a cell in row 2 may come from row 5, which a row-by-row table fills later.
The same happened to the distance transform: one raster pass is wrong where the shortest path goes up or left. We need a method that works without a fixed order.
Pixels as a graph
Recall from the graph-cut day: a graph is a set of nodes and edges between them, each edge with a weight.
Node = pixel; edges to the 4 (or 8) neighbours.
Weight of a move u \to v: here the cost of the pixel entered, C(v) (times the step length 1 or \sqrt 2 for 8 neighbours).
A path from the seed s pays the sum of its weights. On the grids of this unit the seed’s own cost counts too, as in the first unit.
The question: the cheapest path from s to every pixel.
The same equations, no order
The principle of optimality still holds: the best path to v ends with a step from some neighbour u, and its part up to u is the best path to u. So
d(v) = the smallest d(u) + w(u, v) over the neighbours u of v, and d(s) = its start value.
These are the same equations as the DP table. But now d(v) may depend on a neighbour below it, and that neighbour on v itself: the equations go round in circles. There is no row order in which every right-hand side is known first.
Dijkstra’s idea
All weights are non-negative. Keep a tentative cost for every pixel (∞ at first, the start value at the seed).
Take the unfinished pixel with the smallest tentative cost. Its cost is final: any other route to it would pass through some unfinished pixel, which already costs at least as much, and can only add.
Update its neighbours: if going through it is cheaper, lower their tentative cost and remember it as their predecessor.
Repeat until every pixel is final.
A priority queue hands out the smallest tentative pixel quickly. This is DP where the order is discovered while the algorithm runs (Dijkstra, 1959).
Blue: final (step number in the corner). Shaded: tentative, in the queue. The outlined 8 is the next to become final.
Dijkstra as pseudocode
d = ∞ everywhere; d(s) = start value; put s in the queue
while the queue is not empty:
u = the pixel in the queue with the smallest d # now final
for every neighbour v of u that is not final:
if d(u) + w(u, v) < d(v):
d(v) = d(u) + w(u, v)
pred(v) = u # the tree
put v in the queue (or lower its value)
The same update as the DP table; only the order differs: smallest first.
Path to any pixel: follow pred back to the seed.
The math: why the smallest is final
Let F be the set of final pixels, with d(u) correct for u \in F, and let v \notin F have the smallest tentative value \tilde d(v) = \min_{u \in F,\, u \sim v} d(u) + w(u, v).
Take any path from s to v. It leaves F somewhere: let y \notin F be the first pixel on it outside F, entered from x \in F. Its cost is at least
because the rest has non-negative weights and v had the smallest tentative value. So no path beats \tilde d(v): it is final. Induction over the pops proves that every final value is correct.
With a binary heap, each pop and each update costs O(\log N): O(N \log N) for an image with N pixels and a fixed number of neighbours.
Try it: Dijkstra step by step
The shortest-path tree
When Dijkstra stops, every pixel has a final cost and a predecessor: the neighbour its best path comes from.
The predecessors form a tree rooted at the seed: following them from any pixel leads back to the seed.
The cheapest path to any pixel is read off by backtracking, as in the DP table: a few steps, no new computation.
Work: every pixel is made final once and updates its neighbours: about N \log N for N pixels. A 256 \times 256 image takes some tens of milliseconds.
When Dijkstra fails, and when we don’t need it
A negative weight breaks the argument: a pixel declared final could still be reached more cheaply by a later detour through the negative edge. (Costs of pixels, which are \ge 0, are fine.)
If the moves only go forward (down, as in the first unit), the graph has no cycles, and a fixed order exists: the row-by-row table. Monotone-path DP is the special case where the order is known in advance.
order of evaluation
needs
DP table
fixed (rows, angles)
moves only forward
Dijkstra
discovered (smallest first)
non-negative weights
Live-wire: an edge-following cost
Intelligent scissors (Mortensen & Barrett, 1995; Falcão et al., 1998): the user clicks on a boundary, and the cheapest path follows the edges. The local cost of entering a pixel is low on edges:
gradient magnitude: low where the gradient is strong, 1 - G/G_{\max};
Laplacian zero crossing: 0 on the zero crossing of the Laplacian of Gaussian (the centre of the edge), 1 elsewhere;
gradient direction: a penalty for running across the edge direction instead of along it;
each with a weight, after Gaussian smoothing with scale \sigma (the edges day owns these filters). For a thin bright or dark line, the line response itself.
Micrograph: transmission electron micrograph of a mesenchymal stem cell, Robert M. Hunt, CC BY 3.0, via Wikimedia Commons.
The math: the link cost
For a move from p to a neighbour q of length \ell = |q - p| (1 or \sqrt2):
with f_G = 1 - G/G_{\max}, f_Z \in \{0, 1\}, and, for unit vectors \mathbf u_p, \mathbf u_qalong the edge (the gradient turned by 90°) and the unit link \mathbf l (turned to agree with \mathbf u_p),
Weights w_Z = w_G = 0.43, w_D = 0.14 (Mortensen & Barrett); \varepsilon = 0.02 keeps every step slightly expensive, so flat regions do not give free detours. All terms are \ge 0: Dijkstra applies.
Live-wire: how it is used
Click a seed on the boundary. One Dijkstra run computes the tree from the seed to every pixel.
Move the mouse: the wire is the backtracked path from the mouse to the seed. Only backtracking, so it follows in real time.
When the wire leaves the boundary, click to fix the good part: that point becomes an anchor and the seed of a new tree.
Close the contour by joining the last anchor to the first seed.
The user steers, the algorithm does the precise tracing. “Path cooling” fixes anchors automatically where the wire has been stable for a while.
Try it: live-wire
How live-wire behaves
Short-cuts: the cost is a sum along the path, so a long detour along a weak edge can cost more than a short cut across a uniform region. More anchors, or a stronger edge term, help.
The neighbour steals the path: a stronger edge nearby (another cell) is cheaper per pixel; the wire jumps over to it.
Smoothing: too little (\sigma small) and the noise makes cheap pixels everywhere; too much and the edge is displaced and nearby edges merge.
The user is part of the method: the result is as good as the anchors.
Micrograph: transmission electron micrograph of a mesenchymal stem cell, Robert M. Hunt, CC BY 3.0, via Wikimedia Commons.
Same tree, other uses
Length along a structure: the distance along a neurite or a vessel between two points: the cheapest path with a cost that is low on the structure; its length counts steps of 1 and \sqrt2 (as on the skeleton day).
Distance from a point: with cost 1 per step, the tree gives the grid distance from the seed: city block with 4 neighbours, octagons with 8. Exact for the graph, not for the Euclidean plane (next unit). With an image cost, the iso-lines of the distance bend around the structures (right).
Watershed (day 15) floods from markers with the same priority queue. It minimises the maximum height along the path (the lowest pass), where live-wire minimises the sum.
Fast marching, the next unit: Dijkstra’s order with a different update, for continuous, round fronts.
Micrograph: transmission electron micrograph of a mesenchymal stem cell, Robert M. Hunt, CC BY 3.0, via Wikimedia Commons.
Summary
When no fixed order exists, the Bellman equations d(v) = \min_u d(u) + w(u, v) still hold.
Dijkstra: the unfinished pixel with the smallest tentative cost is final (non-negative weights); a priority queue gives the order; O(N \log N).
The result is a shortest-path tree: the cheapest path to any pixel by backtracking.
Live-wire: edge-following local costs, one tree per anchor, real-time backtracking to the mouse; watch for short-cuts and stronger neighbouring edges.
The monotone DP table is the special case with a known order; watershed uses the same queue with the maximum instead of the sum.