Exercise: curves as level sets
Before this 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:
- read inside and outside from the sign of \phi and build shapes with min and max,
- compute the normal and the curvature of a level line from \phi, by hand on a small grid,
- say what a signed distance function is, and why many functions \phi describe the same curve,
- explain why the topology of a contour can change while \phi changes only a little,
- (math) derive the curvature of a circle from \nabla\cdot(\nabla\phi/|\nabla\phi|), and see where min and max stop being distances.
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 |
|---|---|
| Implicit curve | the set where \phi(x, y) = 0; \phi < 0 inside, \phi > 0 outside |
| Union, intersection, complement | \min(\phi_1, \phi_2), \max(\phi_1, \phi_2), -\phi |
| Outward normal | \mathbf n = \nabla\phi / \lvert\nabla\phi\rvert |
| Curvature | \kappa = \nabla\cdot\mathbf n; a circle of radius r has \kappa = 1/r |
| Signed distance function | \lvert\phi\rvert = distance to the curve; then \lvert\nabla\phi\rvert = 1, \mathbf n = \nabla\phi, \kappa = \nabla^2\phi |
| Central differences | \phi_x \approx \tfrac12(\phi_{i+1} - \phi_{i-1}), \phi_{xx} \approx \phi_{i+1} - 2\phi_i + \phi_{i-1} (pixel spacing 1) |
| On the grid | the contour lies between pixels where the sign changes (marching squares, from the marching-cubes day) |
\kappa = \nabla\cdot\frac{\nabla\phi}{|\nabla\phi|} = \frac{\phi_{xx}\phi_y^2 - 2\phi_x\phi_y\phi_{xy} + \phi_{yy}\phi_x^2}{(\phi_x^2 + \phi_y^2)^{3/2}}, with the mixed derivative by central differences \phi_{xy} \approx \tfrac14(\phi_{i+1,j+1} - \phi_{i+1,j-1} - \phi_{i-1,j+1} + \phi_{i-1,j-1}). Where \phi_y = 0 this reduces to \kappa = \phi_{yy}/|\phi_x|.
By hand
The grid below samples \phi = \sqrt{x^2 + y^2} - 1.5 at x, y \in \{-2, -1, 0, 1, 2\} (rounded to 3 decimals):
| y \backslash x | −2 | −1 | 0 | 1 | 2 |
|---|---|---|---|---|---|
| 2 | 1.328 | 0.736 | 0.500 | 0.736 | 1.328 |
| 1 | 0.736 | −0.086 | −0.500 | −0.086 | 0.736 |
| 0 | 0.500 | −0.500 | −1.500 | −0.500 | 0.500 |
| −1 | 0.736 | −0.086 | −0.500 | −0.086 | 0.736 |
| −2 | 1.328 | 0.736 | 0.500 | 0.736 | 1.328 |
B1. (a) Mark every pixel inside the curve. How many are there? (b) Along the row y = 0, the sign changes between x = 1 and x = 2. Interpolate linearly to find where the curve crosses. Compare with the true circle. (c) The same along the row y = 1, between x = 1 and x = 2. True crossing: x = \sqrt{1.5^2 - 1}.
B2. At the pixel (1, 0): (a) compute \phi_x and \phi_y by central differences, |\nabla\phi|, and the normal; (b) compute \phi_{yy} and the curvature \kappa = \phi_{yy}/|\phi_x| (here \phi_y = 0). Compare with the exact value 1/\rho, where \rho is the distance of the pixel from the centre.
B3. At the pixel (1, 1): compute \phi_x, \phi_y and the normal by central differences. Compare the normal with the exact one, (x, y)/\rho. Is the estimate of |\nabla\phi| also exact? Why might it not be?
B4. Set operations. (a) Write a \phi for a ring (annulus) between the circles of radius 1 and 2 around the origin, using \max. (b) Evaluate it at the origin, at \rho = 1.5 and at \rho = 3. Inside or outside? (c) Is your \phi a signed distance function? Check one point in each of the three regions.
B5. Lines. (a) Show that \phi = (3x + 4y - 10)/5 is a signed distance function (what is |\nabla\phi|?). Which side of the line is “inside”? (b) How far is the origin from the line? Read it from \phi. (c) The same line is also the zero set of 3x + 4y - 10. What changes, and what does not?
B6. Predict. An ellipse with semi-axes a = 2.2 (along x) and b = 1.2. At which points is the curvature largest, and at which smallest? The exact values are a/b^2 and b/a^2: compute them, and the radius of the circle that fits the curve best at each point.
B1. (a) The 9 pixels with negative values: the centre, its 4 neighbours (-0.5) and the 4 diagonal neighbours (-0.086). (b) \phi(1, 0) = -0.5, \phi(2, 0) = 0.5: the crossing is halfway, at x = 1.5: exactly the radius (along this row \phi is linear in x). (c) \phi(1, 1) = -0.086, \phi(2, 1) = 0.736: t = 0.086/0.822 = 0.104, so x = 1.104. The true crossing is \sqrt{1.25} = 1.118: linear interpolation is off by 0.014 because \phi bends between the two pixels.
B2. (a) \phi_x = (\phi(2, 0) - \phi(0, 0))/2 = (0.5 + 1.5)/2 = 1, \phi_y = (\phi(1, 1) - \phi(1, -1))/2 = 0: |\nabla\phi| = 1, normal (1, 0): exact. (b) \phi_{yy} = \phi(1, 1) - 2\phi(1, 0) + \phi(1, -1) = -0.086 + 1 - 0.086 = 0.828, so \kappa \approx 0.83. The pixel is at \rho = 1: the level line through it is a circle of radius 1 with \kappa = 1. The coarse grid underestimates the curvature by 17%.
B3. \phi_x = (\phi(2, 1) - \phi(0, 1))/2 = (0.736 + 0.5)/2 = 0.618, and by symmetry \phi_y = 0.618. Normal (0.618, 0.618)/0.874 = (0.707, 0.707): exactly (1, 1)/\sqrt 2. But |\nabla\phi| = 0.874, not 1: the central difference averages the slope over two pixels, and the distance function bends in between (it has a kink at the centre, two pixels away).
B4. (a) Inside the ring means inside the big disk and outside the small one: \phi = \max(\rho - 2,\ 1 - \rho) with \rho = \sqrt{x^2 + y^2}. (b) Origin: \max(-2, 1) = 1, outside (in the hole). \rho = 1.5: \max(-0.5, -0.5) = -0.5, inside, 0.5 from both circles. \rho = 3: \max(1, -2) = 1, outside. (c) Yes: in the hole 1 - \rho is the distance to the inner circle, outside \rho - 2 is the distance to the outer one, and in the ring the larger of the two (the one closer to zero) is minus the distance to the nearer circle.
B5. (a) \nabla\phi = (3, 4)/5, length 1. Inside is where 3x + 4y < 10: the side containing the origin. (b) \phi(0, 0) = -2: the origin is 2 from the line, on the inside. (c) The curve, the inside and the normal direction are the same; |\nabla\phi| = 5, so the values are 5 times the distances.
B6. Largest at the ends of the long axis, (\pm 2.2, 0), where the curve turns sharply: \kappa = 2.2/1.2^2 = 1.53, radius 1/\kappa = 0.65. Smallest at (0, \pm 1.2), where it is flattest: \kappa = 1.2/2.2^2 = 0.248, radius 4.03.
Math questions
B7. (math) For \phi = \sqrt{x^2 + y^2} - r, compute \nabla\phi and \nabla\cdot(\nabla\phi/|\nabla\phi|), and show that every level line has curvature 1/\rho.
B8. (math) For \phi = x^2 + y^2 - r^2: compute |\nabla\phi| and \nabla^2\phi. Show that the formula \kappa = \nabla\cdot(\nabla\phi/|\nabla\phi|) still gives 1/\rho, but \nabla^2\phi does not.
B9. (math) Two disks of radius 1 centred at (\pm 0.5, 0) overlap. (a) Evaluate \min(\phi_1, \phi_2) (signed distances of the two disks) at the origin. (b) What is the true distance from the origin to the boundary of the union? (c) Explain why \min of two signed distances is exact outside the union but not always inside.
B7. \nabla\phi = (x, y)/\rho with \rho = \sqrt{x^2 + y^2}; |\nabla\phi| = 1. \partial_x(x/\rho) = 1/\rho - x^2/\rho^3 = y^2/\rho^3 and \partial_y(y/\rho) = x^2/\rho^3, so \kappa = (x^2 + y^2)/\rho^3 = 1/\rho. The level line through a point at distance \rho is the circle of radius \rho: its curvature is 1/\rho, on the curve itself 1/r.
B8. \nabla\phi = 2(x, y), |\nabla\phi| = 2\rho, \nabla^2\phi = 2 + 2 = 4. The unit normal is 2(x, y)/(2\rho) = (x, y)/\rho, the same as in B7, so its divergence is again 1/\rho. The Laplacian, 4, is not a curvature at all: \kappa = \nabla^2\phi needs |\nabla\phi| = 1.
B9. (a) \phi_1(0, 0) = \phi_2(0, 0) = 0.5 - 1 = -0.5, so \min = -0.5. (b) The boundary of the union consists of the parts of each circle outside the other disk; the nearest such points to the origin are the two intersection points (0, \pm\sqrt{1 - 0.25}) = (0, \pm 0.866). True signed distance: -0.866. (c) Outside the union, the nearest boundary point of the union is the nearest point of the nearer circle, and that point lies on the union’s boundary: \min is exact. Inside, the nearest point of a circle may lie inside the other disk (from the origin, the nearest point of circle 1 is (-0.5, 0), which lies inside disk 2), so it is not on the union’s boundary; \min then underestimates the depth. The zero set is still right, which is all a level set needs; reinitialisation (unit 2) repairs the values.
Explore: slicing surfaces
Left: the surface z = \phi(x, y) (drag to rotate; the buttons give standard views) with the orange plane at height c. Right: the top view, blue where \phi < 0 and red where \phi > 0, thin level lines every 0.5, the zero contour in solid black and the contour \phi = c dashed orange. Click the top view to see the normal there (arrow), |\nabla\phi|, the curvature \kappa of the level line through the point and its circle of curvature (green, dashed). The three buttons under “φ for the same curve” give three functions with the same zero contour: the distance d, 3d, and d + d^2/2R (for the circle this is (x^2 + y^2 - R^2)/2R). Coordinates run from -3 to 3; clicks snap to multiples of 0.05.
E1. Preset circle (radius 1.5, distance d). The marked point is (1.2, -0.9). Read \phi, |\nabla\phi|, the normal and \kappa. Check them against the formulas. Then click (2.4, -1.8): what changes and why?
E2. Preset one becomes two. Lower the plane c in steps of 0.05 and watch the readout “pieces”. At which c does the contour split? Which value of \phi explains it (compare Q3 of the quiz)? Rotate the surface: where is that point on the surface?
E3. Preset same curve, different φ. Click (0.6, -0.45) and read \phi, |\nabla\phi| and \kappa for each of the three functions. Which numbers change, which do not? Which of the three is a signed distance function?
E4. Preset corner: κ blows up (a square of half side 1.5). The marked point (1.6, -1.6) is just outside a corner. Read \kappa and 1/\kappa. Predict \kappa at (1.7, -1.7) before you click. Then click (1.2, 1.0) and (1.0, 1.0) inside the square. Explain the three very different values.
E5. Choose the ellipse and click its two ends, (2.2, 0) and (0, 1.2). Compare \kappa with B6.
E6. Preset egg crate: \phi = \sin(\pi x/2)\sin(\pi y/2), not a distance. Move c from -0.9 to 0.9 and describe how the contours change. What does the zero contour look like? Click (0, 0) and (1, 1): why does the demo give no normal there?
E1. \phi = 0.00 (the point is on the circle: \sqrt{1.2^2 + 0.9^2} = 1.5), \nabla\phi = (0.80, -0.60), |\nabla\phi| = 1.00, \mathbf n = (0.80, -0.60) = (x, y)/\rho, \kappa = 0.67 = 1/1.5. At (2.4, -1.8), twice as far out (\rho = 3): \phi = 1.50 (the distance to the circle), the same normal, \kappa = 0.33: the level line through this point is a circle of radius 3.
E2. At c = 0.20 the readout shows 1 piece, at 0.15 two. The value is \phi = 0.2 at the origin, the gap between the disks: the saddle point of the surface, between the two cones. Planes above it cut one connected curve, planes below it two.
E3.
| φ | \phi | \lvert\nabla\phi\rvert | \kappa |
|---|---|---|---|
| distance d | −0.75 | 1.00 | 1.33 |
| 3d | −2.25 | 3.00 | 1.33 |
| d + d^2/2R | −0.56 | 0.50 | 1.33 |
The values of \phi and the slope change; the zero contour, the normal direction and the curvature of the level line do not (the level lines are the same circles, only labelled with different heights). Only d is a signed distance function (|\nabla\phi| = 1).
E4. At (1.6, -1.6): \kappa = 7.08, 1/\kappa = 0.14: outside a corner the level lines of the distance are quarter circles around the corner, and this point is 0.1\sqrt2 = 0.14 from it. At (1.7, -1.7) the distance doubles: \kappa = 1/0.283 = 3.54. At (1.2, 1.0) the nearest boundary is the right side: the level line is straight, \kappa = 0. At (1.0, 1.0) the point is on the diagonal, where the right and top sides are equally near: the distance has a kink there, and the finite differences give \kappa \approx 212, a meaningless huge number. Corners of the curve, and the ridges inside a shape, are where \kappa blows up.
E5. \kappa = 1.53 at (2.2, 0) and 0.25 at (0, 1.2), as in B6: the sharp ends bend about six times more than the flat sides.
E6. For c near -1: small closed blobs around the minima (where \sin\sin = -1); they grow as c rises. At c = 0 the contour is a grid of straight lines x = 0, \pm 2 and y = 0, \pm 2, which cross at saddle points: there neighbouring blobs touch. Above 0 the blobs around the maxima take over and shrink towards c = 1. At (0, 0) (a saddle) and at (1, 1) (a maximum) the gradient is zero: there is no direction “across” the level line, so no normal and no curvature. At such points the topology of the contours changes.
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/implicit/exercise.rs. Here \phi is an image, sampled at the pixels; derivatives are differences of neighbours.
C1. union, intersection and difference: pixel by pixel \min(\phi_1, \phi_2), \max(\phi_1, \phi_2) and \max(\phi_1, -\phi_2). The check is the ring of B4.
C2. row_crossings(phi, y): where the curve crosses row y:
for x from 0 to width − 2:
a = φ(x, y), b = φ(x+1, y)
if a and b have opposite signs:
crossing at x + a / (a − b) # linear interpolation
The checks are B1(b) and B1(c), on the 5 \times 5 table.
C3. normal(phi, x, y): \phi_x = \tfrac12(\phi(x+1, y) - \phi(x-1, y)), \phi_y likewise, the length |\nabla\phi| and the unit normal (\phi_x, \phi_y)/|\nabla\phi|. The checks are B2(a) and B3.
C4. curvature(phi): at every pixel
φx, φy = central differences, as in C3
φxx = φ(x+1, y) − 2 φ(x, y) + φ(x−1, y) φyy likewise
φxy = (φ(x+1, y+1) − φ(x+1, y−1) − φ(x−1, y+1) + φ(x−1, y−1)) / 4
κ = (φxx φy² − 2 φx φy φxy + φyy φx²) / (φx² + φy²)^(3/2)
The checks are B2(b), and a circle of radius 20 pixels stored three ways (distance, three times the distance, x^2 + y^2 - r^2): \kappa = 1/\rho for all three.
Then cargo run -p levelsets -- implicit and open the pictures in out/implicit/. The program cuts the union of two circles of radius 1 at (\pm1.2, 0) at several heights c and counts the pieces of \phi < c; it measures the curvature of the level line through (1, 0) of the table’s \phi on finer and finer grids; and it colours the curve r = 1 + 0.2\cos 3\theta by its curvature (red: \kappa > 0, blue: \kappa < 0).
C5. At which height c does the number of pieces change? Explain the value. Compare two-circles-c0.1.png and two-circles-c0.3.png.
C6. How does the curvature at (1, 0) approach the exact value 1 as the grid spacing h shrinks? Compare with B2. (math) About how fast does the error shrink when h is halved?
C7. In lobes-curvature.png, where is the curve red and where blue? Compare the printed values at the tip and in the dent with the slide “Normal and curvature, read from φ”.
The model solution is levelsets/src/implicit/solution.rs in the download.
C5. Two pieces for c = 0, 0.1 and 0.19; one piece for c = 0.21 and 0.3. The value of \phi at the origin, the saddle point between the two circles, is 1.2 - 1 = 0.2: below it the region \phi < c consists of two disks of radius 1 + c that do not touch, above it they overlap. In the pictures the level c = 0.1 gives two separate blue disks, the level c = 0.3 one peanut-shaped region. The function is the same in both; only the cut changed.
C6. \kappa = 0.828 with h = 1 (the answer of B2), 0.944 with h = 0.5, 0.985 with h = 0.25 and 0.998 with h = 0.1. The errors are 0.172, 0.056, 0.015, 0.002: halving h divides the error by about 3 to 4, close to 4. Central differences have an error proportional to h^2 (Taylor, as in Filtering & convolution), so the curvature does too, once h is small compared with the radius.
C7. Red (convex, \kappa > 0) around the three lobes, blue (concave, \kappa < 0) in the three dents. The program gives \kappa = 2.08 at the tip (1.2, 0) and -1.56 in the dent at radius 0.8, the exact values to two decimals; the slide rounds them to +2.1 and -1.6.
Where this leads
So far \phi was fixed and we moved the cutting plane. The next unit keeps the cut at \phi = 0 and changes \phi instead, a little at every time step, so that the curve moves with a chosen speed: the level set equation. Merging and splitting will come for free, for the reason seen in E2.