Exercise: intensity inside an object
Before this unit: Day 14: Labelling.
See also: Day 13: Morphology; Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- compute the intensity statistics of an object by hand: integrated, mean, median, quartiles, MAD, edge-band mean,
- predict which of them change under a background offset, a gain, a mask error and saturation,
- compute an intensity-weighted centroid and the mass displacement,
- assign pixels to radial rings and compute FracAtD and MeanFrac,
- normalise well values with a z-score and a robust z-score, and say when that is not enough.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Integrated intensity | the sum of the pixel values of the object: the amount of dye |
| Mean | sum / number of pixels: the concentration |
| Median, quartiles | sort the n values; the value at position h = (n-1)p (counting from 0), interpolating between neighbours; p = 0.5, 0.25, 0.75 |
| MAD | the median of the distances |I - \text{median}| |
| Edge band | object pixels with a 4-neighbour outside the object (object minus its erosion by the cross) |
| Mass displacement | distance between the geometric centroid and the intensity-weighted centroid |
| Radial position | \rho = d_c / (d_c + d_e): d_c to the centre, d_e to the edge; k rings of equal width in \rho |
| FracAtD, MeanFrac | share of the intensity in a ring; that share divided by the ring’s share of the pixels |
| Robust z | (x - \text{median of controls}) / (1.4826 \cdot \text{MAD of controls}) |
Standard deviations of pixel values divide by n; the z-score of wells uses the usual sample standard deviation (n - 1).
By hand
The object is the whole 5 \times 5 block below (on a dark background); x is the column (0–4, left to right), y the row (0–4, top to bottom).
I = \begin{bmatrix} 1 & 2 & 2 & 2 & 1 \\ 2 & 3 & 3 & 4 & 2 \\ 2 & 3 & 4 & 8 & 6 \\ 2 & 3 & 5 & 9 & 7 \\ 1 & 2 & 2 & 3 & 2 \end{bmatrix}
B1. (a) Compute the integrated intensity, the mean, the minimum and the maximum. (b) Sort the 25 values. Give the median and the lower and upper quartiles. (With n = 25 the positions h are whole numbers.) (c) Compute the MAD.
B2. The edge band is the outer ring of 16 pixels. (a) Compute the edge mean and the mean of the inner 3 \times 3 pixels. (b) Compute edge / mean. Is this cell rim-stained?
B3. The camera adds a constant background of 10 to every pixel. (a) Without recomputing everything, give the new integrated intensity, mean, median, MAD and standard deviation (the old one is 2.12). (b) Compute the new edge / mean. Why did it move towards 1? (c) Instead of the offset, the gain doubles. Which of the numbers of B1 and B2 change, and how?
B4. The segmentation is one pixel too large: the object is now the 7 \times 7 block with a border of background pixels of value 1 around I. Compute the new mean and median. Which of the two is hurt more, and why?
B5. (a) Compute the geometric centroid. (b) The column sums of I are 8, 13, 16, 26, 18 and the row sums 8, 14, 23, 26, 10. Compute the intensity-weighted centroid. (c) Compute the mass displacement. In which direction is the signal displaced?
B6. Radial rings (k = 3). (a) Three pixels have (d_c, d_e) = (1, 4), (3, 3) and (5, 1). Compute \rho for each and the ring it falls in. (b) A small object has 2 pixels of value 10 in ring 1, 3 pixels of value 6 in ring 2 and 5 pixels of value 2 in ring 3. Compute FracAtD and MeanFrac for each ring. Where is the signal enriched?
B7. A feature of five control wells: 98, 102, 100, 95, 130. Five treated wells: 101, 140, 99, 160, 120. (a) Compute the mean and sample standard deviation of the controls, and the z-score of each treated well. Which wells have |z| > 3? (b) Compute the median and MAD of the controls, and the robust z of each treated well. Which wells have |z| > 3 now? (c) Why do the two answers differ?
Math questions
B8. (math) Show that for J = gI + b (g > 0) every quantile satisfies q_p(J) = g\,q_p(I) + b, that \mathrm{MAD}(J) = g\,\mathrm{MAD}(I), and that a ratio of two means \mu_1/\mu_2 is unchanged by g but not by b.
B9. (math) Show that the intensity-weighted centroid of a uniform object coincides with its geometric centroid, and that it does too for any intensity that is symmetric under a point reflection through the geometric centroid (I(2\bar{\mathbf x} - \mathbf x) = I(\mathbf x), object symmetric as well).
B10. (math) For normal data with standard deviation \sigma, show that the MAD tends to \sigma\,\Phi^{-1}(3/4), and hence that 1.4826 \cdot \mathrm{MAD} estimates \sigma.
B11. (math) Show that \sum_j a_j\,\mathrm{MeanFrac}_j = 1 for the rings of an object (a_j = share of the pixels in ring j), and that \mathrm{MeanFrac}_j equals the ring’s mean intensity divided by the object’s mean intensity.
B1. (a) Integrated 81, mean 81/25 = 3.24, min 1, max 9. (b) Sorted: 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 4 4 5 6 7 8 9. Median: position 12 → 2. Lower quartile: position 6 → 2. Upper quartile: position 18 → 4. (c) Distances from the median 2, sorted: ten 0s, eight 1s, then 2 2 3 4 5 6 7. The median (position 12) is 1: MAD = 1.
B2. (a) Edge ring: 1+2+2+2+1 (top) + 1+2+2+3+2 (bottom) + 2+2+2 (left, rows 1–3) + 2+6+7 (right) = 39 over 16 pixels: 2.44. Inner 3 \times 3: 3+3+4+3+4+8+3+5+9 = 42, mean 4.67. (b) Edge / mean = 2.44/3.24 = 0.75. Below 1: the edge is darker than the average, so not rim-stained (a rim-stained cell has edge / mean > 1).
B3. (a) Integrated 81 + 25 \cdot 10 = 331, mean 13.24, median 12, MAD 1 (unchanged), standard deviation 2.12 (unchanged). (b) (2.44 + 10)/(3.24 + 10) = 12.44/13.24 = 0.94. The offset is added to both means; a ratio (a+b)/(c+b) tends to 1 as b grows. Subtract the background before forming ratios. (c) Integrated, mean, min, max, median, quartiles, MAD, standard deviation and the edge mean all double (162, 6.48, …, MAD 2, sd 4.24). Edge / mean stays 0.75: the gain cancels in a ratio.
B4. The 7 \times 7 block has 49 pixels and sum 81 + 24 \cdot 1 = 105: mean 105/49 = 2.14 (was 3.24, −34 %). Sorted, the 49 values start with 3 + 24 = 27 ones, so the median (position 24) is 1 (was 2). Here the median is hurt more: the added background pixels are now the majority of the dark values. In a large object the extra ring is a small fraction of the pixels; the median then hardly moves while the mean still drops (demo: uniform cell, mask 2 px too large, median unchanged at 112.1, mean 110.0 → 104.3).
B5. (a) (\bar x, \bar y) = (2, 2). (b) \bar x_I = (0\cdot8 + 1\cdot13 + 2\cdot16 + 3\cdot26 + 4\cdot18)/81 = 195/81 = 2.41; \bar y_I = (0\cdot8 + 1\cdot14 + 2\cdot23 + 3\cdot26 + 4\cdot10)/81 = 178/81 = 2.20. (c) \sqrt{0.41^2 + 0.20^2} = 0.45 px, towards the right and down: the bright pixels 8, 9, 7, 6 sit in the lower right.
B6. (a) \rho = 1/5 = 0.2 → ring 1; 3/6 = 0.5 → ring 2 ([1/3, 2/3)); 5/6 = 0.83 → ring 3. (b) Total 20 + 18 + 10 = 48. FracAtD = 20/48, 18/48, 10/48 = 0.417, 0.375, 0.208. Pixel shares 2/10, 3/10, 5/10. MeanFrac = 2.08, 1.25, 0.42: enriched in the centre, depleted at the edge (a “nuclear” pattern).
B7. (a) Mean 105, sd 14.21. z = −0.28, 2.46, −0.42, 3.87, 1.06: only the well with 160 passes |z| > 3. (b) Median 100; distances 2, 2, 0, 5, 30, median 2; scale 1.4826 \cdot 2 = 2.97. Robust z = 0.34, 13.5, −0.34, 20.2, 6.7: three wells (140, 160, 120) pass. (c) The control 130 is an outlier. It pulls the mean up and inflates the sd from about 3 to 14, so real effects look small. The median and MAD ignore it.
B8. x \mapsto gx + b is strictly increasing, so it keeps the order of the sorted values; the value at position h (and any interpolation between two neighbours, being linear) maps to g\,q_p + b. Then |J_k - q_{0.5}(J)| = g\,|I_k - q_{0.5}(I)|, and the median of these is g\,\mathrm{MAD}(I). Means: \mu_i(J) = g\mu_i + b, so \frac{g\mu_1 + b}{g\mu_2 + b} equals \mu_1/\mu_2 for b = 0 (any g) but not in general for b \ne 0.
B9. With I = c: \sum I\mathbf x / \sum I = c\sum \mathbf x / (c\,n) = \bar{\mathbf x}. With point symmetry, pair each pixel \mathbf x with \mathbf x' = 2\bar{\mathbf x} - \mathbf x (also in the object, same intensity): I(\mathbf x)\mathbf x + I(\mathbf x')\mathbf x' = 2I(\mathbf x)\bar{\mathbf x}. Summing over all pairs gives \sum I\mathbf x = \bar{\mathbf x}\sum I.
B10. For X \sim N(\mu, \sigma^2) the median is \mu, and the MAD is the median m of |X - \mu|: P(|X - \mu| \le m) = 2\Phi(m/\sigma) - 1 = 1/2, so \Phi(m/\sigma) = 3/4 and m = \sigma\,\Phi^{-1}(3/4) = 0.6745\,\sigma. The sample MAD converges to m, and 1/0.6745 = 1.4826.
B11. \sum_j a_j\,\mathrm{MeanFrac}_j = \sum_j \mathrm{FracAtD}_j = 1. And \mathrm{FracAtD}_j / a_j = \dfrac{S_j / S}{n_j / n} = \dfrac{S_j/n_j}{S/n} = \bar I_j / \bar I, with S_j, n_j the ring’s sum and pixel count.
Explore
The demo measures one synthetic cell. All six patterns have the same mask (6521 pixels) and the same total signal (mean 100). Sliders change the acquisition (background offset, gain, noise, saturation level) and the mask (grown or shrunk by up to 2 pixels), and the number of rings. The image shows the mask outline (yellow), the edge band (cyan), the rings (alternating tints), the geometric centroid (white +) and the intensity-weighted centroid (red dot), joined by the mass-displacement arrow. The histogram shows the values inside the mask with the mean (blue) and the median and quartiles (red). The table gives every feature and its change Δ against the ideal measurement of the same cell (no offset, gain 1, no noise, no saturation, exact mask). The tabs population and plate are used in E7–E9.
E1. Preset background offset (offset 40, noise σ = 4). Which features moved by about 40, which by about 40 \cdot 6521, and which hardly moved? Why did std and MAD change a little at all?
E2. Preset same integral, other pattern, then click through the six patterns. Check that the integrated intensity and the mean never change. Which single column best separates uniform from punctate? Uniform from gradient? Which pattern has the same FracAtD as the uniform cell, and which column tells them apart?
E3. Preset rim vs filled. Note edge / mean. Now shrink the mask by 1 and by 2 pixels, then grow it by 1. What happens to edge / mean, and why is it so sensitive?
E4. Preset polarised cell. Read the mass displacement. Add an offset of 50: what happens to it, and why? Then tick “rings around the intensity maximum” with 4 rings: compare MeanFrac of ring 1 with the centroid-centred rings.
E5. Preset nuclear: radial bins. Read MeanFrac for the 4 rings. Try 2 and 8 rings. Add an offset of 50; then instead set the gain to 2. Which of the two changes MeanFrac?
E6. Preset dilated mask (offset 10, mask +2 px) and then saturation. For each, which columns are wrong, by how much, and in which direction?
E7. Tab population (or preset two cell types): 10 cells of type A and 10 of type B, with different brightness. Plot mean against std. Which feature separates the types, which does not? Try area against integrated intensity.
E8. Preset plate offset: robust z. First choose normalisation none: compare the control wells of plate A and plate B. Then robust z: how many treated wells (marked *) pass |z| > 3 on each plate, and how many other wells?
E9. Preset plate gradient hides it (gradient 12 per column, robust z). How many treated wells pass |z| > 3 now, and why? Switch to row/col + robust z. Then set the gradient back to 0 and compare.
E1. Mean (100 → 140.1), median (102.1 → 141.6) and the quartiles (138.4 and 144.5) moved by about 40; the max moved more (to 156.1), because the noise adds to the extremes; the integrated intensity by about 40 \cdot 6521 = 260\,840 (652 100 → 913 293). The std moved only from 7.6 to 8.7 and the MAD from 0 to 3.1: an offset does not change the spread, but the noise (σ = 4) does add spread. Edge / mean moved from 0.66 to 0.75, towards 1.
E2. Integrated 652 100 and mean 100.0 for all six. Uniform against punctate: the std (7.6 against 82.0) or the max (102 against 559). Uniform against gradient: the std (7.6 against 41.8), the MAD (0 against 33.6), or the mass displacement (0 against 10.6 px). The gradient has the same FracAtD as the uniform cell (0.058, 0.190, 0.325, 0.426), because a linear ramp adds as much on one side of each ring as it removes on the other; RadialCV tells them apart (0.08 to 0.48 against 0).
E3. Edge / mean 2.43 with the exact mask. Shrinking by 1 px: 3.27; by 2 px: 3.58; growing by 1 px: 1.34. The rim is only a few pixels wide, and the edge band is one pixel: moving the mask by one pixel moves the band to a different place on the steep intensity profile of the rim (and the mean changes too: 100 → 94.1 → 85.1 when shrinking). Edge features need an accurate mask.
E4. Mass displacement 8.88 px. With offset 50: 5.92 px. The offset adds the same value to every pixel, which pulls the weighted centroid towards the geometric one (a uniform object has displacement 0). With rings around the intensity maximum, ring 1 now sits on the spot: MeanFrac 4.07, 1.66, 0.85, 0.68 (centroid-centred: 0.81, 1.19, 1.23, 0.78, which shows the spot only as enrichment in the middle rings).
E5. MeanFrac 3.26, 2.62, 0.45, 0.42. With 2 rings: 2.77, 0.43; with 8: 3.26, 3.26, 3.26, 2.17, 0.45, 0.44, 0.44, 0.40 (the boundary of the bright region falls inside ring 4). The offset changes MeanFrac (2.51, 2.08, 0.63, 0.61: towards 1); the gain does not (it cancels in the ratio).
E6. Dilated mask: the mean drops from 110.0 to 104.3 (and the integrated intensity rises from 717 310 to 735 400: 528 extra pixels of blurred edge and background), the std jumps from 7.6 to 21.5, edge / mean falls from 0.69 to 0.22; the median (112.1) does not move. Saturation (spot cell clipped at 300): the max is 300 instead of 488.4, the mean 96.0 instead of 100.0, the integrated intensity 4 % too low, the std 62.7 instead of 77.7, and the mass displacement 8.12 instead of 8.88 px; median and quartiles are unchanged (only the brightest pixels are clipped).
E7. The mean overlaps completely (type A 84–123, type B 81–124): brightness varies within both types. The std separates them cleanly (A: 8.9–11.5, B: 56–90): punctate staining has a wide spread of values. Area also separates them, just (A 3063–4439 px, B 2131–2995 px), and integrated intensity mixes size and brightness, so the types overlap there.
E8. Without normalisation the controls of plate A lie between 94 and 103, those of plate B between 149 and 169: the same treatment would give different numbers on the two plates. With robust z all four treated wells pass |z| > 3 on both plates (z 10.1–12.6 on A, 4.4–6.1 on B) and no other well does.
E9. None: the gradient spreads the controls (64 to 133 on plate A), so their MAD and the robust scale grow, and the treated wells reach only z = 0.4 to 1.4. Robust z corrects an offset and a gain, not a trend across the plate. With the row/column correction all four treated wells come back (z 6.8–9.8 on plate A) and no other well passes. With gradient 0 robust z alone was enough.
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/intensity_features/exercise.rs.
C1. quantile(values, p), mad(values) and robust_z(x, controls): quantiles by sorting, as in the Background table, then the MAD and the robust z-score built on them:
sort the values: v[0] ≤ v[1] ≤ … ≤ v[n−1]
h = (n − 1) · p
lo = floor(h); hi = min(lo + 1, n − 1)
quantile = v[lo] + (h − lo) · (v[hi] − v[lo])
MAD = median of |v − median(v)|
robust z = (x − median(controls)) / (1.4826 · MAD(controls))
The checks are your answers to B1(b), B1(c) and B7(b).
C2. object_stats(img, labels, label): collect the values of the pixels with that label, then the integrated intensity, mean, standard deviation (divide by n), minimum, maximum, median, quartiles and MAD. The checks use B1, B3 (offset and gain) and B4 (mask one pixel too large).
C3. edge_band(labels, label): the object pixels with a 4-neighbour outside the object:
for every pixel p with the label:
if one of its 4 neighbours is outside the image or has another label:
band(p) = true
The check is B2: the outer ring of 16 pixels, edge mean 2.44.
C4. mass_displacement(img, labels, label): both centroids from one pass over the pixels:
m00 = m10 = m01 = M00 = M10 = M01 = 0
for every pixel (x, y) of the object, with value I:
m00 += 1; m10 += x; m01 += y
M00 += I; M10 += I·x; M01 += I·y
geometric = (m10 / m00, m01 / m00); weighted = (M10 / M00, M01 / M00)
mass displacement = distance between them
The check is B5.
C5. Radial rings: ring_of(dc, de, k) gives the ring of one pixel, \min(k, \lfloor k\rho\rfloor + 1) with \rho = d_c/(d_c + d_e); radial_distribution(values, rings, k) adds up the intensity and the pixel count of every ring and returns FracAtD and MeanFrac. The checks are B6(a) and B6(b).
Then cargo run -p measurements -- intensity-features and open the pictures in out/intensity-features/. The program builds six cells like those of the lecture: one elliptic mask of 6521 pixels, values blurred a little and scaled so that every cell has mean 0.4. It prints one row of features per cell. cells.png shows the cells with the edge band (cyan), the geometric centroid (white +) and the intensity-weighted centroid (red +); rings.png shows the 4 rings in four tints.
C6. Which columns of the table separate the uniform cell from the gradient? Which separate the bright rim from the nuclear cell? Is there a pair of cells that MeanFrac cannot tell apart?
C7. The last three lines measure the uniform cell again: with an offset of 0.1 added to the whole image, and then with the mask also grown by 2 pixels. Which numbers move, and by how much? Compare with your answers to B3 and B4.
The model solution is measurements/src/intensity_features/solution.rs in the download.
C6. All six cells have the same integrated intensity (2608) and mean (0.400). Uniform and gradient have the same MeanFrac in every ring (1.02, 1.02, 1.02, 0.98) and the same edge / mean (0.68): a linear ramp adds as much on one side of each ring as it takes away on the other. They differ in the standard deviation (0.028 against 0.180), the maximum (0.41 against 0.75) and the mass displacement (0 against 11.5 pixels; in cells.png the red cross has moved towards the bright side). Bright rim and nuclear are told apart by edge / mean (2.08 against 0.21) and by MeanFrac (0.26 in the inner rings and 1.94 at the edge, against 3.11, 3.02, 0.43, 0.30); their mass displacement is 0 for both. The off-centre spot has the largest displacement, 12.8 pixels.
C7. With the offset, the integrated intensity grows by 0.1 \cdot 6521 = 652 (2608.5 → 3260.4), the mean and the median by 0.1 (0.400 → 0.500, 0.407 → 0.507), and the standard deviation stays 0.028, as in B3. Growing the mask by 2 pixels adds blurred edge and background pixels: the mean drops to 0.476 and the standard deviation triples to 0.089, while the median stays 0.507. In this large object the median is the robust one; in the small block of B4 the extra pixels were the majority and the median dropped too. (The MAD is 0 here: more than half of the pixels of the uniform cell have exactly the same value.)
Where this leads
The intensity statistics treat the pixels of an object as a bag of values: they ignore how the values are arranged. The next unit, texture, looks at pairs of pixels: the co-occurrence matrix and the features computed from it. Later on, the feature table is the input of the random forests day (classifying objects) and of the evaluation day (turning measurements into claims). Per-object intensities in several channels are what the unmixing, colocalisation and FRET days correct and compare.