Exercise: from skeleton to graph
Before this unit: Medial axis, distance transform and reconstruction (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:
- classify skeleton pixels and build the skeleton graph (nodes, edges, lengths) of a small skeleton,
- compute edge lengths by pixel steps, with weights 1 and √2, and with the corrected estimator, and say how wrong each is,
- prune short terminal edges and redraw the graph,
- use E - V + C to count cycles,
- (math) derive the bias of the length estimators and the degree count of a tree.
Do the pen-and-paper part first, then the demo. Pixels are (row, column), counted from 0.
Background
| Idea | What it means |
|---|---|
| End point / regular / junction | a skeleton pixel with 1 / 2 / ≥ 3 skeleton neighbours (8-neighbourhood) |
| Node | an end point, or a cluster of touching junction pixels |
| Edge | a piece of regular pixels between two nodes; its path runs from a node pixel to a node pixel |
| Steps | axial (left, right, up, down) n_e; diagonal n_o; corners n_c = changes of direction |
| Lengths | pixel steps n_e + n_o; weighted n_e + \sqrt2\,n_o; corrected 0.980\,n_e + 1.406\,n_o - 0.091\,n_c |
| Pruning with k | delete terminal edges (end point to junction) shorter than k; a junction keeps at least two edges; a node left with two edges merges them |
| Cycles | E - V + C (edges − nodes + connected pieces) = number of holes |
By hand
B1. A skeleton in a 9 × 9 image (1 = skeleton):
\begin{matrix} 0&0&0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0&1&1\\ 0&0&1&0&0&0&1&0&0\\ 0&0&0&1&0&1&0&1&0\\ 0&0&0&0&1&0&0&0&0\\ 0&0&0&0&1&0&0&0&0\\ 0&0&0&0&1&0&0&0&0\\ 0&0&0&0&1&0&0&0&0\\ 0&0&0&0&0&0&0&0&0 \end{matrix}
- Write the number of skeleton neighbours in every skeleton pixel. Which pixels are end points, which are junctions?
- Draw the graph: number the nodes, list the edges with the pixels of their paths.
- Give each edge’s length by pixel steps and with weights 1 and √2. What is the total length both ways?
- Compute E - V + C. Does it match the number of holes?
B2. Prune the skeleton of B1 with k = 2 (weighted lengths). (a) Which edges are removed? Redraw the graph. What happens to the junction at (2, 6)? (b) What is the length of the merged edge from (1, 8) to (4, 4)? (c) And with k = 3? (Remember: a junction keeps at least two edges.)
B3. Straight digital lines, drawn with one pixel per step along the longer axis. (a) From (0, 0) to (50, 50): n_e, n_o, the true length, and the three estimates. Errors in %? (b) From (0, 0) to 100 to the right and 50 down (slope ½): here the steps alternate axial, diagonal, axial, …: n_e = 50, n_o = 50, n_c = 99. True length and the three estimates? Errors? (c) A horizontal line of 100 steps: the three estimates? Which estimator is exact here, and which is not?
B4. Graphs and cycles. (a) A skeleton has 2 junctions joined by 3 separate edges (a “θ” shape) and nothing else. E, V, C? How many holes did the mask have? (b) A skeleton consists of two separate pieces: a ring (no nodes; count it as one node with a loop edge) and a Y. E - V + C? (c) A tree has 7 end points and only junctions with 3 edges. How many junctions does it have, and how many edges?
B5. A vessel of radius 6 pixels has a few bumps on its wall, and a real side branch 30 pixels long. Spurs from the bumps are 5–9 pixels long (measured from the junction on the centreline). (a) Why are the spurs at least about as long as the radius? (b) Which pruning length k removes the spurs and keeps the side branch? What goes wrong with k = 4 and with k = 40? (c) The same image also has thin capillaries of radius 1.5. Why is “k = 1.5 \times \mathrm{DT} at the junction” a better rule than a fixed k?
B1. (a) Neighbour counts: (1, 1): 1; (2, 2): 2; (3, 3): 2; (4, 4): 3; (5, 4), (6, 4): 2; (7, 4): 1; (3, 5): 2; (2, 6): 3; (1, 7): 2; (1, 8): 1; (3, 7): 1. End points: (1, 1), (1, 8), (3, 7), (7, 4). Junctions: (4, 4) and (2, 6). (b) Nodes: 1 = (1, 1), 2 = (1, 8), 3 = junction (2, 6), 4 = (3, 7), 5 = junction (4, 4), 6 = (7, 4). Edges:
| edge | nodes | path | n_e | n_o | steps | weighted |
|---|---|---|---|---|---|---|
| a | 1–5 | (1,1) (2,2) (3,3) (4,4) | 0 | 3 | 3 | 4.24 |
| b | 3–5 | (2,6) (3,5) (4,4) | 0 | 2 | 2 | 2.83 |
| c | 2–3 | (1,8) (1,7) (2,6) | 1 | 1 | 2 | 2.41 |
| d | 3–4 | (2,6) (3,7) | 0 | 1 | 1 | 1.41 |
| e | 5–6 | (4,4) (5,4) (6,4) (7,4) | 3 | 0 | 3 | 3.00 |
- Total: 11 steps; weighted 13.90.
- E = 5, V = 6, C = 1: 5 - 6 + 1 = 0. No hole, a tree.
B2. (a) Only edge d (1.41 < 2) is a short terminal edge. Remove (3, 7). The junction (2, 6) is left with two edges (b and c), so it becomes a regular pixel and b and c merge. The graph is a Y: nodes (1, 1), (1, 8), (4, 4), (7, 4); 3 edges. (b) (1, 8) (1, 7) (2, 6) (3, 5) (4, 4): 1 axial and 3 diagonal steps: 1 + 3\sqrt2 = 5.24. (c) With k = 3, edges c (2.41) and d (1.41) are both short terminal edges at (2, 6). Removing both would leave the junction with one edge; it keeps two, so only the shorter, d, is removed. The result is the same as for k = 2. (In the next round, the merged edge is 5.24 long and stays.)
B3. (a) n_e = 0, n_o = 50, true length 50\sqrt2 = 70.71. Steps 50 (−29.3 %); weighted 70.71 (exact); corrected 1.406 \cdot 50 = 70.30 (−0.6 %). (b) True \sqrt{100^2 + 50^2} = 111.80. Steps 100 (−10.6 %); weighted 50 + 50\sqrt2 = 120.71 (+8.0 %); corrected 0.980 \cdot 50 + 1.406 \cdot 50 - 0.091 \cdot 99 = 110.29 (−1.4 %). (c) Steps 100 and weighted 100 are exact; corrected 0.980 \cdot 100 = 98.0 (−2 %): it gives up exactness on axis-aligned lines to be good at all angles.
B4. (a) V = 2, E = 3, C = 1: 3 - 2 + 1 = 2 cycles: the mask had 2 holes (the θ encloses two regions). (b) Ring: V = 1, E = 1. Y: V = 4, E = 3. Total E = 4, V = 5, C = 2: 4 - 5 + 2 = 1: one hole, inside the ring. (c) Degrees: 7 \cdot 1 + 3J = 2E, and for a tree E = V - 1 = 7 + J - 1. So 7 + 3J = 2(6 + J), J = 5 junctions, E = 11 edges.
B5. (a) A spur starts at the centreline (where the junction is) and has to reach the bump on the wall: about one radius, plus the bump. (b) Any k between 9 and 30, e.g. k = 15 (2.5 × the radius). k = 4 removes none of the spurs; k = 40 also removes the real side branch. (c) A fixed k that cleans the wide vessel (≥ 10) would delete real capillary branches of length 5–10, while a k suited to the capillaries (≈ 3) leaves the spurs of the wide vessel. Tying k to the local radius scales the threshold with the vessel: about 9 for the vessel, 2.3 for a capillary.
Math questions
B6. (math) A long straight line at angle \theta \in [0°, 45°] with true length L has n_e = L(\cos\theta - \sin\theta) axial and n_o = L\sin\theta diagonal steps. (a) Show this. (b) Find the ratio estimate / true for pixel steps and for weights 1, √2, as functions of \theta. (c) Find the worst angle and the worst ratio for each.
B7. (math) For weights a (axial) and b (diagonal), the estimate of a line at angle \theta is L(a\cos\theta + (b - a)\sin\theta). Show that requiring exactness at 0° and 45° gives a = 1, b = \sqrt2. Why can no choice of a, b be exact at all angles?
B8. (math) Show that the sum of the degrees of all nodes of a graph is 2E. Use it, with E = V - 1 for a tree, to show that a tree whose junctions all have degree 3 has exactly two more end points than junctions.
B6. (a) The line advances L\cos\theta along x and L\sin\theta along y. Each diagonal step advances one in both, each axial step one in x only: n_o = L\sin\theta, n_e + n_o = L\cos\theta. (b) Pixel steps: (n_e + n_o)/L = \cos\theta. Weighted: \cos\theta - \sin\theta + \sqrt2\sin\theta = \cos\theta + (\sqrt2 - 1)\sin\theta. (c) \cos\theta is worst at 45°: 0.707. The weighted ratio \cos\theta + c\sin\theta with c = \sqrt2 - 1 is largest where \tan\theta = c, i.e. \theta = 22.5°, with value \sqrt{1 + c^2} = 1.082; it equals 1 at 0° and 45°.
B7. At 0°: a = 1. At 45°: L(a + b - a)/\sqrt2 = Lb/\sqrt2 = L, so b = \sqrt2. The estimate a\cos\theta + (b - a)\sin\theta is a combination of \cos\theta and \sin\theta, i.e. R\cos(\theta - \varphi) for some R, \varphi; it can equal 1 at most at two angles in [0°, 45°] (unless constant, which is impossible), so there is always an error in between. Reducing the error everywhere means accepting a small error at 0° and 45°: the corrected estimator.
B8. Every edge has two ends, each counted once in the degree of the node it ends at (a loop counts twice at its node): \sum_i \deg(i) = 2E. For a tree with n end points and J junctions of degree 3: n + 3J = 2E = 2(n + J - 1), so n = J + 2.
Explore
A 256 × 256 mask (grey) is thinned to a skeleton (black) and turned into a graph: nodes (blue = end point, red = junction, purple = loop node) and edges coloured by length, with the length written on each. Pruning with prune k removes terminal edges shorter than k (pink: pruned); tick k relative to the radius DT to prune at k \times \mathrm{DT} instead. length chooses the estimator used for the totals, the colours and pruning. The table lists each edge: its nodes, its length by the three estimators, and its mean radius (DT along the edge). Click or drag on the image to draw (or erase) with the brush.
E1. Preset a Y. Read V, E, C and the three edge lengths (1/√2). Compare the pixel-step and corrected lengths of the two diagonal arms with the vertical stem. What is the mean radius of the arms?
E2. Preset a loop: cycles = holes. Check E - V + C against the number of holes. Where do the four short edges at the corners come from? Paint over one hole with the brush: how do E, V and E - V + C change?
E3. Preset diagonal lines and the √2. Read the three estimates at 0°, 26° and 45° and compare them with B3. At which angle is the 1/√2 estimate worst? What do the step counts n_e, n_o, n_c look like at 26°?
E4. Preset noise makes spurs. How many end points, junctions and edges, and what total length? Set k = 12: how many spurs go? Then k = 24 (preset prune them). Why does k have to be so large here, compared with the bumps (radius 2–5)?
E5. Back to noise makes spurs, tick k relative to the radius DT and find the smallest slider value that gives the T graph (3 ends, 1 junction). Explain with B5.
E6. Preset a thresholded vessel crop (k = 8). Set k = 0: how many end points and edges? Compare the total length with and without pruning: by how much do spurs inflate it?
E7. Choose draw your own and draw a skeleton-like shape with a loop and two side branches. Predict E, V, C before looking.
E1. V = 4 (3 ends, 1 junction), E = 3, C = 1. Lengths (1/√2): 120.4 and 121.4 for the arms, 92.4 for the stem. Pixel steps: 91 and 92 for the arms against 92 for the stem, although the arms are about 30 % longer: diagonal steps counted as 1. Corrected: 115.9, 116.6, 90.5. Mean radius about 5.2 to 5.5 (the arms taper).
E2. E = 14, V = 12 (4 ends, 8 junctions), C = 1: 14 - 12 + 1 = 3 = 3 holes. The four short edges run into the corners of the square plate: the corner forks of the medial axis. Filling one hole removes its loop: the graph loses (at least) one cycle, E - V + C = 2.
E3. 0°: true 170.0; steps 170.0, 1/√2 170.0, corrected 166.6. 26°: true 170.4; steps 153.0, 1/√2 184.1, corrected 168.2. 45°: true 169.7; steps 120.0, 1/√2 169.7, corrected 168.7. The 1/√2 estimate is worst around 22.5° (+8 %; B6). At 26°: n_e = 78, n_o = 75, n_c = 150: nearly every step changes direction.
E4. 19 end points, 16 junctions, 34 edges, total 632.5. k = 12 removes only 3 spurs. k = 24 removes 16 spurs: 3 ends, 1 junction, 3 edges, 393.7. The bar is 32 pixels wide: a spur must run from the centreline 16 pixels to the wall before it reaches its bump, so most spurs are 16–23 long, although the bumps are tiny (B5).
E5. At slider value 6 (k = 1.5 × DT) the result is the T: 3 ends, 1 junction, 3 edges, 393.7, the same as with k = 24. Below that, some spurs survive. A spur is about one radius long, so a threshold of 1.5 radii removes spurs at any vessel width.
E6. Without pruning: 74 end points, 142 edges, total length 1011.5 (1/√2). With k = 8: 10 end points, 17 edges, 866.0. The spurs add 145.5 pixels, 17 % of the length.
E7. For a loop with two side branches attached at different places: 2 end points + 2 junctions on the loop, V = 4; edges: 2 branches + 2 loop arcs between the junctions, E = 4; C = 1; E - V + C = 1.
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_graph/exercise.rs. The code indexes pixels as img[(x, y)] = (column, row), while this page writes (row, column).
C1. neighbour_count(skel): the number of skeleton pixels among the 8 neighbours of every skeleton pixel. The check is B1(a).
C2. path_steps(path): the step counts n_e, n_o, n_c of a path of pixels; the three length estimators are given (pixel_steps, weighted, corrected). The checks are the lines of B3.
C3. build_graph(skel): nodes and edges:
node pixels = skeleton pixels with a neighbour count other than 2
node = a group of touching junction pixels (count ≥ 3), or a single other node pixel
for every node pixel p, for every skeleton neighbour q of p:
if q belongs to another node: edge p–q (once per pair of nodes)
else if q is not yet visited:
path = [p, q]
walk on: from each regular pixel, go to its neighbour you did not come from,
until you reach a node pixel; mark the regular pixels visited
edge from the node of p to that node, along path
regular pixels never visited lie on a closed loop: make one of them a node, walk round
The checks are the graph of B1(b) and (c), and a cross whose thick middle is one node.
C4. cycles(graph): E - V + C, with C the number of connected pieces of the graph. The checks are B1(d), the θ of B4(a) and the ring and Y of B4(b).
C5. prune(skel, k): two rounds of
build the graph
for every junction j (3 or more edges):
spurs = terminal edges at j (other end: an end point) with weighted length < k
delete the pixels of the shortest spurs (not those of j), at most (edges of j) − 2 of them
The checks are B2 with k = 2 and k = 3.
Then cargo run -p skeleton -- skel-graph and open the pictures in out/skel-graph/ (grey: mask; edges yellow and green in turn; blue: end points; red: junctions; pink: pruned). The program first measures digital straight lines of about 170 pixels at angles from 0° to 45° with the three estimators. Then it thins a thick T (32 pixels wide) with random bumps on its edge, builds the graph, prunes it with k = 12 and k = 24, and builds the graph of a square plate with three holes. (The thinning is given in the program, not an exercise here.)
C6. In the table of straight lines, where is each estimator worst, and by how much? Compare with B3 and B6.
C7. How many end points, junctions and edges does the bumpy T have before pruning, and after pruning with k = 12 and k = 24? Why does k have to be so large? By how much did the spurs inflate the total length?
C8. Check E - V + C for the plate (plate-graph.png). Does the skeleton have corner branches like those in the lecture’s demo?
The model solution is skeleton/src/skel_graph/solution.rs in the download.
C6. Pixel steps are exact at 0° and worst at 45°: 0.707 of the true length (−29 %, as B3(a)). Weights 1 and √2 are exact at 0° and 45° and worst around 22°: 1.082 (+8 %, B6: the maximum \sqrt{1 + (\sqrt2 - 1)^2} = 1.082 at 22.5°). The corrected estimator stays between 0.980 (at 0°, −2 % as B3(c)) and 1.007: within 2 % at every angle. At 26° the line has 78 axial and 75 diagonal steps and 149 corners: nearly every step changes direction.
C7. Not pruned: 10 end points, 8 junctions, 17 edges. k = 12: 7, 5, 11 (three spurs gone). k = 24: 3 end points, 1 junction, 3 edges: the T. A spur starts at the centre line and must reach the bump on the wall, 16 pixels away, so most spurs are longer than 12 (B5 of the exercise above). The corrected total length drops from 401.6 to 324.1: the spurs added 77.5 pixels, 24 % of the length of the T’s skeleton.
C8. E = 6, V = 4 (four junctions, no end points), C = 1: 6 - 4 + 1 = 3, the three holes. The skeleton has no branches into the plate’s corners: the thinning used here cuts corners off instead of leaving forks (the lecture’s demo, which thins with templates, finds four corner edges and E = 14, V = 12; the cycle count is 3 either way).
Where this leads
The graph turns a skeleton into numbers: lengths, branch points, loops, radii. The next unit puts it to work on neurons and vessel networks: a tube-enhancing filter (the vesselness of the edges day) to get a mask, then skeleton, pruning, graph, and the standard measurements: total length, branching, Sholl analysis, radius and tortuosity, and how to check them against a known truth.