Exercise: medial axis and reconstruction
Before this unit: Hit-or-miss and thinning (previous unit); Day 14: Labelling; Day 2: Edges, blobs and ridges.
See also: Day 15: Watershed; Day 16: Measurements.
Goal
By the end of this exercise you should be able to:
- find the maximal discs (the medial axis) of a small shape from its distance transform, and compare them with a thinning result,
- compute Lantuéjoul’s sets S_r by erosions and openings,
- reconstruct a shape from its skeleton and radii, and spot redundant skeleton points,
- explain why the medial axis is sensitive to the boundary,
- (math) prove the reconstruction property and the link between Lantuéjoul’s formula and the maximal-disc test.
Do the pen-and-paper part first, then the demo. Pixels are (row, column), counted from 0.
Background
| Idea | What it means |
|---|---|
| Distance transform | \mathrm{DT}(p) = steps from p to the nearest background pixel (1 on the boundary) |
| Disc of radius r at p | the pixels q with d(p, q) < r: a diamond (city-block), a square (chessboard) or a round disc (Euclidean) |
| Largest disc at p | radius \mathrm{DT}(p); with city-block or chessboard steps, radius r covers the pixels at most r - 1 steps away |
| Maximal disc | not contained in any other disc inside the shape. On the grid: p is on the axis if no neighbour q has \mathrm{DT}(q) \ge \mathrm{DT}(p) + d(p, q) (4 neighbours for city-block, 8 for chessboard) |
| Reconstruction | the union of the maximal discs is the shape |
| Lantuéjoul | S_r = (erode r times by B) minus (its opening by B); skeleton = \bigcup S_r; shape = \bigcup (S_r dilated r times by B) |
By hand
B1. A 5 × 9 rectangle (rows 1–5, columns 1–9 of a 7 × 11 image). Its city-block distance transform:
\begin{matrix} 1&1&1&1&1&1&1&1&1\\ 1&2&2&2&2&2&2&2&1\\ 1&2&3&3&3&3&3&2&1\\ 1&2&2&2&2&2&2&2&1\\ 1&1&1&1&1&1&1&1&1 \end{matrix}
- Is the corner pixel (1, 1) (value 1) a maximal-disc centre? And (1, 2)? Is (2, 2) (value 2)? (3, 4) (value 3)?
- Mark all maximal-disc centres. How many are there, and what shape do they form?
- Thinning with the 8 templates of the last unit gives the skeleton below. Compare it with (b): which is connected, which is symmetric, which would reconstruct the rectangle?
\begin{matrix} 1&0&0&0&0&0&0&0&1\\ 1&0&0&0&0&0&0&1&0\\ 0&1&1&1&1&1&1&1&0\\ 0&1&0&0&0&0&0&1&0\\ 1&0&0&0&0&0&0&0&1 \end{matrix}
- Repeat (b) with chessboard distances (square discs). What changes at the ends, and why?
B2. A 5 × 5 square (in a 7 × 7 image). Compute Lantuéjoul’s sets S_0, S_1, S_2: (a) with B = the 3 × 3 square; (b) with B = the 3 × 3 cross. (c) For each, reconstruct the square from the S_r (dilate S_r by B r times) and check that you get it back.
B3. A skeleton with chessboard radii: (4, 4) with r = 3, (4, 5) with r = 3, (4, 6) with r = 2, (4, 7) with r = 1 (a disc of radius r covers the pixels at most r - 1 chessboard steps away). (a) Draw the union of the four discs. How many pixels has the shape, and what is it? (b) Which skeleton points can be dropped without changing the shape? Which are the maximal-disc centres of the shape you drew?
B4. A rectangle 60 wide and 20 high is set on fire along its whole boundary. (a) When does the fire reach the centre? Where do the fronts from the two long sides meet, and from which time on? (b) Sketch the medial axis. How long is the middle segment, and how long is each branch to a corner? (c) A bump of size 1 is added in the middle of the top side. What changes in the medial axis, and in a skeleton you would use to measure the rectangle’s length?
B5. You want to measure (a) the length and (b) the local width of a blood vessel from its mask. Which do you use: a thinning skeleton, the medial axis, or both? Explain.
B1. (a) (1, 1): its 4-neighbours (1, 2) and (2, 1) have \mathrm{DT} = 1, not 2: no neighbour’s disc contains its disc: maximal (a diamond of radius 1 is just the pixel, and no larger diamond fits into the corner). (1, 2): its neighbour (2, 2) has \mathrm{DT} = 2 = 1 + 1: not maximal. (2, 2): neighbours (1, 2), (2, 1) have 1, (3, 2), (2, 3) have 2; none has 3: maximal. (3, 4): neighbours 2, 2, 3, 3: maximal. (b) 13 pixels: the 4 corners (1, 1), (1, 9), (5, 1), (5, 9) with \mathrm{DT} = 1; (2, 2), (2, 8), (4, 2), (4, 8) with 2; the middle row (3, 3) to (3, 7) with 3. A segment with forks to the four corners, an X at each end, symmetric. Here it is 8-connected (each pixel touches the next diagonally); for larger and curved shapes the maximal-disc set often breaks up. (c) The thinning result is connected and one pixel wide, but not symmetric (left and right ends differ, as the templates are applied in a fixed order) and not exactly centred at the ends. Only the maximal-disc centres with their radii reconstruct the rectangle exactly (union of diamonds). (d) With chessboard distances, the corners are no longer centres: a square disc of radius 2 at (2, 2) contains the corner (1, 1). The maximal discs are only the middle row (3, 3) to (3, 7), the squares of radius 3. Square discs fit square corners, so the axis does not fork.
B2. (a) Square: A \ominus B = the inner 3 × 3, and the 5 × 5 square is its own opening (the square fits everywhere): S_0 = \emptyset. A \ominus 2B = the centre pixel; the 3 × 3 is its own opening: S_1 = \emptyset. The centre pixel is removed by the opening: S_2 = {centre}. Skeleton: one pixel with radius 2. (b) Cross: the opening of the square by the cross misses the 4 corners: S_0 = the 4 corners (1, 1), (1, 5), (5, 1), (5, 5). A \ominus B = inner 3 × 3, its opening misses its corners: S_1 = (2, 2), (2, 4), (4, 2), (4, 4). S_2 = the centre. Skeleton: the two diagonals, 9 pixels. (c) Square: the centre dilated twice by the 3 × 3 square is the 5 × 5 square. Cross: the centre dilated twice by the cross is a diamond of 13 pixels; adding S_1 dilated once (4 crosses at the diagonal pixels) fills (1, 2), (2, 1) … and adding the 4 corners (S_0) completes the square: 25 pixels both times.
B3. (a) (4, 4), r = 3: rows 2–6, columns 2–6. (4, 5), r = 3: rows 2–6, columns 3–7. (4, 6), r = 2: rows 3–5, columns 5–7. (4, 7), r = 1: the pixel itself. Union: rows 2–6, columns 2–7, a 5 × 6 rectangle of 30 pixels. (b) (4, 6) and (4, 7) lie, with their discs, inside the disc of (4, 5): drop them. The maximal-disc centres of the 5 × 6 rectangle are (4, 4) and (4, 5), both with chessboard \mathrm{DT} = 3.
B4. (a) The centre is 10 from the long sides and 30 from the short ones: the fire reaches it at t = 10. Fronts from the two long sides meet on the horizontal midline, at t = 10, wherever the short sides are farther away: from x = 10 to x = 50. (b) A midline segment of length 60 - 20 = 40, and from each end two diagonal branches at 45° to the corners, each 10\sqrt2 = 14.1 long. (c) The bump gets its own maximal discs: a new branch runs from the midline up to the bump (10 long). For measuring the length you would want to ignore it (and the four corner branches): prune short branches, or smooth the mask first.
B5. (a) Length: a thinning skeleton (connected, one pixel wide, so its length can be counted along a path). The pixel-level medial axis may be broken or two pixels wide. (b) Width: the distance transform read on the skeleton: 2\,\mathrm{DT} is the local diameter, which is the radius information of the medial axis. In practice: both, thinning for the line and \mathrm{DT} along it.
Math questions
B6. (math) Show that every shape is the union of its maximal discs: A = \bigcup_{p \in S(A)} D(p, \mathrm{DT}(p)). (Use that A is finite on the grid.)
B7. (math) With B = the 3 × 3 cross and \mathrm{DT} the city-block distance transform (1 on the boundary), show that A \ominus rB = \{\mathrm{DT} \ge r + 1\} and that S_r = the pixels with \mathrm{DT} = r + 1 that have no 4-neighbour with \mathrm{DT} = r + 2.
B8. (math) For the continuous rectangle [0, a] \times [0, b] with a > b: show that a point (x, y) with b/2 < x < a - b/2 is on the medial axis exactly when y = b/2, and that near the corner (0, 0) the axis is the diagonal y = x for 0 \le x \le b/2. What is the total length of the axis?
B6. Each disc D(p, \mathrm{DT}(p)) lies in A, so the union is in A. Conversely, take x \in A; its own disc contains it. If that disc is not maximal, it lies in a strictly larger disc in A; repeat. On a finite grid there are finitely many discs in A, and the chain of strictly growing discs must end at a maximal disc, which contains x.
B7. p \in A \ominus rB iff the diamond of radius r around p lies in A iff every pixel at city-block distance \le r is object iff the nearest background pixel is at distance \ge r + 1, i.e. \mathrm{DT}(p) \ge r + 1. A pixel of E = \{\mathrm{DT} \ge r + 1\} survives the opening E \circ B iff it lies in a cross q + B \subseteq E, i.e. q \in E \ominus B = \{\mathrm{DT} \ge r + 2\} with q = p or a 4-neighbour of p. If \mathrm{DT}(p) \ge r + 2, take q = p. If \mathrm{DT}(p) = r + 1, p survives iff a 4-neighbour has \mathrm{DT} \ge r + 2 (it can only be r + 2, as \mathrm{DT} changes by at most 1 between 4-neighbours). So S_r = E \setminus (E \circ B) = the pixels with \mathrm{DT} = r + 1 and no 4-neighbour with r + 2: the maximal-disc test.
B8. \mathrm{DT}(x, y) = \min(x, a - x, y, b - y). In the middle part (b/2 < x < a - b/2) the short sides are farther than b/2 \ge \min(y, b - y), so \mathrm{DT} = \min(y, b - y): a tent across, with its crest on y = b/2, where the two nearest boundary points (one on each long side) are both at distance b/2. Near the corner, for x, y < b/2: \mathrm{DT} = \min(x, y), whose crest (two nearest boundary points, one on each side of the corner) is y = x. Total: midline a - b plus four diagonals of length \tfrac{b}{2}\sqrt2: a - b + 2\sqrt2\,b. For 60 \times 20: 40 + 56.6 = 96.6.
Explore
The shape is a polygon rasterised at 256 × 256: drag the white handles to change it. Left: the distance transform as colour (iso-lines every 8 steps) and the axis in red, computed by maximal discs, Lantuéjoul (colour = r of S_r) or thinning; the metric sets the disc shape. Click near the axis to see its maximal disc. grassfire t burns the shape: the front (orange) is the iso-line \mathrm{DT} = t, and axis pixels light up once burnt. λ keeps only the maximal-disc pixels whose two nearest boundary points are at least λ apart. Bump adds a small disc on the boundary. Right: the union of all axis discs, with the missing pixels in red, and the readout.
E1. Preset a rectangle forks at the ends (176 × 72 pixels, Euclidean, λ = 8). Where does the middle segment of the axis start and end? Compare its length with B8. What is the radius of the disc shown near the top-left corner?
E2. Same preset. Set λ to off: what happens to the axis graph and to the reconstruction? Then switch the metric to city-block and to chessboard: how many end points does the axis have in each case, and why does the chessboard axis not fork?
E3. Choose maximal discs, then thinning, then Lantuéjoul for the rectangle (city-block). Which reconstruct it exactly? How many pixels does the thinning reconstruction miss, and where?
E4. Preset one bump, one branch. Compare the axis graph with bump size 0 and 3. Move the bump around the boundary. From which bump size does the new branch fork at its end?
E5. Preset grassfire (dumbbell, t = 22). Which part has burnt? Move t: at which time does the fire burn through the bar, and when does it reach the centres of the two discs? Read DT there (click on the axis).
E6. Preset reconstruct the star from its axis (Lantuéjoul, square). Is the reconstruction exact? Is the skeleton connected? Now choose thinning: connected? exact?
E7. Preset the ring keeps its loop. How many loops does the thinned axis have, and why can no method remove it? Drag the inner polygon until it touches the outer one: what happens to the ring and to the axis?
E1. The middle segment runs along rows 127–128 from about x = 75 to x = 180, about 105 pixels. B8 predicts a - b = 176 - 72 = 104 for the continuous rectangle. The disc at (45, 97), near the top-left corner on the diagonal branch, has radius 6.
E2. Without λ the Euclidean maximal-disc set has 340 pixels in 5 pieces with 12 end points (the diagonal branches break into pixels), but it reconstructs the rectangle exactly. With λ = 8: 332 pixels, 1 piece, 4 end points, but 12 pixels missing in the reconstruction (discs near the corners were dropped). City-block: 4 end points, exact. Chessboard: 2 end points (just the midline, 212 pixels), exact: a square disc fills a square corner, so no smaller disc is needed there.
E3. Maximal discs and Lantuéjoul both reconstruct the rectangle exactly (for city-block, both give the same 352 pixels). Thinning (248 pixels) misses 139 pixels with city-block discs, at the corners: the thinned line does not run far enough into the corners, so no disc covers them.
E4. Without the bump: 4 end points, 5 branches. With bump 3: 5 end points, 7 branches (one new spur, which also splits the edge it leaves into two). The spur follows the bump around the boundary. From bump size 4 on, the spur itself forks at its end (6 end points, 9 branches), like a small rectangle end.
E5. At t = 22 the outer 73 % of the shape has burnt: the whole bar and the outer rings of the two discs. The bar is 28 pixels high: its middle burns at t = 14 (DT 14 on the bar’s axis). The disc centres burn last, at t ≈ 46 (DT 45.88 at (68, 128) and (188, 128)).
E6. Lantuéjoul’s reconstruction is exact, but its skeleton (498 pixels) falls into 124 separate pieces: the S_r of neighbouring r do not touch. Thinning: connected (1 piece, 5 end points, one per arm of the star), but the reconstruction misses 653 pixels, mostly in the arm tips.
E7. One loop: the axis must separate the hole from the outside, and every method keeps it (thinning removes only simple pixels; the maximal discs on both sides of the hole are distinct). If the inner polygon touches the outer one, the ring is cut: the hole merges with the background, and the axis becomes a curved line with two ends.
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 skeleton/src/skel_medialaxis/exercise.rs. The code indexes pixels as img[(x, y)] = (column, row), while this page writes (row, column). Erosion and dilation by the 3 × 3 cross or square are given.
C1. distance_transform(mask, metric), city-block or chessboard, in two raster passes:
DT = 0 on the background, a large number on the object
forwards (row by row from the top left), for every object pixel p:
DT(p) = min(DT(p), DT(q) + 1) for the neighbours q already visited
(city-block: left, above; chessboard: also above-left, above-right)
backwards (from the bottom right): the same with the mirrored neighbours
Outside the image counts as background. The check is the distance transform of B1.
C2. medial_axis(dt, metric): the maximal-disc centres, the object pixels p with no neighbour q (one step away) where \mathrm{DT}(q) \ge \mathrm{DT}(p) + 1. The checks are B1(b) (13 pixels, city-block) and B1(d) (5 pixels, chessboard).
C3. reconstruct(axis, dt, metric): the union of the discs:
for every axis pixel p:
r = DT(p) − 1
mark every pixel q with dist(p, q) ≤ r
The checks are B1 (the rectangle comes back, both metrics) and B3 (the 5 × 6 rectangle).
C4. lantuejoul(mask, cross): the sets S_r:
E = mask; r = 0
while E is not empty:
S_r = E minus dilate(erode(E)) # E minus its opening
E = erode(E); r = r + 1
The checks are B2(a) and (b), and B7: with the cross, S_r are exactly the maximal-disc centres with \mathrm{DT} = r + 1.
C5. reconstruct_lantuejoul(skeleton, cross): the union of S_r dilated r times. Instead of dilating each S_r on its own, go from the largest r down: “dilate once, add the next S_r”. The check is B2(c).
Then cargo run -p skeleton -- skel-medialaxis and open the pictures in out/skel-medialaxis/ (grey: the distance transform, red: the skeleton). For a rectangle of 176 × 72 pixels, a star, an ellipse and the same ellipse with a bump of radius 3 on its lower edge, the program prints the size of the axis for both metrics and for Lantuéjoul’s formula (with the square), the number of separate pieces, and the number of pixels the reconstruction gets wrong.
C6. Compare rectangle-city-block.png and rectangle-chessboard.png, and the pixel counts. Explain the difference with B1(d). How long is the middle segment? Compare with B8.
C7. Every reconstruction is exact. Yet how many pieces does the axis of the star and of the ellipse have? What would that mean for measuring the length of a shape with it?
C8. Compare ellipse-city-block.png with ellipse-bump-city-block.png near the bump. What did the bump (13 pixels added to a shape of 15 701) change, in the axis and in the distance transform?
The model solution is skeleton/src/skel_medialaxis/solution.rs in the download.
C6. City-block: 352 pixels, a midline with four diagonals to the corners (an X at each end). Chessboard: 212 pixels, only the midline: a square disc fits a square corner, so the corner pixels lie inside one large disc, as in B1(d). The rectangle has an even height, so the midline is two rows thick: 212 = 2 \cdot 106 pixels, from x = 75 to x = 180, about 105 long. The continuous medial axis of B8 has a middle segment of a - b = 176 - 72 = 104. Lantuéjoul with the square gives exactly the chessboard axis (212 pixels).
C7. The star’s axis falls into 35 pieces (city-block) or 121 (chessboard), the ellipse’s into 61 or 79, although every one rebuilds its shape without a single wrong pixel. Along a curved or slanted boundary, the maximal discs sit at scattered pixels and do not form a line. The discrete medial axis is a good shape code, but a poor curve: to measure length or count branches, thin first (connected), and read the radius from the distance transform along the thinned line.
C8. The axis grows from 145 to 148 pixels and from 61 to 62 pieces: a new piece of 3 pixels sits on the bump itself, the centres of the small discs that fill it. In the distance transform a faint crease (a ridge) now runs from the bump up to the middle of the ellipse: there, fronts from the bump and from the rest of the lower edge meet. In the continuous medial axis the crease is a whole new branch, and a thinned skeleton turns it into one (slide “Sensitivity to the boundary”); on the grid, with city-block discs, only its end on the bump survives as maximal discs. Either way, 0.1 % of the area changes the skeleton.
Where this leads
The skeleton is a set of pixels. The next unit turns it into a graph (end points, junctions, branches with lengths), corrects the length for the pixel grid, and prunes the spurs that boundary noise creates. The distance transform on the skeleton becomes the radius of each branch, the key measurement for vessels.