Exercise: geodesic active contours
Before this unit: Moving a curve: the level set equation (previous unit); Day 30: Snakes and active shape models; Day 13: Morphology.
See also: Day 51: Marching cubes.
Goal
By the end of this exercise you should be able to:
- turn an image into an edge-stopping function g by hand, and say where a front slows down,
- explain why a braking balloon does not stop on its own, and what the advection term adds,
- predict what the parameters \sigma, k, c, \varepsilon, a do, and when the front leaks or gets stuck,
- use one curve to segment several objects (outside-in) or grow seeds (inside-out),
- (math) compute the first variation of the edge-weighted length for a circle, and find equilibria.
Do the pen-and-paper part first, then the demo. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Idea | What it means |
|---|---|
| Edge strength | \lvert\nabla I_\sigma\rvert: gradient magnitude of the image smoothed with a Gaussian of width \sigma |
| Edge-stopping function | g = 1/(1 + \lvert\nabla I_\sigma\rvert^2/k^2): 1 in flat regions, small on edges; g = 0.5 at strength k, 0.1 at 3k |
| Braking balloon | outward speed F = g\,(c - \varepsilon\kappa); c > 0 inflates, c < 0 shrinks |
| Geodesic active contour | minimise \oint g\,ds; adds an advection along -\nabla g (strength a), into the edge |
| Level set form | \phi_t = -g c\lvert\nabla\phi\rvert + \varepsilon g\kappa\lvert\nabla\phi\rvert + a\,\nabla g\cdot\nabla\phi (\phi < 0 inside) |
Moving each point of a closed curve outward by a small \delta(s) changes L_g = \oint g\,ds by \oint(g\kappa + \nabla g\cdot\mathbf n)\,\delta\,ds: the length element grows by \kappa\delta\,ds and the weight changes by \nabla g\cdot\mathbf n\,\delta. Steepest descent: F = -(g\kappa + \nabla g\cdot\mathbf n), which in level set form is \phi_t = g\kappa|\nabla\phi| + \nabla g\cdot\nabla\phi.
By hand
B1. A 1D edge. The pixels 0, 0, 0, 10, 10, 10. (a) Smooth with [1\ 2\ 1]/4 (repeat the border pixels). (b) Compute |\nabla I| with central differences \tfrac12(I_{i+1} - I_{i-1}) and g for k = 2. (c) A front moves with speed g (balloon c = 1). Where is it slowest? Does it fall below 0.1 anywhere? Which k would be needed for that? (d) Compute -g' (central differences) at each pixel. On which side of the edge does it point right, and on which left?
B2. Strong and weak edges. A step of height h blurred by a Gaussian of width \sigma has a largest slope of h/(\sigma\sqrt{2\pi}). In the demo’s test image the objects are 130 grey levels above the background, but one disk fades to only 25 above it on one side; \sigma = 1.5, k = 8. (a) Compute g at the centre of the strong edge and of the weak edge. (b) A braking balloon with c = 0.3 (no advection) reaches the strong edge. How long does it need to creep through a 3-pixel-wide band where g \approx 0.06?
B3. Balloon against curvature. In a flat region (g = 1, no advection), a circle of radius r moves with F = c - \varepsilon/r. (a) For which radius does it stand still? What happens to circles slightly smaller or larger? (b) You click seed circles of radius 8 with c = 0.3, \varepsilon = 0.3. Do they grow? And with c = 0.05, \varepsilon = 1?
B4. Predict. Describe in one sentence what happens to the segmentation of a noisy image when you (a) set \sigma = 0, (b) make \sigma very large, (c) make k very large, (d) make |c| very large, (e) set a = 0, (f) make \varepsilon very large.
B5. Outside-in and inside-out. An image has 5 bright cells, two of which touch. (a) You start with one big box and c < 0. How many curves do you expect at the end, and what could make it 4? (b) You click 2 seeds in each cell and use c > 0. What happens to the two seeds in one cell?
B1. (a) 0, 0, 2.5, 7.5, 10, 10. (b) |\nabla I| = 0, 1.25, 3.75, 3.75, 1.25, 0; g = 1, 0.72, 0.22, 0.22, 0.72, 1. (c) Slowest on the two middle pixels, at 22% speed. It never falls below 0.1: that needs |\nabla I| > 3k, so k < 3.75/3 = 1.25 (with k = 1: g = 0.066). (d) -g' = 0.14, 0.39, 0.25, -0.25, -0.39, -0.14: positive (pointing right) on the left half, negative (pointing left) on the right half. From both sides it points to the edge between pixels 3 and 4.
B2. (a) Strong: slope 130/(1.5\sqrt{2\pi}) = 34.6 grey levels per pixel, g = 1/(1 + (34.6/8)^2) = 0.051. Weak: slope 6.6, g = 0.59: the front hardly notices it. (The demo, with noise and the shapes’ curvature, measures about 0.06 and 0.44.) (b) Speed 0.06 \cdot 0.3 = 0.018 pixels per unit time: 3/0.018 \approx 170 time units. The braking balloon only delays; given time, it crosses every edge.
B3. (a) F = 0 at r^* = \varepsilon/c. Smaller circles have \varepsilon/r > c and shrink (and vanish), larger ones grow: an unstable equilibrium. (b) r^* = 0.3/0.3 = 1: radius 8 grows. With c = 0.05, \varepsilon = 1: r^* = 20, so seeds of radius 8 shrink and disappear before they can reach any edge.
B4. (a) Noise makes g small almost everywhere: the front sticks near its start. (b) Edges blur into each other and weak edges disappear: objects merge, small ones are lost, boundaries are rounded. (c) g \approx 1 even on real edges: the front passes through them. (d) It leaks through every weak edge and overshoots corners. (e) Only braking: the front slows at edges but keeps creeping and eventually crosses them. (f) Curves become round and short: concave parts are cut off, small objects vanish.
B5. (a) Five, one per cell, if the touching cells are separated by an edge strong enough; if the boundary between the two touching cells is weak (or \sigma large), they stay together in one curve: 4. (b) They grow and merge into one curve when their fronts meet: the topology adapts.
Math questions
B6. (math) Let g depend only on the distance \rho from a point, and let C be the circle of radius r around it. (a) Write L_g(r) = \oint g\,ds. (b) Compute dL_g/dr and show it equals \oint(g\kappa + \nabla g\cdot\mathbf n)\,ds. (c) For g(\rho) = 1 - 0.9\,e^{-(\rho - 10)^2/8} (a ring-shaped edge at \rho = 10), is the circle r = 10 a stationary point? Is it a minimum of L_g among circles?
B7. (math) Show that the advection term a\,\nabla g\cdot\nabla\phi in \phi_t corresponds to an outward normal speed F = -a\,\nabla g\cdot\mathbf n. Explain with this why a front just outside an edge moves inward, and one just inside moves outward.
B8. (math) The time-step limit for the advection part is \Delta t\,\max(|v_x| + |v_y|) \le \Delta x with \mathbf v = -a\nabla g. Why does a larger a, or a sharper edge (smaller \sigma), force a smaller time step?
B6. (a) L_g = 2\pi r\,g(r). (b) dL_g/dr = 2\pi(g + r g') = \oint(g/r + g')\,ds over the circle of length 2\pi r; with \kappa = 1/r and \nabla g\cdot\mathbf n = g'(r) this is \oint(g\kappa + \nabla g\cdot\mathbf n)\,ds. (c) At r = 10: g' = 0, g = 0.1: dL_g/dr = 2\pi \cdot 0.1 > 0, so it is not stationary: the length term still wants to shrink it. The minimum among circles is slightly inside the valley, where g + r g' = 0, i.e. g' = -g/r: here at r = 9.955 (found numerically). A balloon c > 0 can compensate this small inward shift. This is the shrinking bias of length terms, as for snakes and graph cuts.
B7. \phi_t + F|\nabla\phi| = 0 with \phi_t = a\nabla g\cdot\nabla\phi gives F = -a\nabla g\cdot\nabla\phi/|\nabla\phi| = -a\nabla g\cdot\mathbf n. Just outside an edge, g increases outward (\nabla g\cdot\mathbf n > 0): F < 0, inward. Just inside, g increases inward (\nabla g\cdot\mathbf n < 0): F > 0, outward. Both move towards the valley.
B8. |\mathbf v| = a|\nabla g|: doubling a doubles the speed, and a sharper edge makes g drop from 1 to near 0 over fewer pixels, so |\nabla g| is larger. Faster transport must still move at most one pixel per step.
Explore: edge-based segmentation
A 128 \times 128 image: a synthetic test image (three disks, the lower-left one fading on its right side, and a C shape; noise adjustable) or the micrograph. image / g switches the view to the edge-stopping function (white = 1, black = 0); −∇g arrows shows the advection direction. Start shapes: big box, or click the image to add start circles (radius 8). The yellow curve is the front; “pieces” counts its closed curves, and for the test image “wrong” counts pixels that differ from the true objects. \phi is reinitialised every 20 steps.
E1. Preset shrink-wrap (c = -0.3, \varepsilon = 0.3, a = 5, \sigma = 1.5, k = 8). Play. Note the step at which the front splits into 3 and into 4 pieces, and the final number of wrong pixels. Look at the g view and the arrows: where is g smallest, and where do the arrows point?
E2. Same preset, but set the advection a = 0 and reset. What happens, and why (compare B2(b))?
E3. Same preset with a = 5 again, but balloon c = -0.6. Watch the lower-left disk. How many pieces remain, and why?
E4. Preset leak through the weak edge (c = 1, a = 1, a seed inside the lower-left disk of 531 pixels). How many pixels are inside after 100 steps? Then try c = 0.3 with a = 1, and c = 0.3 with a = 3. Which settings hold the front?
E5. Preset C shape: no balloon (c = 0, \varepsilon = 1, a = 2, start: a circle around the C). Where does the front stop? Compare its area with that of the C itself (1020 pixels). Then set c = -0.3 and reset.
E6. Preset noise, no smoothing (noise 25, \sigma = 0). Look at the g view. What does the front do? Set \sigma = 2 and reset.
E7. Preset micrograph: nucleus (\sigma = 1.5, k = 4, c = 0.3, a seed in the nucleus). Where does the front stop? Try k = 8, and k = 2, each from a reset. Explain with B4.
E1. (Δt = 0.195.) Around step 800 the front has split into 3 pieces, by step 1200 (t \approx 235) into 4: one per object. It then stays put with 79 wrong pixels (2376 inside; the true objects have 2455 pixels). g is darkest on the object boundaries (about 0.06), about 1 elsewhere (median 0.97); the weak right side of the lower-left disk is only grey (about 0.44). The arrows point into the dark rims from both sides.
E2. With a = 0 (Δt = 0.667) the front slows at the edges but never stops: it creeps through them into the objects, and by t \approx 530 nothing is left inside. Exactly as in B2(b): g > 0 everywhere, so a braking balloon only delays.
E3. At first four pieces (step 800), but the stronger push breaks through the weak side of the lower-left disk and eats it: 3 pieces by step 1600, 598 wrong pixels. The weak edge only holds while the advection beats g c.
E4. After 100 steps 1315 pixels are inside: the front has escaped through the weak side and floods the background (10795 pixels after 300 steps). With c = 0.3, a = 1 it still leaks, more slowly (2102 pixels after 300 steps). With c = 0.3, a = 3 it stops at 527 pixels, the disk.
E5. The front wraps the C but bridges the opening with an almost straight segment and stops with about 1600 pixels inside (after 4000 steps): the C plus most of its inner hole. With c = -0.3 the balloon pushes it into the slot: 979 pixels after 2000 steps, close to the C alone.
E6. In the g view the whole image is speckled dark (median g about 0.12): every noisy pixel is an “edge”. The box hardly moves (13194 pixels inside after 1200 steps). With \sigma = 2 the noise is smoothed away: 4 pieces and 392 wrong pixels after 600 steps (later the weak disk is lost again: 3 pieces).
E7. With k = 4 the front grows through the textured nucleus and stops at the dark nuclear envelope (about 2570 pixels). With k = 8 the envelope no longer counts as a strong enough edge: the front passes it in places and spreads into the cytoplasm (about 4240 pixels). With k = 2 the texture inside the nucleus already counts as edges: the front stops after a few steps (237 pixels). (B4(c) and (a).)
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 levelsets/src/geodesic/exercise.rs. Grey levels run from 0 to 1 here, so k = 8 of the demo becomes k = 8/255.
C1. edge_stopping(smoothed, k): g = 1/(1 + |\nabla I|^2/k^2) at every pixel of an image that is already smoothed, with central differences. The check is B1(b) and B1(c).
C2. braking_step(phi, g, c, eps, dt): one step of stage 1, the braking balloon:
for every pixel:
balloon = g · c · |∇φ| # upwind, for the sign of c (given)
smoothing = ε · g · κ|∇φ| # central differences, as in curvature flow
new φ = φ − Δt · balloon + Δt · smoothing
The checks: with g = 1 and \varepsilon = 0 it is the level set step of B1 of the previous unit; with g = 0.1 the front moves a tenth as far; and the circles of B3(b), one growing, one shrinking.
C3. advection_step(phi, g, a, dt): the pull into the edge, transport with the velocity \mathbf v = -a\nabla g:
for every pixel:
vx, vy = −a · (central differences of g)
φx = φ(x) − φ(x−1) if vx > 0, else φ(x+1) − φ(x) # upwind: where the values come from
φy likewise
new φ = φ − Δt · (vx φx + vy φy)
The checks: the g of B1, whose valley pulls a zero crossing to its left towards the edge (B1d), and a linear case worked out by hand.
Then cargo run -p levelsets -- geodesic and open the pictures in out/geodesic/: g.png is the edge-stopping function (\sigma = 1.5, k = 8/255); segment.png shows, left to right, a box shrinking onto the objects (c = -0.4, \varepsilon = 0.3) with the pull (a = 5) and without it (a = 0), each until t = 180, and two seeds of radius 10 grown in the disk with the weak edge until t = 60 (red: c = 1, a = 1; green: c = 0.3, a = 3). One step (\Delta t = 0.25) is braking_step followed by advection_step.
C4. How many pieces and how many wrong pixels does the program report with and without the pull? Where in segment.png does the front without the pull go wrong, and why there first?
C5. Compare the two seeds with the true size of the weak disk (printed). Explain with g what happens on the faint right side of the disk.
C6. One step costs a few dozen operations per pixel, and a front needs about one step per fraction of a pixel it travels. Count the steps of the box run. Why can the step \Delta t not simply be made larger? (Look at the time-step rules on the slide “The math: the geodesic active contour flow” and in the previous unit.) The program prints nothing about it: estimate the largest |v_x| + |v_y| from the steepest g.
The model solution is levelsets/src/geodesic/solution.rs in the download.
C4. With the pull: 4 pieces, one per object, and 38 wrong pixels. Without it: 3 pieces and 1741 wrong pixels. Without the pull the front only slows down on the edges (g is never exactly 0, its smallest value here is 0.051) and creeps on into the objects. The disk with the weak edge goes first: its g on the faint side is far from 0, so the front hardly slows there; by t = 180 it has eaten the whole disk, and the fronts have also bitten into the top-left disk and the C.
C5. The weak disk has 1005 pixels. The gentle seed (c = 0.3, a = 3) ends with 988: it stops on the edge all round. The strong seed (c = 1, a = 1) has escaped through the faint right side and flooded the background: 5707 pixels after t = 60, and still growing. On the faint side |\nabla I| is small compared with k, so g stays well above 0 and the brake is weak, and the valley of g is shallow, so the pull is weak too; a strong balloon simply pushes through.
C6. t = 180 with \Delta t = 0.25: 720 steps over the whole image, each with two passes (balloon and curvature, then the pull). The explicit steps have limits: the balloon wave \Delta t \le 1/(\sqrt2\max|gc|) = 1.8, the curvature term \Delta t \le 1/(4\varepsilon\max g) = 0.83, and the pull \Delta t\,(|v_x| + |v_y|) \le 1. Next to the strong edges g drops from about 1 to 0.05 within a few pixels, and |v_x| + |v_y| = a(|g_x| + |g_y|) reaches about 3.6 with a = 5: the pull allows only \Delta t \le 0.28, the tightest of the three. Beyond the limit the step can explode, as in the previous unit.
Where this leads
Edge-based contours only look at local differences: noise, blur and weak edges are their enemies, and the result depends on \sigma, k and the balloon. The next unit asks a global question instead: which curve splits the image into two regions that are each as uniform as possible? That is the Chan–Vese model, a level set cousin of the graph-cut energy.