Exercise: hierarchical segmentation and merge trees

Goal

By the end of this exercise you should be able to:

  • build a merge tree by altitude, dynamics and area, and read partitions from it,
  • compute an ultrametric contour map and threshold it,
  • run greedy merging on a region adjacency graph,
  • predict which hierarchy keeps which structure,
  • (math) prove the ultrametric inequality and the link between the dynamics hierarchy and h-minima.

Background

Idea What it means
Merge tree leaves = initial regions; each merge makes a parent node at a level; levels never decrease going up
Cut at level t apply every merge with level \le t; regions = subtrees below the cut
Altitude hierarchy merge basins across passes in increasing order (= single linkage on pass heights)
Attribute hierarchy flood by altitude; at each meeting, the merge gets the attribute of the smaller region (dynamics = pass − its minimum; area; volume = area × depth); then sort the merges by these values
Ultrametric u(p, q) = level at which p and q first share a region
UCM each boundary gets the u of the regions on its two sides; thresholding gives the partitions
RAG merging always merge the most similar adjacent pair (e.g. smallest difference of means), update, repeat

By hand

B1. A 1D landscape with five basins a–e (left to right): minima 1, 3, 0, 4, 2; passes a|b 5, b|c 4, c|d 7, d|e 6; widths (areas) 10, 2, 8, 3, 6. (a) Build the merge tree by altitude. Which three regions remain after two merges? (b) Compute the dynamics of each basin (c is the global minimum) and build the tree by dynamics. Three regions? (c) Build the tree by area (when two regions meet at a pass, the smaller area dies; the merged region’s area is the sum). Three regions? (d) Build it by volume = area × (pass − minimum) of the smaller region.

B2. The UCM of the dynamics hierarchy of B1 assigns a value to each of the four boundaries a|b, b|c, c|d, d|e. (a) Write the four values. (b) Threshold at 3: which boundaries remain, and which regions? (c) Which threshold gives the h-minima watershed with h = 4.5?

B3. A 3 \times 3 grid of regions with mean grey levels (4-neighbours) \begin{bmatrix} 10 & 12 & 30 \\ 11 & 25 & 31 \\ 40 & 26 & 50 \end{bmatrix}. Run greedy merging by difference of means. Give each merge with its difference until one region is left. What are the three regions just before the last two merges?

B4. Predict. A small bright spot (area 20 pixels, depth 80) and a large faint blob (area 2000, depth 5) sit on a flat background. Which of the hierarchies altitude, dynamics, area and volume keeps the spot longest? Which keeps the blob longest?

B1. (a) Passes in increasing order: b|c 4, a|b 5, d|e 6, c|d 7. Tree: (b, c) at 4; a joins at 5; (d, e) at 6; all at 7. After two merges: \{a, b, c\}, \{d\}, \{e\}. (b) Dynamics = how far you climb before reaching a deeper basin: b (min 3) via b|c 4 to c: 1. d (4) via d|e 6 to e (2): 2. a (1) via a|b 5 to c (0): 4. e (2) via d|e 6, c|d 7 to c: 5. Flooding by altitude: b|c at 4 (b dies, value 1), a|b at 5 (a dies: 5 - 1 = 4 vs 5 - 0 = 5), d|e at 6 (d dies: 2), c|d at 7 ({d, e} dies: 7 - 2 = 5). Sorted: b|c 1, d|e 2, a|b 4, c|d 5. Three regions: \{a\}, \{b, c\}, \{d, e\}. (c) b|c at 4: areas 2 and 8: value 2 ({b, c} has 10). a|b at 5: {a} 10 and {b, c} 10: value 10. d|e at 6: 3 and 6: value 3 ({d, e} has 9). c|d at 7: {a, b, c} 20 and {d, e} 9: value 9. Sorted: b|c 2, d|e 3, c|d 9, a|b 10: b into c, d into e, {d, e} into {b, c}, then a. Three regions: \{a\}, \{b, c\}, \{d, e\}. (d) b|c: 2 \cdot (4 - 3) = 2 vs 8 \cdot 4 = 32: 2. a|b: 10 \cdot 4 = 40 vs 10 \cdot 5 = 50: 40. d|e: 3 \cdot 2 = 6 vs 6 \cdot 4 = 24: 6. c|d: 20 \cdot 7 = 140 vs 9 \cdot 5 = 45: 45. Sorted: 2, 6, 40, 45: b|c, d|e, a|b, c|d: the same order as dynamics here.

B2. (a) a|b 4, b|c 1, c|d 5, d|e 2. (b) Boundaries with value > 3 remain: a|b and c|d. Regions \{a\}, \{b, c\}, \{d, e\}. (c) The h-minima with h = 4.5 keep the minima with dynamics ≥ 4.5: c (global) and e (5): two regions, \{a, b, c\} and \{d, e\}. Threshold the UCM at 4.5: only c|d (5) remains. Same partition.

B3. Step 1: three pairs with difference 1: 10–11, 30–31, 25–26 (ties; any order). Step 2: {10, 11} (mean 10.5) and 12: 1.5. Step 3: {25, 26} (25.5) and {30, 31} (30.5): 5. Step 4: 40 joins {25, 26, 30, 31} (mean 28): 12 (its other neighbour, {10, 11, 12} with mean 11, differs by 29). Step 5: {10, 11, 12} (11) and {25, 26, 30, 31, 40} (30.4): 19.4. Step 6: 50 joins the rest (mean 23.1): 26.9. Before the last two merges: \{10, 11, 12\}, \{25, 26, 30, 31, 40\}, \{50\}.

B4. The spot: deep but tiny. Altitude: its pass is high (bright spot on flat background): kept long. Dynamics 80: kept long. Area 20: dies early. Volume 20 \cdot 80 = 1600, compared with the blob’s 2000 \cdot 5 = 10\,000: dies before the blob. So dynamics (and altitude) keep the spot longest. The blob: area and volume keep it longest; dynamics (5) and altitude (low pass) remove it early.

Math questions

B5. (math) Prove the ultrametric inequality u(p, r) \le \max(u(p, q), u(q, r)) for any merge tree. Show that it implies: of the three distances among any three points, the two largest are equal.

B6. (math) Show that cutting the dynamics hierarchy at level h gives the same partition as the watershed from the h-minima (minima with dynamics \ge h as markers), assuming all dynamics differ from h.

B7. (math) Show that for the single-linkage (altitude) tree, u(p, q) equals the minimax distance between p and q.

B5. At level t = \max(u(p,q), u(q,r)) the region containing q contains both p and r (nesting: once together, always together; regions of a partition are disjoint). So u(p, r) \le t. For three points, suppose u(p, r) is strictly the largest; then u(p, r) > \max(u(p,q), u(q,r)), contradicting the inequality. So the largest value occurs at least twice.

B6. In the altitude flooding, a basin “dies” when it first meets a basin with a deeper minimum; its dynamics is the pass height minus its minimum, which is its extinction value in the dynamics hierarchy. Cutting at h applies exactly the merges of basins whose dynamics is below h: each such basin is merged into the region of the deeper basin it meets. The minima with dynamics ≥ h survive, one region each. That is the h-minima watershed: the h-minima transform fills every basin shallower than h up to its lowest pass, which joins it to a deeper neighbour, and flooding from the remaining minima assigns each pixel accordingly. (Boundary positions on the filled plateaus can differ by tie rules; the regions correspond one to one.)

B7. Merges happen in increasing order of edge weight; p and q first share a region at the weight t of the edge whose union joined their components. Before that, all edges < t did not connect them, so every path between them has an edge \ge t: minimax distance \ge t. After it, they are connected using edges \le t only: minimax distance \le t. Hence u(p, q) = minimax distance.

Explore: merge trees

Initial regions (leaves) are watershed basins of the gradient, FH regions or SLIC superpixels of a synthetic image of touching nuclei, a test scene, an image with a weak spot, or the micrograph. Choose a hierarchy; the left panel shows the partition at the current cut (mean grey levels, yellow boundaries), the middle one the UCM, the bottom the dendrogram (y = merge order; drag to move the cut). For the synthetic images, the readout counts the true objects matched with IoU ≥ 0.5. Click a region to select its branch; merge up and split change the cut on that branch only.

M1. Preset altitude (20 regions). How many of the 18 nuclei are matched? Move the cut: what is the best number you can reach, and at how many regions?

M2. Presets dynamics vs altitude and area at 20 regions: how many nuclei are matched? Which nuclei does the altitude hierarchy lose, and why?

M3. Choose RAG: mean diff. on the nuclei. How many are matched at 20 regions? Why is this criterion worse here, although it was better on the weak-spot image?

M4. Presets cheapest edge leaks… and … mean difference does not (2 regions). What does each keep? Set 3 regions in both.

M5. Preset per-branch cut: find a pair of touching nuclei that is one region, click it and press split. Then find a nucleus in two pieces and press merge up on one piece. Does the matched count go up?

M6. Preset UCM thresholds (micrograph): which boundaries are brightest in the UCM? Change the leaves to SLIC: how does the UCM change?

M1. 13 of 18 at 20 regions; the best is 13 (at 16 regions). The altitude hierarchy merges each nucleus with the background as soon as its weakest boundary point is reached; dim nuclei and touching pairs go early.

M2. Dynamics: 14 of 18; area: 16 of 18 (volume also 16). Their best values are 16 (dynamics, at 22 regions) and 17 (area, at 21). The altitude hierarchy loses dim nuclei (low passes to the background) and keeps fragments of large bright nuclei separate too long; area merges small fragments first, which is what an over-segmented nucleus needs.

M3. 10 of 18 at 20 regions (12 at best, at 45 regions). The nuclei have internal texture and touch each other with similar means: the mean difference between two halves of one nucleus is not smaller than that between two touching nuclei, and the background (one huge region) absorbs dim nuclei whose means are close to it. Mean difference helps against a single weak pixel, not against similar neighbours.

M4. Altitude: the dark disc stays, the bright disc has leaked into the background through its weak spot. Mean difference: the bright disc (with the ramp wedge) stays, the dark disc has merged. At 3 regions both hierarchies keep both discs (2 of 2 matched).

M5. Yes: splitting a merged pair and merging a fragmented nucleus each add a matched nucleus; a per-branch cut can beat every horizontal cut, because different nuclei need different levels.

M6. The nuclear envelope and the large dark structures of the cytoplasm are the brightest (most lasting) boundaries; small texture boundaries are dark. With SLIC leaves the UCM also contains the regular superpixel boundaries, most of which disappear early (dark), and the big boundaries are smoother.

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 graphseg/src/merge_trees/exercise.rs. The leaves (initial regions) are numbered 0 \dots n-1, and the passes between them are edges (a, b, h). A merge tree is a list of merges (a, b, level) of tree nodes: the leaves are nodes 0 \dots n-1, and merge number k in the list creates node n + k. The five basins a–e of B1 are leaves 0–4.

C1. altitude_tree(n, edges): Kruskal on the passes, recording every join as a merge:

node[r] = r for every leaf (the tree node of each component, kept at its root)
for each pass (a, b, h), lowest first:
    A = find(a),  B = find(b)
    if A ≠ B:
        add the merge (node[A], node[B], h)
        join B into A;  node[A] = the new node (n + number of merges − 1)

The check is B1(a): b|c at 4, a joins at 5, d|e at 6, all at 7.

C2. attribute_tree(n, edges, minimum, area, attribute): flood by altitude as in C1, but give each join the attribute of the smaller region, then sort:

for each pass (a, b, h), lowest first, with A = find(a) ≠ B = find(b):
    value = min(attr(A), attr(B))      # dynamics h − min, area |C|, volume |C|·(h − min)
    remember (a, b, value)
    join A and B: the new minimum is the smaller one, the new area the sum
return altitude_tree(n, the remembered edges)      # C1 sorts them by value

The checks are B1(b), (c) and (d).

C3. cut_first(n, merges, k): apply the first k merges and label the leaves by region (union–find again; each tree node needs one of its leaves). cut (apply all merges up to a level) and cut_regions (stop at a number of regions) are given and use it. The checks are B1 and B2(b, c).

C4. ucm(n, merges, edges): for every edge between two leaves, the level at which they first share a region. Replay the merges with union–find without shortcuts and remember when each link was made:

for each merge (A, B, level), in order:
    ra = root of A's leaf,  rb = root of B's leaf     # walk up, no path halving
    hang the smaller tree under the larger;  link_level[lower root] = level
u(a, b) = the largest link level on the way up from a and from b
          to their first common ancestor

(It works because the levels never decrease: the last link on the way is the one that joined the two.) The checks are B2(a) and u(a, e) = 7 in the altitude tree.

C5. rag_merge(means, areas, pairs): greedy merging by difference of means on a region adjacency graph, with a priority queue and lazy updates:

queue every adjacent pair (a, b) with |mean a − mean b|
while the queue is not empty:
    take the pair with the smallest difference
    skip it if a or b has changed since it was queued
    merge b into a: area-weighted mean, areas added, b's neighbours become a's
    queue a with each of its neighbours (new differences)

The check is B3: the differences 1, 1, 1, 1.5, 5, 12, 19.4, 26.9 and the three regions before the last two merges.

Then cargo run -p graphseg -- merge-trees and open the pictures in out/merge-trees/. The program makes leaves from a synthetic image of 18 nuclei: the 150 deepest watershed basins of its gradient. It builds all five hierarchies on them, cuts each at 20 regions (mean grey levels, yellow boundaries), counts the nuclei matched with overlap (IoU) ≥ 0.5, and searches for the best number of regions between 5 and 60. It also draws the UCM of the dynamics hierarchy.

C6. Read the counts. Which hierarchy matches most nuclei at 20 regions, and at its best cut? Does the best number of regions agree between the hierarchies?

C7. Look at ucm-dynamics.png. Which boundaries are bright, which dark? What would you see if you thresholded it at a high value?

C8. (math) The program cuts every hierarchy at a number of regions. Why can the greedy mean-difference merging not simply be cut at a level, as the altitude tree can?

The model solution is graphseg/src/merge_trees/solution.rs in the download.

C6. At 20 regions: altitude 12 of 18, dynamics 12, area 11, volume 12, mean difference 13. The best cuts: altitude 12 (at 14 regions), dynamics 13 (25), area 12 (14), volume 13 (18), mean difference 14 (37). No hierarchy and no single cut finds all 18, and the best number of regions differs from one hierarchy to the next. Some true nuclei overlap and are partly hidden by a neighbour, which no partition of these leaves can match well.

C7. The outlines of the nuclei are bright: those boundaries survive to the top of the hierarchy. The boundaries inside the nuclei (between basins of their texture) are dark: they vanish early. A high threshold keeps only the bright outlines, one closed contour around each nucleus (or pair of touching nuclei): the coarse levels of the hierarchy.

C8. After a merge the mean of the new region changes, and so do its differences to all its neighbours. A later merge can therefore have a smaller difference than an earlier one: the levels need not increase. “All merges with level \le t” would then pick merges out of order, and the result would not be a cut of the tree. The altitude tree (and the attribute trees, which are sorted) have levels that never decrease.

Where this leads

The quality of a hierarchy depends on its leaves. Superpixels are leaves designed for this: compact, about equal in size, and respecting the boundaries.