Exercise: thresholds from noise statistics
Before this unit: Finding spots: filter, then local maxima (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:
- estimate the noise of an image robustly with the MAD,
- compute the noise of a filtered image and a threshold in units of it,
- predict the number of false spots per image for a threshold k, and explain why the normal tail underestimates it,
- compute the detection limit for a given threshold and noise,
- explain what Poisson noise does to a global threshold, and how a local noise estimate or the Anscombe transform fixes it,
- (math) derive the MAD constant, the detection test, the Euler-characteristic estimate and the Anscombe variance.
Background
| Idea | What it means |
|---|---|
| MAD noise estimate | \hat\sigma = \mathrm{median}\,|x - \mathrm{median}(x)|/0.6745; robust: spots are a small fraction of pixels |
| Noise after a filter | \sigma_n\sqrt{\sum_k w_k^2}; normalised Gaussian at \sigma_f: \approx \sigma_n/(2\sqrt\pi\sigma_f) = 0.188\,\sigma_n at \sigma_f = 1.5 |
| Threshold | t = k \times noise sd of the filtered image |
| Noise maxima (white noise, Gaussian \sigma_f = 1.5, 3 × 3 windows) | 4 862 per 512² image; above k = 3, 4, 5: 99, 4.3, 0.1 per image (20 seeded images) |
| Euler formula | \mathbb E[\#\text{maxima} > u] \approx A\,\frac{1}{2\sigma_f^2}\,\frac{u\,e^{-u^2/2}}{(2\pi)^{3/2}} (123, 5.0, 0.07 for the same case) |
| Detection limit | A_\text{min} \approx k\sigma_n/\sqrt{\pi\sigma^2} |
| Poisson noise | variance = mean; local estimate \sqrt{\text{gain}\cdot\text{background} + \text{read}^2}, or Anscombe 2\sqrt{x + 3/8} (sd ≈ 1) |
By hand
B1. MAD. The finest-scale values of a small background patch are (3, -1, 0, 2, -2, 1, 25, -1, 0); the 25 is a spot. (a) Compute the median, the absolute deviations and their median, and \hat\sigma. (b) Compute the ordinary standard deviation of the nine values. Which estimate describes the noise? (c) How many of the nine values could be spots before the MAD estimate breaks down completely?
B2. Threshold. The image noise is \sigma_n = 10; the filter is a normalised Gaussian with \sigma_f = 1.5. (a) What is the noise sd of the filtered image? (b) What threshold (in filtered grey values) corresponds to k = 4? (c) You change the filter to σ_f = 3 (and keep k = 4). Does the threshold in grey values go up or down? Does the number of false spots per image go up or down? (Use the Euler formula’s dependence on \sigma_f.)
B3. False spots in a bigger image. Use the Background numbers for 512² images. (a) A 2048² image: how many noise maxima, and how many false spots at k = 3, 4 and 5? (b) You want at most one false spot per 2048² image. Which of the three k is enough? (c) The naive estimate is (number of maxima) × P(Z > k), with P(Z > 3) = 0.00135 and P(Z > 4) = 3.2 \times 10^{-5}. Compute it for the 512² image at k = 3 and 4, and compare with the measured counts.
B4. Detection limit. k = 4, \sigma_n = 10, spots of σ = 1.5 px. (a) What is the dimmest spot (peak amplitude above the background) that reaches the threshold at its centre? (b) A spot of exactly this amplitude: what is its chance to be detected, roughly? (The noise at its centre moves the filtered value up or down.) (c) With k = 5?
B5. Poisson. Two regions of an image have backgrounds of 100 and 400 photons per pixel. (a) What are the noise standard deviations? (b) A global threshold is set at k = 4 for the dark region. What is it in units of the bright region’s noise? (c) Use the measured noise-maximum counts (at k = 2 there are about 962 per 512² image, at k = 4 about 4.3) to estimate how many times more false spots per area the bright region produces.
B1. (a) Sorted: -2, -1, -1, 0, 0, 1, 2, 3, 25: median 0. Absolute deviations: 3, 1, 0, 2, 2, 1, 25, 1, 0, sorted 0, 0, 1, 1, 1, 2, 2, 3, 25: median 1. \hat\sigma = 1/0.6745 = 1.48. (b) The mean is 3, the standard deviation 8.4: inflated almost six-fold by one value. The MAD describes the noise. (c) Up to four: as long as fewer than half the values are outliers, the median of the deviations stays among the noise values (with five of nine outliers it jumps to an outlier).
B2. (a) 10 \times 0.188 = 1.88. (b) 4 \times 1.88 = 7.5. (c) The noise after a wider filter is smaller (10 \times 0.094 = 0.94), so the threshold in grey values goes down (3.8). The number of false spots goes down too: the formula scales with 1/\sigma_f^2, a smoother image has 4 times fewer maxima, so 4 times fewer false spots at the same k. (But wider filters merge close spots and lose some sensitivity to small spots: units 1 and 4.)
B3. (a) 16 times the area: 16 \times 4\,862 \approx 77\,800 maxima; false spots 16 \times 99 \approx 1\,590 (k = 3), 16 \times 4.3 \approx 69 (k = 4), 16 \times 0.1 = 1.6 (k = 5). (b) Not even k = 5 quite (1.6 per image); about k = 5.1 would be needed (the Euler formula gives 1.1 at k = 5 and 0.7 at 5.1 for 2048²). (c) 4\,862 \times 0.00135 = 6.6 at k = 3 (measured 99: 15 times more); 4\,862 \times 3.2 \times 10^{-5} = 0.15 at k = 4 (measured 4.3: 28 times more). The naive estimate is worse the higher k, as the math slide says (a missing factor of about k^2).
B4. (a) A_\text{min} = 4 \times 10/\sqrt{\pi \cdot 2.25} = 40/2.66 = 15.0. (b) About 50 %: at exactly the threshold, the noise pushes the filtered value up as often as down. (In practice slightly less, because the maximum may also shift to a neighbouring pixel.) For about 95 % detection the spot needs a matched-filter SNR of about k + 1.6. (c) 50/2.66 = 18.8.
B5. (a) \sqrt{100} = 10 and \sqrt{400} = 20. (b) 4 \times 10 = 40 grey levels, which is 40/20 = 2 sd in the bright region. (c) Per area, 962/4.3 \approx 224 times more false spots (the Euler formula gives a ratio of 202). One global threshold is badly wrong for Poisson noise.
Math questions
B6. (math) Show that for normally distributed noise \mathrm{median}\,|x - \mathrm{median}(x)| \to 0.6745\,\sigma as the number of values grows.
B7. (math) The detection test. In a window, I_k = A p_k + B + n_k with known spot shape \mathbf p, unknown A and B, and white noise of known variance \sigma_n^2. (a) Write the residual sum of squares for the best fit with A = 0 (background only) and with A free. (Hint: with \mathbf q = \mathbf p - \bar p\mathbf 1, the model is A\mathbf q + B'\mathbf 1 with B' = B + A\bar p, and \mathbf q \perp \mathbf 1.) (b) Show that twice the log likelihood ratio is (\mathbf q^\top\mathbf I)^2/(\sigma_n^2\|\mathbf q\|^2). (c) Explain why thresholding this at k^2 is the zero-mean matched filter thresholded at k noise sd.
B8. (math) The Euler formula. Evaluate A\,\frac{1}{2\sigma_f^2}\,\frac{u e^{-u^2/2}}{(2\pi)^{3/2}} for A = 1024^2, \sigma_f = 2 and u = 4, 4.5, 5. Which threshold gives about one false spot per image?
B9. (math) Anscombe. Use the delta method, \mathrm{Var}\,g(X) \approx g'(\mu)^2\mathrm{Var}\,X, to show that g(x) = 2\sqrt{x + 3/8} gives variance ≈ 1 for Poisson counts. Evaluate the approximation at \mu = 4 and \mu = 100.
B6. The sample median converges to the population median; here the median m of |X - \mu|, defined by P(|X - \mu| \le m) = \tfrac12. With Z = (X - \mu)/\sigma: 2\Phi(m/\sigma) - 1 = \tfrac12, so m = \Phi^{-1}(0.75)\,\sigma = 0.6745\,\sigma. (The sample median of x converges to \mu, so using it instead of \mu does not change the limit.)
B7. (a) Background only: \mathrm{RSS}_0 = \|\mathbf I - \bar I\mathbf 1\|^2. With A free, the model A\mathbf q + B'\mathbf 1 has orthogonal columns, so \hat A = \mathbf q^\top\mathbf I/\|\mathbf q\|^2, \hat B' = \bar I, and \mathrm{RSS}_1 = \mathrm{RSS}_0 - (\mathbf q^\top\mathbf I)^2/\|\mathbf q\|^2. (b) For Gaussian noise of known variance, 2\ln\Lambda = (\mathrm{RSS}_0 - \mathrm{RSS}_1)/\sigma_n^2 = (\mathbf q^\top\mathbf I)^2/(\sigma_n^2\|\mathbf q\|^2). (c) \mathbf q^\top\mathbf I is the zero-mean matched-filter response, and \sigma_n\|\mathbf q\| its noise sd. 2\ln\Lambda > k^2 is |\text{response}|/\text{sd} > k; for bright spots we keep only the positive side.
B8. A/(2\sigma_f^2) = 1\,048\,576/8 = 131\,072, divided by (2\pi)^{3/2} = 15.75: 8 322. Times u e^{-u^2/2}: u = 4: 11.2; u = 4.5: 1.50; u = 5: 0.16. About one false spot per image needs u \approx 4.6.
B9. g'(x) = 1/\sqrt{x + 3/8}, so \mathrm{Var}\,g(X) \approx \mu/(\mu + 3/8): 0.91 at \mu = 4, 0.996 at \mu = 100. The variance no longer depends on \mu (to first order), so one threshold fits every brightness.
Explore: how many false spots?
A seeded 256 × 256 image of noise only (flat background 100, sd 10) or spots + noise, with Gaussian or Poisson noise (20 photons per pixel with a broad bright patch of up to 200). The filter is a Gaussian minus a wide blur, or a LoG; the response is divided by its noise sd and local maxima (3 × 3) above k are detections (red = false, green = true, yellow = missed). The histogram shows the values of all local maxima against the shape ordinary pixels would have (dashed). The lower plot shows false spots per image against k: measured on 16 seeded noise images, the naive tail estimate and the Euler formula; the red dot is the current image.
E1. Preset k = 3 is not safe. How many false spots does this image have, and how many does the curve predict? How many local maxima are there? Compare with 0.13 % of the maxima.
E2. Preset maxima are higher than pixels. Where is the peak of the maxima histogram compared with the dashed curve? What does this mean for a threshold of 2?
E3. Preset the formula vs the count. How close are the measured curve and the Euler formula between k = 3 and 5? Where does the naive estimate lie?
E4. Change the filter σ from 1.5 to 3 at k = 3. What happens to the number of maxima and to the false spots? Compare with B2(c).
E5. Preset bigger image, more false spots. How many false spots now, and why? Which k brings it back to about the 256² count at k = 3?
E6. Preset Poisson: false spots on the bright patch, then local noise estimate fixes it and Anscombe fixes it. Read the false spots in each case and explain where they went. What happens to the histogram?
E7. Preset dim spots: the detection limit (k = 5). How many spots are missed? Lower k step by step to 3: read the false spots and the recall. Which k would you choose, and why?
E1. 25 false spots; the measured curve predicts 26 (16 noise images). The image has 1 164 local maxima, and 0.13 % of them would be only 1.6: the maxima are not ordinary pixels.
E2. The maxima histogram peaks at about 1.3 sd, the dashed curve (pixels) at 0. The whole distribution is shifted to the right, so a threshold of 2 lets through a large fraction of the maxima: 249 false spots in this image.
E3. The Euler curve lies almost on the measured one from k = 3 to 4.5 (slightly above it); above k \approx 4.5 the measurement on 16 small images runs out of events. The naive estimate lies more than ten times lower at k = 3 and further below for larger k.
E4. The smoother response has about four times fewer maxima (on noise images: 326 instead of 1 205 per 256² image), and the false spots drop accordingly (8.6 instead of 26 per image at k = 3; the Euler formula scales with 1/\sigma_f^2). The price, with spots, is lower sensitivity to small spots and merging of close ones.
E5. 117 false spots at k = 3 (112 expected): four times the area, four times the maxima, four times the false spots. Raising k to about 3.45 brings it back to about 25 (read the measured curve; on 20 seeded 512² noise images, 26.9 at k = 3.45).
E6. With the global noise estimate (7.25, set by the many dark pixels) the threshold on the bright patch is only about 2 of its local sd: 87 false spots, 80 on the patch (noise images predict 1.7). The local estimate divides by \sqrt{\text{background}} at each pixel: 1 false spot, none on the patch. Anscombe first, then one global estimate (1.00 in Anscombe units): 0 false spots. The histogram loses its long right tail and looks like the noise-only one again.
E7. At k = 5: no false spots, 10 of 40 spots missed (recall 0.75). Lowering k finds more of the dim spots (recall rises) while false spots appear, slowly at first and then quickly below about 3.5 (compare the measured curve). A k around 4 keeps the false spots near one per image while finding most spots; the right choice depends on the cost of a false spot against a missed one (unit 4: precision–recall curve).
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_threshold/exercise.rs.
C1. mad_sigma(values): the robust noise estimate:
m = median of the values
σ̂ = median of |value − m|, over all values, divided by 0.6745
The check is B1 (a) and (c).
C2. estimate_noise(img): the MAD of a high-pass image, here the differences of horizontal neighbours d = I(x+1, y) - I(x, y), divided by \sqrt2 (the noise variances of the two pixels add). The check: noise of sd 0.05 on a steep ramp with bright squares.
C3. maxima_above(z, k): the local maxima of a response in noise sd (larger than all 8 neighbours) that are above k. The checks count noise maxima: about 1 220 per 256 \times 256 image at \sigma_f = 1.5, and about 26 above k = 3, the numbers of the lecture.
C4. euler_expected(area, sigma_f, u): the Euler formula of the Background table. The checks are the lecture’s 123, 5.0, 0.07 and your answers to B8.
C5. anscombe(img): 2\sqrt{x + 3/8} for every pixel. The check: Poisson counts of mean 100 and 20 have variance close to 1 afterwards (B9).
Then cargo run -p pointdetection -- spot-threshold and read the printed tables; the pictures are in out/spot-threshold/ (the response in noise sd, with the maxima above the threshold in red). The program filters 12 seeded noise images of 512 \times 512 (sd 10) with a Gaussian of \sigma_f = 1.5, divides by the estimated noise after the filter and counts the maxima above k = 3, 4, 5. Then it makes an image of Poisson noise only (20 photons per pixel, a broad bright patch of up to 200) and thresholds it at k = 4 in three ways.
C6. Compare the measured false spots per image with the Euler formula and with the naive estimate. Which is closer? Does the measurement agree with the lecture’s 99, 4.3 and 0.1?
C7. For the Poisson image: how many false spots with the global noise estimate, and where are they (poisson-global.png)? How many with the local estimate and with Anscombe? Explain.
C8. (math) The program divides the filtered response by \hat\sigma\sqrt{\sum w^2}. Why is this the right number for a Gaussian filter, and why is it wrong when the noise of neighbouring pixels is correlated?
The model solution is pointdetection/src/spot_threshold/solution.rs in the download.
C6. The noise estimate is 9.98 to 9.99 (true 10). Each image has about 4 822 local maxima (the lecture: 4 862). Above k = 3, 4, 5: 103.6, 3.8 and 0.0 per image (the lecture, 20 images: 99, 4.3, 0.1). The Euler formula gives 123, 5.0 and 0.07: a little high, but the right size at every k. The naive estimate gives 6.5 and 0.15: 16 and 25 times too low, and worse the higher k.
C7. Global estimate (6.29 photons, set by the many dark pixels): 86 false spots, all 86 on the bright patch, where the true noise sd is up to \sqrt{200} = 14, so k = 4 there is only about 2 local sd. Local estimate, dividing by \sqrt{\text{background}}: 1 false spot. Anscombe first (the estimate is then 1.00, as it should be), one global threshold: 1 false spot. Both make the noise the same everywhere before the threshold; in poisson-global.png the response is visibly rougher on the patch, while in poisson-anscombe.png it looks the same everywhere.
C8. The filtered value is \sum_k w_k I_k. For independent pixels of variance \sigma^2, the variance of a weighted sum is \sigma^2\sum_k w_k^2 (B8 of the previous unit). If neighbouring pixels are correlated (the camera, or an image that was smoothed or compressed before), the covariances 2\sum_{k<l} w_k w_l\,\mathrm{Cov}(I_k, I_l) add to it; for a positive filter and positive correlations the true noise after the filter is larger, so the threshold is too low and more false spots pass. Then estimate the noise on the filtered image itself, or on blank images.
Where this leads
Every detection now has a position on the pixel grid and a score in units of noise. The next unit measures the position between the pixels, and asks how precise that can be: the answer is set by the number of photons.