Exercise: shape features
Before this unit: Region properties and moments (previous unit); Day 13: Morphology.
Goal
By the end of this exercise you should be able to:
- find the convex hull of a small pixel object, its area with the shoelace formula, the solidity and the extent,
- find the min and max Feret diameters of a polygon with calipers flush with its edges,
- read the inscribed radius and mean radius from a distance transform,
- explain how the form factor depends on the perimeter estimator, the resolution and the units.
Do the pen-and-paper part first, then the demo.
Background
| Feature | Definition |
|---|---|
| equivalent diameter | \sqrt{4A/\pi} (2D); \sqrt[3]{6V/\pi} (3D) |
| convex hull | smallest convex polygon containing the pixel corners (pixel (x, y) covers [x, x+1] \times [y, y+1]) |
| shoelace | A = \frac12\sum_i (x_i y_{i+1} - x_{i+1} y_i) over the vertices in order |
| solidity, extent | A/A_{\text{hull}}; A/A_{\text{box}} |
| form factor | 4\pi A/P^2 (1 for a disc) |
| Feret | w(\theta) = width in direction \theta; max Feret = largest distance between two hull vertices; min Feret = smallest width, with a caliper flush with a hull edge |
| inscribed radius | \approx \max \mathrm{DT} - \tfrac12 (DT between pixel centres) |
By hand
B1. A 22-pixel object in a 6 \times 6 box (1 = object; x = column, y = row):
1 1 0 0 0 0
1 1 0 0 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 0 0
0 0 1 1 0 0
- Find the convex hull of the pixel corners: list its vertices in order. (Only the first and last pixel of each row matter.)
- Its area with the shoelace formula, the solidity and the extent.
- Now the hull of the pixel centres (x + \tfrac12, y + \tfrac12): vertices, area, solidity. What is strange?
B2. The convex polygon (0, 0), (6, 0), (8, 3), (5, 6), (0, 4). (a) Its area (shoelace). (b) For each edge, find the vertex farthest from the line through that edge and its distance. The smallest of these is the min Feret: which edge, and how large? (c) The max Feret: the largest distance between two vertices.
B3. The Euclidean distance transform of a 29-pixel object (distance from each object pixel centre to the nearest background pixel centre; “.” = background):
. . . . . . . . .
. 1 1 1 1 1 1 1 .
. 1 2 2 2 2 2 1 .
. 1 2 2.24 1.41 1 1 1 .
. 1 2 2 1 . . . .
. 1 1 1 1 . . . .
. . . . . . . . .
- The maximum inscribed radius (≈ max DT − ½) and where its centre is.
- The mean of DT over the object.
- Why is the value 2.24 where it is, and why is the pixel to its right only 1.41?
B4. A 4 \times 4 square of pixels. (a) Its form factor with the edge-count perimeter. What is the true form factor of a square? (b) Its form factor with the chain code through the pixel centres (perimeter 12). Comment.
B5. Predict the effect of segmentation errors. A disc of radius 22 pixels is segmented one pixel too large all round. By how much (in %) do the area, the perimeter, the equivalent diameter and the form factor change? And for a rod 10 pixels wide and 40 long?
B6. Name collisions. For a disc, a square and a 2 \times 1 rectangle, compute P^2/(4\pi A) and the second-moment compactness 2\pi\,\overline{r^2}/A (\overline{r^2} = mean squared distance from the centroid). Which of the two is more sensitive to a ragged outline?
B1. (a) Candidate corners per row: row 0: x from 0 to 2; row 1: 0 to 6; rows 2–3: 0 to 6; rows 4–5: 2 to 4. The hull: (0,0), (2,0), (6,1), (6,4), (4,6), (2,6), (0,4). (b) Shoelace terms x_i y_{i+1} - x_{i+1}y_i: 0, 2, 18, 20, 12, 8, 0, sum 60, area 30. Solidity 22/30 = 0.733. Extent 22/36 = 0.611. (c) Centre hull: (0.5, 0.5), (1.5, 0.5), (5.5, 1.5), (5.5, 3.5), (3.5, 5.5), (2.5, 5.5), (0.5, 3.5), area 19: “solidity” 22/19 = 1.16, more than 1. The centre hull is half a pixel inside the object all round, so it is smaller than the pixel area: the two conventions differ by about half a pixel times the perimeter, which is a lot for small objects.
B2. (a) Shoelace: 0 + 18 + 33 + 20 + 0 = 71, area 35.5. (b) Edge (0,0)–(6,0): farthest (5,6), 6.00. Edge (6,0)–(8,3): farthest (0,4), 7.21. Edge (8,3)–(5,6): farthest (0,0), 7.78. Edge (5,6)–(0,4): farthest (6,0), 5.94. Edge (0,4)–(0,0): farthest (8,3), 8.00. Min Feret 5.94, with a caliper along the edge (5,6)–(0,4): the distance of (6,0) from that line is \frac{|(0-5)(0-6) - (4-6)(6-5)|}{\sqrt{5^2 + 2^2}} = 32/\sqrt{29} = 5.94. (c) The largest vertex distance is between (0,0) and (8,3): $ = $ 8.54.
B3. (a) Max DT 2.24 (\sqrt 5) at row 3, column 3: inscribed radius ≈ 1.74, centred there. (b) The values sum to 38.65 over 29 pixels: mean 1.33. (c) From (3,3) the nearest background pixel centres are (5,4) and (4,5) in (row, column), at \sqrt{2^2 + 1^2} = \sqrt5; straight up, down or left the background is 3 away. Its right neighbour (3,4) is diagonal to the background pixel (4,5): \sqrt2.
B4. (a) A = 16, P = 16: 4\pi \cdot 16/256 = \pi/4 = 0.785. A square has form factor \pi/4 exactly (true P = 16), so the edge count is right for an axis-aligned square. (b) 4\pi\cdot 16/144 = 1.40: above 1, impossible for a real shape: the chain code through the centres runs half a pixel inside the true boundary (12 instead of 16).
B5. Disc: area \approx \pi r^2 \to \pi(r+1)^2: +9.3 % (\approx 2/r); perimeter +1/r = +4.5 %; equivalent diameter +4.5 %; form factor \propto A/P^2 unchanged (both scale like a disc of radius r + 1). Rod 10 \times 40: area 400 \to 12 \cdot 42 = 504, +26 % (the demo, with round caps: +21 %); perimeter 100 \to 108, +8 %; form factor \propto A/P^2: 1.26/1.17 = +8 %. Thin objects suffer much more.
B6. Disc: 1 and 1. Square (side 1): 16/(4\pi) = 1.27 and 2\pi\cdot\frac{1}{6} = 1.05. Rectangle 2\times1: 36/(8\pi) = 1.43 and 2\pi\cdot\frac{5/12}{2} = 1.31. The perimeter-based one: a ragged outline adds a lot of length but hardly moves the pixels’ mean squared radius.
Math questions
B7. (math) Prove the shoelace formula for a triangle with one vertex at the origin, then for any polygon by splitting it into triangles from the origin. Explain the role of the sign.
B8. (math) Show that for a convex polygon the width function w(\theta) is, between two consecutive “events”, of the form c\,|\cos(\theta - \varphi)|, and conclude that the min width is attained with one caliper flush with an edge.
B9. (math) Show that dilating a convex object by a disc of radius d adds the area P d + \pi d^2 (Steiner’s formula; hint: rectangles along the edges and sectors at the vertices). What is the relative change of the area for a disc and for a long thin rod?
B10. (math) The isoperimetric inequality says 4\pi A \le P^2 for every plane shape, with equality only for the disc. Check it for a square and for an equilateral triangle, and explain why a measured form factor above 1 must come from the estimator.
B7. The triangle (0,0), (x_1,y_1), (x_2,y_2) has area \frac12|x_1 y_2 - x_2 y_1| (half the determinant), positive when the vertices run counter-clockwise (in a y-up frame). For a polygon, sum the signed triangles (0, p_i, p_{i+1}): parts outside the polygon are covered once positively and once negatively and cancel. A negative result means the vertices run the other way; take the absolute value. (With image coordinates, y down, counter-clockwise on screen gives a negative sign.)
B8. w(\theta) = \max_i \mathbf p_i\cdot\mathbf u - \min_j \mathbf p_j \cdot \mathbf u. Between events the maximising vertex a and minimising vertex b are fixed, so w = (\mathbf p_a - \mathbf p_b)\cdot\mathbf u_\theta = |\mathbf p_a - \mathbf p_b|\cos(\theta - \varphi), which is positive and concave on the interval (cosine on less than \pi). A concave function attains its minimum at an end point of the interval; at an event one caliper passes through two consecutive vertices, i.e. lies along an edge.
B9. The dilated set is the object plus a band of width d: along each edge a rectangle of area \ell_i d, at each vertex a circular sector whose angles add up to 2\pi (the exterior angles of a convex polygon), together \pi d^2. So \Delta A = Pd + \pi d^2 (for any convex set, by approximation). Disc: \Delta A / A = (2\pi r d + \pi d^2)/(\pi r^2) \approx 2d/r. Rod of width w \ll length L: \approx 2Ld/(wL) = 2d/w: thin objects are the most sensitive.
B10. Square of side s: 4\pi s^2 \le 16 s^2, i.e. \pi \le 4 ✓ (form factor \pi/4). Equilateral triangle of side s: A = \frac{\sqrt3}{4}s^2, P = 3s: 4\pi\frac{\sqrt3}{4} = 5.44 \le 9 ✓ (form factor 0.60). Since no shape exceeds 1, a measured value above 1 means the perimeter was underestimated (or the area overestimated) by the digital estimators.
Explore
The shape canvas of the last unit in its features mode. Overlays (check boxes): the convex hull (pixel corners or centres), the calipers at angle \theta (with the width plot w(\theta) below the table; red dots: min and max Feret), min/max Feret, the largest inscribed circle, the smallest enclosing circle, the circle of equal area, a Fourier outline with k coefficients. The table compares the digitised values with the exact values of the outline. px / µm switches the units (0.25 µm per canvas unit); rows 2× apart makes the pixels twice as tall as wide; mask error grows or shrinks the mask by 1–2 pixels before measuring.
E1. Preset 6 × 6 hull (exercise): check B1 (hull area, solidity, extent) with both hull conventions. What are the max and min Feret?
E2. Preset 4 × 4 square (exercise): check B4 with the three form factor rows.
E3. Preset disc: form factor 0.62 by edge count. Read the three form factors and the solidity. Why is the solidity not 1 for a disc? Then preset small disc: form factor above 1: the three form factors now?
E4. Preset star vs disc: same area, solidity tells: the dashed circle has the same area as the star. Read the solidity, form factor (Crofton) and the Feret diameters, and compare with the disc of the lecture table (solidity 0.96, form factor 1.02).
E5. Preset two touching nuclei: solidity drops: read the solidity and the min/max Feret, and compare with the single disc of E3. Which other features would flag the pair?
E6. Preset rod: min Feret ≈ 2 × inscribed radius. Compare the min Feret, the inscribed radius and the mean radius with the exact rod (length 34 between the cap centres, half-width 5). Move \theta: at which angle is w(\theta) largest?
E7. Preset anisotropic pixels: the circle that is not. In px, read the area, eccentricity and Feret diameters. Switch to µm: what are they now, and what should they be (the disc has radius 5 µm)?
E8. Back on the disc preset: set the mask error to +1 and −1. How do area, perimeter, equivalent diameter and form factor change? Compare with B5.
E1. Corners: hull area 30, solidity 0.733, extent 0.611. Centres: hull area 19, solidity 1.158. Max Feret 7.21 (\sqrt{52}: from (0,0) to (6,4), or to (4,6)), min Feret 5.82 (corner hull).
E2. Edge count 0.785, chain code 1.396, Crofton 1.017; exact (the outline is the pixel boundary) 0.785.
E3. Disc (radius 22): edge count 0.620, chain code 0.928, Crofton 0.993; solidity 0.970. The hull of the corners spans the staircase of the outline, which is not convex: the small triangles at each step are missing from the object. Small disc (radius 3.2): 0.698, 1.290, 1.066.
E4. Star: area 804, solidity 0.536 (exact 0.523), form factor 0.334 (exact 0.316), Feret 48.0 / 44.0. The disc of the same area: 0.96, 1.02, 33.3 / 32.0. Area and equivalent diameter (32.0) are the same; everything about the outline differs.
E5. Solidity 0.917 (exact 0.928), Feret 48.7 / 26.0; the single disc had 0.970. The drop is small but systematic: the notches at the neck are missing from the hull. Also flagging the pair: an area about twice that of a nucleus, a max/min Feret ratio near 2 (48.7/26.0) and an eccentricity of about 0.86 for objects that are otherwise round.
E6. Min Feret 11.33 (exact 10.00: the corner hull adds about one pixel), inscribed radius 4.89 (exact 5), mean radius 2.20; 2 \times 4.89 = 9.8 \approx min Feret. w(\theta) is largest along the rod, \theta \approx 29° (the rod is rotated by 0.5 rad = 28.6°): 44.9 (exact 44 = 34 + 2 · 5).
E7. In px: area 160, eccentricity 0.87, Feret 20.4 / 10.0: the disc looks like an ellipse squeezed to half its height. In µm (rows resampled first): area 80 µm² (true \pi \cdot 5^2 = 78.5), eccentricity 0.20, Feret 10.8 / 10.0 µm (true 10 / 10). The residual eccentricity comes from the coarse rows (each pixel 1 µm tall).
E8. Mask error +1: area 1528 → 1656 (+8.4 %), Crofton perimeter 139.1 → 144.5 (+3.9 %), equivalent diameter 44.1 → 45.9 (+4.1 %), form factor 0.993 → 0.997. −1: area 1404 (−8.1 %), perimeter 133.7, equivalent diameter 42.3. As predicted in B5: area ≈ ±2/r, lengths ±1/r, form factor nearly unchanged.
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 labelling/src/shape_features/exercise.rs. Points are (x, y) with x = column and y = row (down); pixel (x, y) covers [x, x+1] \times [y, y+1].
C1. hull_candidates(mask): for every row, the two left corners of its first object pixel and the two right corners of its last one. Only these can be hull vertices.
C2. convex_hull(points), Andrew’s monotone chain. With \operatorname{cross}(o, a, b) = (a_x - o_x)(b_y - o_y) - (a_y - o_y)(b_x - o_x):
sort the points by x, then by y
hull = empty list
for every point q, left to right:
while hull has ≥ 2 points and cross(second-last, last, q) ≤ 0: remove the last
append q
the same for every point q, right to left (never removing points of the first half)
drop the last point (it is the first one again)
The check is the hull of B1 (a), vertex by vertex.
C3. polygon_area(poly): the shoelace formula. The checks are B1 (b) and (c) (both conventions) and B2 (a).
C4. feret(hull): the min Feret is the smallest, over all edges, of the largest distance of a vertex from the line through that edge (|\operatorname{cross}(a, b, p)| / |b - a|); the max Feret is the largest distance between two vertices. The checks are B2 (b), (c) and the corner hull of B1.
C5. distance_transform(mask): the exact Euclidean distance from every object pixel centre to the nearest background pixel centre, by brute force (every background pixel, and the image edge). The check is B3.
Then cargo run -p labelling -- shape-features and open out/shape-features/hulls.png. The program thresholds the synthetic image of 30 nuclei, labels it, and prints for every object its area, hull area, solidity, Feret diameters, inscribed radius (\max \mathrm{DT} - \tfrac12) and how many true nuclei it contains. Hulls with solidity below 0.9 are red.
C6. Which objects contain more than one nucleus? Does the rule “solidity below 0.9” find all of them, and does it flag any single nucleus? Would the area alone do better?
C7. Compare the min Feret with twice the inscribed radius for the single nuclei and for the red objects. What does the difference tell you?
C8. The brute-force distance transform compares every object pixel with every background pixel. How many comparisons does it make on a 256 \times 256 image that is half object? Why does the program run it on each object cut out separately?
The model solution is labelling/src/shape_features/solution.rs in the download.
C6. The mask has 19 objects; 9 contain two or three nuclei. All 5 red objects (solidity 0.76 to 0.89) are merged nuclei, so the rule raises no false alarm, but it misses the other 4 pairs (solidity 0.92 to 0.94): their nuclei overlap so much that the pair is almost convex. The single nuclei have solidity 0.93 to 0.955. The area alone is no better: the largest single nucleus has 938 pixels, while the smallest pair has 570. Area and solidity together, or the shape of the distance transform (watershed day), are needed.
C7. For single nuclei the min Feret is only 1 to 2.5 pixels more than twice the inscribed radius (e.g. 24.0 against 2 · 11.5; 35.0 against 2 · 16.5; the corner hull adds about a pixel): both measure the width of a convex, round object. For the red objects the gap is larger, 3.4 to 14 pixels (object 7: 25.2 against 2 · 9.5; object 15: 45.0 against 2 · 15.5): the hull bridges the waist, while the largest inscribed disc only fits inside one nucleus.
C8. 32\,768 \cdot 32\,768 \approx 10^9 distance computations: seconds, not milliseconds. Cut out with one pixel of margin, each object (a few hundred to 1700 pixels) needs at most a few million, and the nearest background pixel always lies in the cut-out frame or just outside it. The morphology day computes distance transforms in linear time.
Where this leads
The next unit puts all these numbers into a measurement table, one row per object, and uses them to filter and count objects. The measurements day adds intensity, texture and relations between objects; the skeleton day adds length and branching; the moments day higher-order moment invariants. Low solidity is the cue for splitting touching objects: the watershed day.