Exercise: sub-pixel localisation
Before this unit: How many false spots? Thresholds from noise (previous unit); Day 2: Edges, blobs and ridges.
See also: Day 3: Bit depth and noise; Day 4: Image formation; Day 13: Morphology; Day 38: Evaluation and statistics.
Goal
By the end of this exercise you should be able to:
- compute the centroid, the three-point parabola and the three-point log-parabola of a spot, and predict their bias,
- explain what a background does to each of them,
- explain how a Gaussian fit to all pixels works, and what its residual tells you,
- predict the localisation precision from the number of photons, the pixel size and the background,
- (math) derive the log-parabola formula, the linear and Gauss–Newton steps of a Gaussian fit, the Cramér–Rao bound and the centroid bias.
Background
| Idea | What it means |
|---|---|
| Centroid | \hat x = \sum x_k I_k/\sum I_k in a window around the brightest pixel; pulled to the window centre by background |
| Parabola (three points) | vertex of the parabola through c_{-1}, c_0, c_{+1}: \delta = \frac{c_{-1} - c_{+1}}{2(c_{-1} - 2c_0 + c_{+1})}; pixel locking |
| Log-parabola | the same on \ln c_i; exact for a (point-sampled) Gaussian without background |
| Gaussian fit | minimise \sum_k (I_k - B - A\,g_k(x_0, y_0))^2 (least squares) or \sum_k(\mu_k - I_k\ln\mu_k) (Poisson maximum likelihood) |
| Precision | ideal: s/\sqrt N (Cramér–Rao); with pixels and background (Thompson): \frac{s^2 + a^2/12}{N} + \frac{8\pi s^4 b}{a^2N^2} (variance) |
| Least squares vs maximum likelihood | least squares ≈ 16/9 of the bound’s variance (sd ×1.33); maximum likelihood ≈ the bound |
By hand
B1. Three estimators. A Gaussian spot of width 1 px is sampled at -1, 0, 1: c = (0.430, 0.956, 0.783). (a) Compute the centroid offset (c_{+1} - c_{-1})/(c_{-1} + c_0 + c_{+1}). (b) Compute the parabola vertex. (c) Compute the log-parabola vertex. Which is right? (d) Add a background of 0.5 to each sample and repeat (a)–(c). Which estimators change? What should you do before using the log-parabola?
B2. Photons. A PSF of width s = 100 nm, an ideal camera without background. What is the best possible precision with N = 100, 400 and 2 500 photons? How many photons are needed for 1 nm?
B3. Pixels. The pixel size a adds a^2/12 to s^2. (a) With s = 100 nm and 100 nm pixels, how much worse is the precision than with an ideal camera? (b) And with 400 nm pixels? (c) Why are very small pixels not ideal either, when there is background or read noise? (Look at the second term of Thompson’s formula.)
B4. Predict. A dim spot: N = 200 photons, b = 10 background photons per pixel, s = a = 100 nm. (a) Compute the precision with Thompson’s formula. Which of the two terms is larger? (b) Option 1: double the exposure (spot and background photons both double). Option 2: halve the background (e.g. a better filter). Compute both. Which helps more, and why?
B5. Window size. A centroid in a 3 × 3 window or in a 9 × 9 window, on a noisy spot of σ = 1 px with the background subtracted. Which is noisier, and why? Which is more biased if the background is not subtracted?
B1. (a) (0.783 - 0.430)/2.169 = 0.16. (b) (0.430 - 0.783)/(2(0.430 - 1.912 + 0.783)) = -0.353/(-1.398) = 0.25. (c) \ln c = (-0.844, -0.045, -0.245): \delta = (-0.844 + 0.245)/(2(-0.844 + 0.090 - 0.245)) = -0.599/(-1.998) = 0.30. The log-parabola is right: the samples come from a Gaussian centred at 0.30. (d) With 0.5 added: centroid (1.283 - 0.930)/3.669 = 0.10 (pulled further to the centre); the parabola is unchanged, 0.25 (adding a constant does not move a parabola’s vertex); the log-parabola gives 0.28: no longer exact, because \ln(c + b) is not a parabola. Subtract the background first.
B2. 100/\sqrt{100} = 10 nm, 100/\sqrt{400} = 5 nm, 100/\sqrt{2500} = 2 nm. For 1 nm: N = (100/1)^2 = 10\,000 photons.
B3. (a) s_a = \sqrt{100^2 + 100^2/12} = 104.1 nm: about 4 % worse. (b) \sqrt{100^2 + 400^2/12} = 152.8 nm: 53 % worse; the spot falls almost into one pixel and its position within it is lost. (c) The background term grows as 1/a^2: with smaller pixels the spot’s photons are spread over more pixels, each with its own background (and read) noise. The usual compromise is a pixel of about the PSF width (a \approx s).
B4. (a) First term (100^2 + 100^2/12)/200 = 54.2 nm², second 8\pi \cdot 100^4 \cdot 10/(100^2 \cdot 200^2) = 62.8 nm²: the background term is the larger one. Precision \sqrt{117} = 10.8 nm. (b) Option 1 (N = 400, b = 20): 27.1 + 31.4 = 58.5 nm², 7.6 nm. Option 2 (N = 200, b = 5): 54.2 + 31.4 = 85.6 nm², 9.3 nm. Doubling the exposure helps more: it halves both terms, while halving the background halves only the second.
B5. The 9 × 9 window is noisier: the noisy pixels far from the centre (pure background noise after subtraction) enter with large lever arms x_k, and their variance adds up as \sum_k x_k^2\,\mathrm{Var}(I_k). The 3 × 3 window has few such pixels, but truncates the spot (σ = 1 px: about a quarter of the light falls outside the 3 × 3 window), which biases it towards the pixel centre. Without background subtraction the 9 × 9 window is far more biased: it contains 81 background pixels pulling to the window centre, against 9.
Math questions
B6. (math) Derive the vertex of the parabola through (-1, \ell_{-1}), (0, \ell_0), (1, \ell_{+1}). Show that applied to \ell_i = \ln c_i of samples of A e^{-(x - \delta)^2/2s^2} it returns exactly \delta.
B7. (math) Fitting by hand. A 1D spot with known width s = 1 is sampled at x = -2, \dots, 2: I = (7.13, 17.89, 33.68, 28.48, 12.07). (a) With the position known, x_0 = 0.3, the shape values are g = (0.0710, 0.4296, 0.9560, 0.7827, 0.2357) (with \sum g = 2.4750, \sum g^2 = 1.7717, \sum gI = 65.526, \sum I = 99.250). Solve the normal equations for A and B. (b) Now take A = 30, B = 5 as known and start at x_0 = 0. The Jacobian J_k = \partial\mu_k/\partial x_0 = A\,g_k(x_0)(x_k - x_0)/s^2 is (-8.120, -18.196, 0, 18.196, 8.120) and the residuals r_k = I_k - \mu_k are (-1.930, -5.309, -1.320, 5.285, 3.012). Compute one Gauss–Newton step \Delta x_0 = \sum J_k r_k/\sum J_k^2.
B8. (math) Cramér–Rao. For N independent positions from \mathcal N(x_0, s^2), compute the Fisher information and the bound. Show that the mean of the positions reaches it.
B9. (math) Centroid bias. In 1D, a window k = -m, \dots, m holds a spot of total intensity S centred at \delta (entirely inside the window) and a background b per pixel. Show that \hat x = S\delta/(S + (2m + 1)b), and evaluate it for S = 100, \delta = 0.3, b = 5, m = 3.
B6. Write \ell(x) = \alpha x^2 + \beta x + \ell_0. From the two outer points: \alpha = \tfrac12(\ell_{-1} + \ell_{+1}) - \ell_0, \beta = \tfrac12(\ell_{+1} - \ell_{-1}). The vertex is at -\beta/(2\alpha) = (\ell_{-1} - \ell_{+1})/(2(\ell_{-1} - 2\ell_0 + \ell_{+1})). For \ell(x) = \ln A - (x - \delta)^2/2s^2, a parabola itself, the parabola through three of its points is the function itself, with vertex \delta: exact for every A, s, \delta.
B7. (a) \begin{bmatrix}1.7717 & 2.4750\\ 2.4750 & 5\end{bmatrix}\begin{bmatrix}A\\B\end{bmatrix} = \begin{bmatrix}65.526\\ 99.250\end{bmatrix}: A = (5 \cdot 65.526 - 2.4750 \cdot 99.250)/(5 \cdot 1.7717 - 2.4750^2) = 30.0, B = (99.250 - 30 \cdot 2.4750)/5 = 5.0 (the data are noise-free). (b) \sum J_k r_k = 232.9, \sum J_k^2 = 794.1, \Delta x_0 = 0.293: one step from 0 lands at 0.293, close to the true 0.3; a second step gives 0.3000. Gauss–Newton converges fast near the answer.
B8. \ell(x_0) = -\sum_i (x_i - x_0)^2/2s^2, \ell'' = -N/s^2, so \mathcal I = N/s^2 and \mathrm{Var}(\hat x_0) \ge s^2/N. The mean \bar x is unbiased with \mathrm{Var}(\bar x) = s^2/N: it reaches the bound (it is the maximum-likelihood estimate here).
B9. Numerator: \sum_k k I_k = S\delta + b\sum_{k=-m}^m k = S\delta (the background terms cancel in pairs); denominator: \sum_k I_k = S + (2m + 1)b. So \hat x = S\delta/(S + (2m+1)b) = 30/135 = 0.22 instead of 0.30.
Explore: localising one spot
A 13 × 13 pixel patch with one simulated spot (pixel-integrated Gaussian of width s = 100 nm, or an Airy spot with the same width), N photons, b background photons per pixel, pixel size a, seeded Poisson noise. Tab one spot: drag on the image to move the true centre (white cross); markers show each estimator; the residual image is data − least-squares model; step walks through the fit’s iterations. Tab bias: estimate − truth against the true offset (noise-free). Tab precision: 500 seeded repeats (scatter, nm) and the RMS error against N with the theoretical curves.
E1. Preset pixel peak: ±0.5 px. Read the errors of all six estimators. Drag the true centre to a pixel corner: what happens to the brightest-pixel error?
E2. Preset fit all pixels. Press step repeatedly. How many steps does the fit take, and how does the RSS change? What does the residual image look like at the end?
E3. Presets centroid with background, parabola: pixel locking and log-parabola is exact (bias tab). Read the bias at an offset of 0.3 px for each estimator. Change the centroid window m in the first preset: does the bias grow or shrink with m?
E4. Preset Airy spot, Gaussian model. Is the least-squares Gaussian fit biased on an Airy spot? What does the wrong model cost instead? (Compare the precision tab with and without the Airy option.)
E5. Preset 1 000 photons: a few nm. Compare the three estimators with the bound and with Thompson’s formula. Which one reaches the bound?
E6. Preset background dominates dim spots. Read the RMS errors and the bound without background. Why is the centroid scatter split into clusters?
E7. Preset pixel size: too big and too small. Move the pixel size from 50 to 400 nm and watch the curves move. Where is the best pixel size for this background?
E8. Preset maximum likelihood vs least squares, 100 photons. Compare the two. Is the ratio close to the 4/3 of the formulas?
E1. For this noise realisation (1 000 photons, no background, true centre (6.3, 5.8)): brightest pixel 36.1 nm, centroid 4.7, parabola 9.9, log-parabola 3.6, least squares 3.2, maximum likelihood 5.7 nm. One realisation is noisy: the precision tab averages. At a pixel corner the brightest pixel is off by about \sqrt{0.5^2 + 0.5^2} = 0.71 px = 71 nm.
E2. 5 accepted steps; the RSS drops from 6 804 at the start (circle at the brightest pixel) to the noise level (the model then explains everything except the Poisson noise). The final residual looks like noise without structure; a ring or a bump would mean a wrong width or a second spot.
E3. Centroid (b = 20, 7 × 7 window): −0.150 px; parabola: −0.044 px; log-parabola (background subtracted): 0.000; least squares: 0.000. A larger window holds more background pixels, so the centroid bias grows with m (and shrinks with m = 1, at the cost of truncating the spot).
E4. No noticeable bias at 0.3 px (0.000 px; the Airy pattern is symmetric, and so is the error of the Gaussian model). The cost is a little precision: the fitted Gaussian does not match the Airy rings. With 1 000 photons and no background the least-squares error (500 repeats, same settings, computed with the course library) is 4.6 nm on the Airy spot against 4.3 nm on a Gaussian spot.
E5. Centroid 4.9 nm, least squares 4.3, maximum likelihood 3.3; bound s/\sqrt N = 3.2 nm, Thompson 3.3 nm. Maximum likelihood reaches the bound; least squares is about 1.33 times worse, as Mortensen’s 16/9 predicts.
E6. 300 photons on b = 20: centroid 30.0 nm, least squares 11.3, maximum likelihood 11.0 nm; the bound without background is 5.8 nm and Thompson with background 9.6 nm: the background roughly doubles the error. The centroid uses a window centred on the brightest pixel, which jumps between neighbouring pixels from repeat to repeat; with the background not subtracted, each window pulls the estimate towards its own centre: one cluster per brightest pixel.
E7. At 250 nm pixels (b = 5, N = 1 000): centroid 11.9, least squares 5.0, maximum likelihood 4.5 nm against a bound of 3.2 nm (Thompson 3.9). With large pixels the a^2/12 term grows; with very small pixels the background term \propto 1/a^2 grows. The curves are lowest around a \approx 100–150 nm, i.e. about the PSF width.
E8. Least squares 16.9 nm, maximum likelihood 15.2 nm (bound 10.0, Thompson 12.6 nm). The ratio 1.11 is below 4/3 here because with background the two formulas get closer (Mortensen’s background term is the same for both); at low background and high N the ratio approaches 4/3 (E5: 4.3/3.3 = 1.30).
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 pointdetection/src/spot_fit/exercise.rs. A 1D example of the by-hand part is a patch one pixel high.
C1. centroid(patch, background): \hat x = \sum_k x_k (I_k - b)/\sum_k (I_k - b), and the same for y. The checks are B1 (a), (d) and B9.
C2. The three-point rules: parabola_vertex(cm, c0, cp), log_parabola_vertex (the same on the logarithms) and three_point(patch, background, log), which applies them in 2D:
(bx, by) = the brightest pixel
subtract the background from it and from its four neighbours
x̂ = bx + vertex(I(bx−1, by), I(bx, by), I(bx+1, by))
ŷ = by + vertex(I(bx, by−1), I(bx, by), I(bx, by+1))
with vertex(a, b, c) = (a − c) / (2 (a − 2b + c)) # on ln a, ln b, ln c for the log-parabola
The checks are B1 (b)–(d), and a noise-free 2D spot at (6.3, 5.8) that the log-parabola finds exactly.
C3. fit_amplitude_background(patch, x0, y0, s): with the position known, the best A and B of the model B + A\,g from the 2 \times 2 normal equations of the math slide. The check is B7 (a).
C4. position_step(patch, x0, y0, s, a, b): one Gauss–Newton step for the position, per axis:
for every pixel p:
g = exp(−((p_x − x0)² + (p_y − y0)²) / 2s²)
r = I(p) − (b + a·g)
Jx = a·g·(p_x − x0) / s² Jy = a·g·(p_y − y0) / s²
Δx0 = Σ Jx·r / Σ Jx² Δy0 = Σ Jy·r / Σ Jy²
The check is B7 (b).
C5. fit_spot(patch, start, s, iterations): alternate C3 and C4. The checks: the B7 data end at 0.300, and a noise-free 2D spot is fitted exactly.
Then cargo run -p pointdetection -- spot-fit. It prints the bias of each method on a noise-free spot (width 1 px, 1 000 photons, background 20 per pixel) as the true centre moves across a pixel, and the RMS error over 500 seeded Poisson repeats for several photon counts N and backgrounds b (pixel and s both 100 nm). out/spot-fit/ has a one-spot patch and a two-spot patch (3 px apart), each fitted with one spot, and their residuals (data − model, 0 = grey).
C6. Read the bias table. Which methods are biased, in which direction, and how does the bias change with the offset δ? Compare with the bias tab of the demo.
C7. Read the precision table. Which method comes closest to the bound s/\sqrt N? By what factor is the fit above it at N = 100 and 1 000, and how does that compare with the lecture’s 16/9 for the variance? What does a background of 20 photons do at N = 300?
C8. Compare one-residual.png with two-residual.png and the printed residual sums of squares. What does the residual of the two-spot patch tell you?
The model solution is pointdetection/src/spot_fit/solution.rs in the download.
C6. The centroid (7 × 7 window, background not subtracted) is pulled towards the pixel centre, in proportion to the offset: −0.050, −0.099, −0.149, −0.199 px at δ = 0.1 … 0.4, as the centroid-bias formula predicts (the background’s share of the light). The parabola is pulled the same way but less, and not in proportion: −0.022, −0.040, −0.048, −0.038 px, the S-shaped pixel-locking curve, zero at 0 and 0.5. The log-parabola (background subtracted) and the fit have no bias (0.000). The demo’s −0.150 px for the centroid and −0.044 px for the parabola at 0.3 are almost the same; it integrates the spot over each pixel, the program samples it at the pixel centre.
C7. The fit: 13.8 nm at N = 100 and 4.3 nm at N = 1 000 against bounds of 10.0 and 3.2, about 1.35 times the bound, close to \sqrt{16/9} = 1.33 for least squares. The log-parabola uses only five pixels and is far worse at low N (27.5 nm at N = 100), where a few counts make the logarithms noisy. The centroid in a 5 × 5 window with the background subtracted exactly does well without background (11.9 and 4.0 nm), but at N = 10 000 it stays at 2.2 nm against 1.3 for the fit: the small window cuts off the tails of the spot, a bias that no number of photons removes. With b = 20 at N = 300 the fit gives 11.0 nm against a bound of 5.8 without background, almost twice: background photons carry no position information but add noise (the demo: 11.3 nm).
C8. The one-spot residual looks like noise, a little larger in the middle, where the counts and therefore the Poisson noise are largest (sum of squares 1 889). The two-spot residual has a bright lobe on each side and a dark centre (sum of squares 13 196, seven times larger): the single model sits between the two spots, too much light in the middle and too little at both sides. That pattern is the signal to fit two spots (next unit).
Where this leads
The fit assumed one spot per patch. The next unit asks what happens when two spots overlap, when a fit with two spots is worth its extra parameters, and how to score a whole detector against ground truth: detection and localisation separately.