Exercise: neighbours, parents and children

Goal

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

  • list the neighbours of each object of a small label image for 4- and 8-connectivity, and compute percent touching,
  • apply the within distance r and expand until adjacent definitions, and explain why the counts differ,
  • compute nearest-neighbour distances, the angle between the two closest neighbours, and the Clark–Evans ratio,
  • assign children to parents by overlap and by centroid, and recognise the ambiguous cases.

Do the pen-and-paper part first, then the demo.

Background

Idea What it means
Adjacent two labels have a pair of 4-neighbouring (or 8-neighbouring) pixels
Boundary pixel an object pixel with a neighbour (same connectivity) of another value; outside the image counts as background
Percent touching 100 × (boundary pixels with a neighbour of another object) / (boundary pixels)
Within distance r the closest pixel centres of the two objects are at most r apart (r = 1: 4-adjacent, r = \sqrt2: 8-adjacent)
Expand until adjacent every background pixel goes to the nearest object; then adjacency
Clark–Evans R = \bar d / (0.5/\sqrt\rho) with \bar d the mean nearest-neighbour distance, \rho = objects per unit area
Parent by overlap the parent label covering most of the child’s pixels
Parent by centroid the parent label at the child’s centroid (rounded to a pixel)

Coordinates: x = column, y = row, both from 0.

By hand

B1. The label image (0 = background):

\begin{bmatrix} 1 & 1 & 0 & 2 & 2 & 2 \\ 1 & 1 & 0 & 2 & 2 & 2 \\ 1 & 1 & 3 & 0 & 0 & 0 \\ 0 & 3 & 3 & 3 & 0 & 4 \\ 0 & 3 & 3 & 0 & 4 & 4 \\ 5 & 0 & 0 & 0 & 4 & 4 \end{bmatrix}

  1. List the neighbours of each object with 4-connectivity, and with 8-connectivity. Which object gains most?
  2. With 8-connectivity, every pixel of object 1 is a boundary pixel. How many of its 6 pixels touch another object? Give its percent touching. Do the same for object 5.
  3. Who is a neighbour of whom “within r = 2” (closest pixel centres at most 2 apart)? Which new pairs appear compared with 8-adjacency?

B2. Five object centroids in a 10 \times 10 field: 1 = (2, 2), 2 = (5, 2), 3 = (2, 6), 4 = (8, 8), 5 = (9, 3). (a) For each object give the closest and the second closest object and their distances. (b) Compute the angle at object 1 between the directions to its two closest neighbours. (c) Compute the mean nearest-neighbour distance, the expected value for a random pattern of the same density, and the Clark–Evans ratio. Clustered, random or regular?

B3. Two cells, far apart and alone in a large image. How many neighbours does each have with the adjacent, the within-10-px and the expand-until-adjacent definitions? What does that say about the third definition?

B4. Nuclei N and spots S on the same 6 \times 7 grid:

N = \begin{bmatrix} 1&1&1&0&2&2&2 \\ 1&1&1&0&2&2&2 \\ 1&1&1&0&2&2&2 \\ 0&0&0&0&0&0&0 \\ 0&3&3&3&0&0&0 \\ 0&3&3&3&0&0&0 \end{bmatrix}, \qquad S = \begin{bmatrix} 0&0&0&0&0&0&0 \\ 0&1&2&2&2&0&0 \\ 0&0&2&0&0&0&0 \\ 0&3&3&0&0&0&0 \\ 0&0&3&0&0&0&0 \\ 0&0&0&0&0&0&4 \end{bmatrix}

  1. For each spot, count its pixels in each nucleus and give its parent by largest overlap, with the overlap fraction. Which spot touches two nuclei?
  2. Compute each spot’s centroid, round it to a pixel, and give the parent by centroid.
  3. Give the number of spots per nucleus for both rules.
  4. Which rule would you use for small round spots, and which for spots that can be crescent-shaped? Why?

Math questions

B5. (math) For points scattered at random with density \rho, show that P(D > r) = e^{-\rho\pi r^2} for the distance D to the nearest other point, and that E[D] = 1/(2\sqrt\rho).

B6. (math) Show that for two point objects the boundary between their regions in “expand until adjacent” is the perpendicular bisector of the segment between them. What is it for two discs of different radii?

B7. (math) Using Euler’s formula for planar graphs, show that the mean number of neighbours in the Delaunay triangulation of n \ge 3 points is less than 6.

B1. (a) 4-connectivity: 1–3 only (pixel (1, 2) of object 1 next to (2, 2) of object 3). Objects 2, 4 and 5 have no neighbour. 8-connectivity: 1: {3}; 2: {3} (diagonal (3, 1)–(2, 2)); 3: {1, 2, 4, 5}; 4: {3} (diagonal (3, 3)–(4, 4)); 5: {3} (diagonal (0, 5)–(1, 4)). Object 3 gains most: from 1 to 4 neighbours. (b) Object 1: (1, 2) touches 3 directly; (0, 2) and (1, 1) touch 3 diagonally: 3 of 6, 50 %. Object 5: its single pixel touches 3 diagonally: 100 %. (c) Closest pixel distances: 1–2: 2, 1–3: 1, 2–3: \sqrt2, 2–4: 2, 3–4: \sqrt2, 3–5: \sqrt2, 1–4: 3.6, 1–5: 3, 2–5: 5, 4–5: 4. Within 2: 1: {2, 3}; 2: {1, 3, 4}; 3: {1, 2, 4, 5}; 4: {2, 3}; 5: {3}. New pairs: 1–2 and 2–4, two pixels apart across one background column or row.

B2. (a) 1: closest 2 at 3.00, second 3 at 4.00. 2: 1 at 3.00, 5 at 4.12. 3: 1 at 4.00, 2 at 5.00. 4: 5 at 5.10, 3 at 6.32. 5: 2 at 4.12, 4 at 5.10. (b) From 1 the directions are (3, 0) (to 2) and (0, 4) (to 3): 90°. (c) \bar d = (3 + 3 + 4 + 5.10 + 4.12)/5 = 3.84. Density \rho = 5/100 = 0.05, random expectation 0.5/\sqrt{0.05} = 2.24. R = 3.84/2.24 = 1.72: regular (the points avoid each other). With only 5 points and a field this small, the border effect (B5 note) inflates R too.

B3. Adjacent: 0. Within 10 px: 0. Expand until adjacent: each gets 1, the other one: the expansion fills the whole image, so the two regions meet at the midline however far apart the cells are. This definition measures “who is next to whom” in a tessellation, not physical contact: always say which definition was used.

B4. (a) Spot 1 (1 pixel): in nucleus 1 → parent 1, fraction 1. Spot 2 (4 pixels: (2, 1), (3, 1), (4, 1), (2, 2)): 2 in nucleus 1, 1 in background, 1 in nucleus 2 → parent 1, fraction 0.5, and it touches two nuclei. Spot 3 (3 pixels: (1, 3), (2, 3), (2, 4)): 1 in nucleus 3 → parent 3, fraction 0.33. Spot 4: background only → no parent. (b) Spot 1: (1, 1) → 1. Spot 2: centroid (2.75, 1.25) → pixel (3, 1), background → none. Spot 3: (1.67, 3.33) → (2, 3), background → none. Spot 4: (6, 5) → none. (c) Overlap: nucleus 1: 2 spots, nucleus 2: 0, nucleus 3: 1. Centroid: nucleus 1: 1, the others 0. (d) For small round spots both rules agree, and the centroid rule is cheaper. For crescents or spots straddling a boundary the centroid may fall outside every parent (spots 2 and 3), so the overlap rule with a recorded overlap fraction (and a flag for children touching two parents) is safer.

B5. The number of other points in a disc of radius r is Poisson with mean \rho\pi r^2, so P(D > r) = P(0 \text{ points}) = e^{-\rho\pi r^2}. For a non-negative variable, E[D] = \int_0^\infty P(D > r)\,dr = \int_0^\infty e^{-\rho\pi r^2}\,dr = \tfrac12\sqrt{\pi/(\rho\pi)} = \dfrac{1}{2\sqrt\rho}, using \int_0^\infty e^{-a r^2} dr = \tfrac12\sqrt{\pi/a}.

B6. The boundary is the set of \mathbf x with \lVert \mathbf x - \mathbf p\rVert = \lVert \mathbf x - \mathbf q\rVert. Squaring and cancelling \lVert\mathbf x\rVert^2: 2\mathbf x \cdot (\mathbf q - \mathbf p) = \lVert \mathbf q\rVert^2 - \lVert \mathbf p\rVert^2, a line perpendicular to \mathbf q - \mathbf p through the midpoint. For discs with radii r_1, r_2 the distance to a disc is \lVert \mathbf x - \mathbf c_i\rVert - r_i, so the boundary is \lVert \mathbf x - \mathbf c_1\rVert - \lVert \mathbf x - \mathbf c_2\rVert = r_1 - r_2: a branch of a hyperbola, bending around the smaller disc (it is a straight line only for equal radii).

B7. The Delaunay triangulation is a planar graph with V = n vertices. Euler: V - E + F = 2. Every face (including the outer one) has at least 3 edges and every edge borders 2 faces, so 3F \le 2E. Then 2 = V - E + F \le V - E + \tfrac23 E, so E \le 3V - 6, and the mean degree 2E/V \le 6 - 12/V < 6.

Explore

The demo shows a seeded population of nuclei (dark), their cell bodies (each nucleus grown by 9 px, in colour) and small spots. In neighbours mode the lines are the neighbour graph for the chosen definition: adjacent cells (the grown cell bodies touch), within r (the closest nucleus pixels are at most r apart), or expand until adjacent. The histograms show the number of neighbours and the nearest-neighbour distance between nucleus centroids (dashed: a random pattern of the same density); the readout gives the mean number of neighbours for all three definitions, for interior and border objects, and the Clark–Evans ratio. The border rule keeps all objects, removes those whose cell body touches the image border, or measures only objects inside a guard zone of 24 px while all objects still count as neighbours. Click an object to see its row. In parents and children mode the spots are assigned to nuclei or cells: green = has a parent, orange = no parent, magenta = touches two parents.

E1. Preset adjacent vs expanded (40 nuclei, random). Read the mean number of neighbours for the three definitions. Switch between them and describe how the graph changes. Why is “expand until adjacent” so much larger?

E2. Preset within r. Move r from 2 to 30 px and note the mean number of neighbours at r = 2, 5, 10, 20 and 30. Is there a natural choice of r?

E3. Presets clustered and regular. Read the Clark–Evans ratio and compare the nearest-neighbour histogram with the dashed curve. Then choose random (Poisson): why is R a bit above 1? In the regular pattern, switch to adjacent cells: what happens, and why?

E4. Preset border objects have fewer neighbours (60 nuclei, expand until adjacent). Compare interior and border objects. Then choose remove them and guard zone. How many objects are measured, and what is the mean number of neighbours in each case? Which rule would you use?

E5. Preset spots per nucleus. How many spots have no parent, and where are they? Read the histogram of spots per nucleus. Switch between largest overlap and label at centroid: does anything change here? Why might it change for other spots?

E6. Preset a spot between two cells (parents: cells). The selected spot is circled. What does its row say? How many spots have no parent now, and why fewer than with nuclei as parents?

E7. Neighbours mode, expand until adjacent: click a few cells in a crowded region and at the edge of the image. Compare their number of neighbours, percent touching and the angle between the two closest neighbours. Which values do you expect for a cell in the middle of a regular hexagonal sheet?

E1. Adjacent cells 1.15, within r = 10 px 0.45, expand until adjacent 4.90. Adjacent only connects cells whose 9-px-grown bodies touch; within r only nuclei closer than 10 px; expansion connects every object to all objects around it, because the expanded regions fill the image. It counts “who is next to whom”, not contact.

E2. 0 (r = 2), 0.15 (5), 0.45 (10), 1.45 (20), 2.45 (30): the count grows steadily with r. There is no natural r from the data: choose it from the biology (for example the distance over which cells can signal) and report it.

E3. Clustered: R = 0.73, the histogram lies left of the curve (many short distances). Regular: R = 1.86, the histogram is shifted to the right and narrow (mean 37.6 px against 20.2 px for a random pattern). Random: R = 1.11, because nuclei cannot overlap or touch, so very short distances are impossible; real objects are never points. In the regular pattern the grown cell bodies never touch: 0 adjacent neighbours, against 4.80 by expansion.

E4. All objects: interior 5.69, border 4.29 (mean 5.13 for 60). Remove: 51 objects measured, mean 5.10; the objects that were next to the removed ones now have fewer neighbours, so the bias moves inwards. Guard zone: 43 objects measured, mean 5.63, close to the interior value. The guard zone is the unbiased rule; it costs some measured objects.

E5. 5 of 50 spots have no parent: 4 lie in the cytoplasm (inside a cell, outside every nucleus) and 1 between two cells. Spots per nucleus: 12 nuclei with 0, 15 with 1, 9 with 2, 4 with 3 (mean 1.13). The two rules agree here, because the spots are small and round, so the centroid lies in the nucleus whenever most of the spot does. A spot that straddles a nuclear boundary or curves around it could get different answers.

E6. The spot touches two cells: its parent is the cell with the larger overlap (63 % of its pixels), and it is flagged as overlapping 2 parents. With cells as parents, no spot is without a parent (the cells cover the cytoplasm spots too), but one is ambiguous. A “spots per cell” column silently counts that spot in one cell only.

E7. Interior cells in a crowded region have 5–7 neighbours and percent touching close to 100 %; cells at the image edge have fewer (their region is cut by the border) and a lower percent touching, since part of their boundary is the image edge. In a regular hexagonal sheet every interior cell has 6 neighbours, touches on 100 % of its boundary, and its two closest neighbours are 60° apart.

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 measurements/src/relations/exercise.rs.

C1. neighbours(labels, eight): the set of neighbouring labels of every object:

for every pixel p with label a > 0:
    for every neighbour q of p (4 or 8 of them):
        b = label of q (0 outside the image)
        if b > 0 and b ≠ a:  add b to the set of a

The checks are your lists of B1(a), for 4- and 8-connectivity.

C2. percent_touching(labels, label): among the object’s boundary pixels (8-neighbours, outside the image counts as background), the percentage with a neighbour in another object. The check is B1(b): 50 % for object 1, 100 % for object 5.

C3. Distances between centroids: closest_two(points) (the closest and second closest other point of each point), angle_at(p, a, b) (from the dot product of the two directions) and clark_evans(points, area):

d̄ = mean over all points of the distance to the closest other point
ρ = number of points / area
R = d̄ / (0.5 / sqrt(ρ))

The checks are B2(a)–(c).

C4. expand_labels(labels): “expand until adjacent”, giving every background pixel the label of the nearest object pixel:

list all object pixels with their labels
for every background pixel p:
    out(p) = label of the listed pixel closest to p

The checks: B3 (two far cells become neighbours) and object pixels keeping their labels.

C5. Parents of children: parent_by_overlap(children, parents) fills a table of shared pixel counts in one pass, then takes the parent with the most pixels in each row; parent_by_centroid(children, parents) reads the parent label at each child’s rounded centroid. The checks are B4(a) and B4(b).

Then cargo run -p measurements -- relations and open the pictures in out/relations/. The program places 40 nuclei three times (clustered, random, regular), grows cell bodies 9 pixels around them, and prints the mean number of neighbours with the adjacent cells and expand until adjacent definitions, and the Clark–Evans ratio. The pictures show the regions in dim colours, the nuclei bright, and the neighbour graph in white (*-adjacent.png, *-expanded.png). Last, it assigns 50 small spots to the nuclei and to the cells (spots.png).

C6. Read the table. Which definition depends most on the pattern, and why does “expanded” give nearly the same number for random and regular nuclei? Do the three values of R fall where you expect?

C7. expand_labels compares every background pixel with every object pixel. For this image, roughly how many distance computations is that? Why does that not matter for the 5 × 5 images of the pen-and-paper exercises, and what would you use instead for a large image?

C8. Read the last two lines (spots with nuclei or cells as parents). Explain the differences between the two lines.

The model solution is measurements/src/relations/solution.rs in the download.

C6. Adjacent cells: 4.35 (clustered), 1.80 (random), 0.55 (regular); expanded: 5.10, 4.80, 4.80. The adjacent count measures contact, which depends entirely on how close the nuclei are: in regular-adjacent.png most cells stand alone. Expansion fills the whole image, so every nucleus gets the nuclei around it as neighbours however far away they are; it measures the arrangement, and for random and regular nuclei alike it tiles the image with one polygon per nucleus, each touching about five others. R = 0.81 (clustered), 1.21 (random) and 1.60 (regular). The random pattern is above 1 because the nuclei may not overlap, as in the lecture’s demo (1.11).

C7. The image has 65 536 pixels and the 40 nuclei cover a few thousand of them (radius 5 to 8: about 40 · 130 ≈ 5000). So about 6 \cdot 10^4 \cdot 5 \cdot 10^3 = 3 \cdot 10^8 distances per call, a large part of the program’s run time of a few seconds. For a 5 × 5 image it is a few hundred. A large image calls for a distance transform that also records the nearest object pixel (the feature transform of the morphology day): a few passes over the image, independent of the number of objects.

C8. With nuclei as parents, 6 of the 49 spots (one of the 50 was drawn over by another) have no parent: they lie in the cytoplasm, outside every nucleus. 14 are only partly inside their nucleus, and for 2 the overlap and centroid rules disagree: spots on the edge of a nucleus (46 % and 33 % of their pixels inside), whose centroid falls just outside it. With cells as parents every spot has one, only 1 sticks out of its cell, and the two rules agree everywhere: the cells are large compared with the spots.

Where this leads

Relations between objects pair whole objects; the colocalisation day pairs pixels of two channels. Object-based colocalisation (“the share of spots of A inside objects of B”) is the parent–child relation of this unit. The neighbour graph comes back as a graph of pixels on the graph cuts day, and tracking objects over time (the tracking day) links objects between frames instead of within one. The nearest-neighbour distance and Clark–Evans are the start of the spatial statistics day (day 19): whole distance distributions (G, F, J), Ripley’s K, edge corrections, simulation envelopes and two cell types.