Exercise: Richardson–Lucy, 3D, and the PSF
Before this unit: Regularisation: what we add, and how much (previous unit); Day 4: Image formation; Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- run Richardson–Lucy by hand for two steps and check that it keeps the total and stays non-negative,
- explain semi-convergence and choose when to stop, depending on the number of photons,
- say what 3D deconvolution does for widefield and confocal stacks, and why it cannot fill the missing cone,
- predict the effect of a wrong PSF, and why blind deconvolution needs constraints,
- derive RL from the Poisson likelihood (math).
Do the pen-and-paper part first, then the demo. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Idea | What it means |
|---|---|
| Data | photon counts y after removing the camera offset; Poisson noise |
| RL step | blur the guess, ratio y/(\text{blurred} + b), spread the ratio back with the mirrored PSF, multiply |
| Start | flat, every pixel = mean of y (minus the background) |
| Properties | never negative; total kept; the Poisson likelihood never decreases |
| Semi-convergence | error against the truth falls, then rises: the number of iterations is the regularisation parameter |
| Missing cone | the widefield OTF is zero in a cone around the k_z axis; confocal fills it |
| Wrong PSF | too narrow: blur left; too wide: halos, ringing, objects too small |
By hand
B1. 1D, circular, 4 pixels, PSF (0.25, 0.5, 0.25), data \mathbf y = (0, 2, 2, 0), no background. (a) The flat start is \mathbf x_0 = (1, 1, 1, 1). Why 1? (b) Compute \mathbf x_1: blur \mathbf x_0, form the ratio, spread it back, multiply. (c) Compute \mathbf x_2. Check the sum after each step. (d) What does the estimate approach if you continue? Is that plausible for these data?
B2. In the demo’s bead image the error against the truth is smallest after 40 iterations with 1000 photons per unit brightness, and after 8 iterations with 100 photons. (a) Explain why fewer photons need fewer iterations. (b) The misfit (Poisson deviance per pixel) falls to about 1 within the first 10 iterations at 100 photons and then only slowly. Why is “misfit ≈ 1” a reasonable place to stop?
B3. A widefield stack of a cell contains: round beads, microtubules running in all directions, the nuclear envelope (a closed surface), and a flat basal membrane on the coverslip. After 3D deconvolution, which of these is restored least in z, and why? What would change with a confocal stack?
B4. A bead of true FWHM 150 nm is imaged and deconvolved (a) with the correct PSF, (b) with a PSF 20 % too wide, (c) with a PSF 20 % too narrow. Which way is the measured FWHM biased in (b) and (c), and what else do you see around the bead?
B5. An image has a camera offset of 100 counts per pixel and a sample whose brightest spots reach 300 counts. You run RL on the raw image without a background term. Predict what happens to the dark areas, and to the total flux of the estimate.
B6. Blind deconvolution alternates RL steps on the object and on the PSF. (a) Show that “PSF = a point, object = the data” is a perfect fit. (b) Name three constraints that rule this out, and the price of each.
B1. (a) The mean of the data: (0 + 2 + 2 + 0)/4 = 1. The flat start has the same total (4) as the data. (b) Blurred \mathbf x_0 = (1, 1, 1, 1); ratio (0, 2, 2, 0); spread back: pixel 1: 0.25 \cdot 0 + 0.5 \cdot 0 + 0.25 \cdot 2 = 0.5 (its neighbours are pixels 4 and 2), pixel 2: 0.25 \cdot 0 + 0.5 \cdot 2 + 0.25 \cdot 2 = 1.5, pixel 3: 1.5, pixel 4: 0.5. \mathbf x_1 = (0.5, 1.5, 1.5, 0.5), sum 4. (c) Blurred \mathbf x_1: pixel 1: 0.25 \cdot 0.5 + 0.5 \cdot 0.5 + 0.25 \cdot 1.5 = 0.75, pixel 2: 0.25 \cdot 0.5 + 0.5 \cdot 1.5 + 0.25 \cdot 1.5 = 1.25, pixel 3: 1.25, pixel 4: 0.75. Ratio (0, 1.6, 1.6, 0). Spread back: (0.4, 1.2, 1.2, 0.4). \mathbf x_2 = (0.2, 1.8, 1.8, 0.2), sum 4. (A third step gives (0.071, 1.929, 1.929, 0.071).) (d) The outer pixels shrink towards 0 and the middle ones grow towards 2: (0, 2, 2, 0). Plausible for these noise-free data: blurring (0, 2, 2, 0) circularly gives (0.5, 1.5, 1.5, 0.5), and RL is undoing exactly such a blur. With noisy data the same sharpening would also amplify noise.
B2. (a) With fewer photons the relative noise is larger (1/\sqrt{\text{count}}), so the noise dominates already at lower frequencies. RL brings in the weak, high frequencies only after many iterations, so it starts fitting noise sooner. (b) A Poisson deviance of about 1 per pixel is what the noise alone produces: the blurred estimate explains the data as well as the noise level allows. Pushing it further fits the noise (a discrepancy principle for counts).
B3. The flat basal membrane: it is uniform in x and y, so its frequencies lie along the k_z axis, inside the widefield missing cone; it stays smeared in z. The horizontal parts of the nuclear envelope (top and bottom) suffer in the same way, its sides much less; beads and microtubules running at an angle are restored well. A confocal OTF has no missing cone, so the membrane is restored as well (with less contrast gain overall).
B4. (b) Too wide: over-sharpened, the measured FWHM is too small (below the value obtained with the right PSF), with a dark rim (halo) and possibly ringing around the bead. (c) Too narrow: under-restored, the FWHM stays too large; no halo. In the demo (beads, 1000 photons, k = 100): 2.57 px with a PSF 30 % too wide, 2.63 px correct, 3.25 px 30 % too narrow, and a rim down to 0.013 against a background of 0.05 for the wide PSF.
B5. The offset (100 counts everywhere) must be explained as object light. RL concentrates it into bright speckles in the dark areas, and the sharpening of real structure suffers. The total flux of the estimate equals the total of the raw data, including the offset: it is about 100 × (number of pixels) too large, so any intensity measurement is biased. Subtract the offset and model the remaining background.
B6. (a) With h = a single point (sum 1), h \ast x = x; with x = y the model reproduces the data exactly, the best possible fit. (b) A size limit (the PSF must be zero outside a radius; price: the true PSF may be larger), smoothness or a parametric shape such as a Gaussian of unknown width (price: real aberrations are not captured), a known width or a measured starting PSF that is changed only a little (price: it must be known). In the demo none of them stops the drift completely: unconstrained, the width falls from 7.1 px to 3.3 px in 300 iterations (true 4.8 px); with a Gaussian shape to 6.0 px.
Math questions
B7. (math) For y_i \sim \text{Poisson}((A\mathbf x)_i) write the log-likelihood, compute its gradient, and show that the RL update x_j \leftarrow x_j [A^\top(\mathbf y/A\mathbf x)]_j has the maximum-likelihood conditions as its fixed points (for x_j > 0), using A^\top\mathbf 1 = \mathbf 1.
B8. (math) With a background, \mathbf y \approx A\mathbf x + b: show that the total of the new estimate is \sum_i y_i\frac{(A\mathbf x)_i}{(A\mathbf x)_i + b}. When is it equal to \sum_i (y_i - b)?
B9. (math) Show that RL can be written as a gradient step with a pixel-dependent step size: x_j^{\text{new}} = x_j + x_j\,\partial\ell/\partial x_j (no background). Why does this step size keep the estimate non-negative where a plain gradient step would not?
B7. \ell(\mathbf x) = \sum_i y_i\log(A\mathbf x)_i - (A\mathbf x)_i - \log y_i!. \partial\ell/\partial x_j = \sum_i A_{ij}\frac{y_i}{(A\mathbf x)_i} - \sum_i A_{ij} = [A^\top(\mathbf y/A\mathbf x)]_j - 1, using \sum_i A_{ij} = [A^\top\mathbf 1]_j = 1. At a maximum with x_j > 0 the gradient is 0, i.e. [A^\top(\mathbf y/A\mathbf x)]_j = 1, which is exactly the condition x_j^{\text{new}} = x_j of the RL update. (Where x_j = 0 the Karush–Kuhn–Tucker condition only requires the gradient to be ≤ 0, and RL leaves the pixel at 0.)
B8. \sum_j x_j^{\text{new}} = \sum_j x_j\sum_i A_{ij}\frac{y_i}{(A\mathbf x)_i + b} = \sum_i \frac{y_i}{(A\mathbf x)_i + b}(A\mathbf x)_i. It equals \sum_i(y_i - b) when the model fits the data exactly, (A\mathbf x)_i + b = y_i for every i; in general only approximately (in expectation at the right \mathbf x, since E[y_i] = (A\mathbf x)_i + b).
B9. x_j + x_j\,\partial\ell/\partial x_j = x_j + x_j([A^\top(\mathbf y/A\mathbf x)]_j - 1) = x_j[A^\top(\mathbf y/A\mathbf x)]_j: the RL update. The step size is proportional to x_j itself, so a pixel close to 0 moves only by a small amount; since [A^\top(\mathbf y/A\mathbf x)]_j \ge 0, the new value is never negative. A plain gradient step with a fixed step size can overshoot below 0 where the gradient is negative.
Explore: Richardson–Lucy step by step
A 128 × 128 test object is blurred (Gaussian PSF σ = 2 px, or Airy) and Poisson noise is drawn (fixed seed); “photons at 1” is the expected count where the object has value 1. RL runs in the background after every change and keeps about 30 snapshots; the iteration k slider scrubs through them. Panels: observed, RL estimate, error (estimate − truth, blue negative, red positive), and the true PSF next to the one used (or the current blind estimate). Right of them: a profile along row 24 (through the 1 : 4 bead pair and the edge), and the error (% of the largest true value) and the misfit (Poisson deviance per pixel) against the iteration, with the best Wiener error dashed. The readout gives the peak and integrated ratio of the bead pair (truth 4.0) and the total flux.
E1. Preset noise: semi-convergence (100 photons). At which k is the error smallest, and how large is it? What are the error and the misfit at k = 10 and k = 1000? Compare with the best Wiener error.
E2. Preset no noise: keeps improving. Where is the error smallest now, and what happens to the misfit? Then fewer photons: stop earlier (20 photons): the best k and error. Set photons to 1000 (preset semi, then the slider): the best k?
E3. Preset 1000 iterations: speckles (cell phantom). Compare the error at k = 5 and k = 1000 and describe the background. Then forgot the background: what happens in the dark regions, and how does the total flux of the estimate compare with the data? Tick background in the model: the best k and error.
E4. Presets PSF too wide: halos and PSF too narrow: still blurred (k = 100). Describe the beads in each, and read the peak ratio of the bead pair. Which error is lower?
E5. Preset blind: the PSF shrinks to a point. Follow the FWHM of the blind PSF in the readout at k = 10, 100 and 300 (true 4.76 px). Then blind with a Gaussian-shape constraint. Which constraint helps, and does either give the true PSF?
E1. Smallest error at k = 8: 2.20 %. At k = 10: error 2.20 %, misfit 0.98; at k = 1000: error 10.4 %, misfit 0.89 (below 1: noise is fitted). Best Wiener: 2.49 %, so RL at the right stopping point beats it.
E2. Without noise the error keeps falling to k = 1000 (1.22 %), and the misfit drops towards 0 (0.0002): nothing stops the deblurring but the iterations. With 20 photons: best at k = 3 (2.56 %). With 1000 photons: best at k = 40 (1.79 %).
E3. k = 5: 2.74 %; k = 1000: 18.7 %; the background is broken up into bright speckles and the cell interior is grainy. Forgot the background (30 photons per pixel, not in the model): the dark regions fill with speckles (up to 0.70 where the truth is 0.05), the error is never below 27.5 %, and the total flux of the estimate equals that of the data, background included (7.5e5). With the background in the model: best at k = 10, 3.18 %, and the flux of the estimate is the data minus the background (2.6e5).
E4. Too wide (×1.3): over-sharpened beads with dark rims, and the pair’s peak ratio is 9.1 instead of 4. Too narrow (×0.7): the beads stay round and wide (peak ratio 4.2, but only because both stay blurred). Error at k = 100: 2.58 % (wide) and 2.47 % (narrow); with the right PSF 1.87 %. Over-sharpening looks better and is not.
E5. No constraint: FWHM 6.44 px at k = 10, 4.90 at 100 (close to the true 4.76), 3.33 at 300: the PSF passes the right width and keeps shrinking towards a point. The Gaussian shape slows the drift (6.90, 6.34, 5.95) but does not find the true width either. The size limit (r ≤ 6 px) changes almost nothing here. Blind estimation needs strong constraints and a good starting PSF.
Explore: a 3D stack
A 3D volume of 64 × 32 × 32 voxels (100 nm across, 200 nm between planes) with two beads, a vertical filament, a horizontal sheet 200 nm thick and a hollow sphere, imaged with NA 0.8 in air (widefield or confocal 3D PSF), 1000 photons at value 1. Top row: xz slices (drawn to scale) of truth, observed and RL estimate; bottom row: xy slices at the middle plane. Right: the OTF magnitude in the (k_x, k_z) plane (log scale) with the missing cone dashed, and an axial profile (truth, the blurred stack before noise, and RL, each scaled to its own maximum so that the widths can be compared). The readout gives axial FWHMs, measured on the blurred stack before noise and on the RL estimate.
E6. Preset RL restores the bead in z. Read the bead’s axial FWHM before and after 100 iterations. Then the sheet stays smeared: the sheet’s axial FWHM in the truth, blurred and after 100 and 200 iterations. Explain with the OTF image.
E7. Preset confocal: less to gain, nothing missing. Compare the OTF image with the widefield one, and the FWHMs of bead and sheet with E6.
E8. Preset z-steps too large (800 nm between planes). What happens to the bead’s axial FWHM after RL, and why?
E6. Bead: 1476 nm blurred, 526 nm after 100 iterations (444 nm after 200): a factor of about 3. Sheet: 200 nm thick, 4666 nm blurred, 1345 nm after 100 and 1111 nm after 200 iterations: still more than 5 times too thick. The sheet’s frequencies lie along the k_z axis, where the widefield OTF (dark region above and below the centre, inside the dashed cone) is zero: they were never recorded. The bead’s frequencies spread in all directions, and those outside the cone are restored.
E7. The confocal OTF covers the cone (no dark wedge above and below the centre). Bead: 854 nm blurred (already better than widefield) → 233 nm; sheet: 921 nm → 282 nm. Less blur to start with, and no missing information: the sheet is restored too.
E8. With 800 nm steps the bead goes only from 1178 nm to 808 nm (one plane is 800 nm). The stack samples the PSF axially below Nyquist: the fine axial structure is not in the data, and deconvolution cannot sharpen below the sampling.
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 deconvolution/src/richardson_lucy/exercise.rs.
C1. blur(img, psf): blur a periodic image with a PSF (the image repeats across its edges, as for the Fourier transform), \text{out}(x, y) = \sum h(i, j)\, I(x - d_x, y - d_y) with (d_x, d_y) the offset of PSF pixel (i, j) from its centre.
C2. rl_step(x, y, psf, background): one step of the slide “Richardson–Lucy in words”, with a constant background b:
blurred = blur(x, h) + b # what the microscope would show
ratio = y / blurred # pixel by pixel; 0 where blurred = 0
back = blur(ratio, h mirrored) # spread the ratio back
x_new = x · back # pixel by pixel
The checks are the table of the slide “One step by hand” and your \mathbf x_1, \mathbf x_2 of B1. A PSF that shifts the image one pixel to the right checks that the ratio is spread back with the mirrored PSF.
C3. richardson_lucy(y, psf, iterations, background): start from a flat image (every pixel the mean of y - b) and repeat C2. The checks: \mathbf x_3 of B1, and the total number of photons stays that of the data, with no negative pixel.
C4. poisson_deviance(y, m): the misfit of the slide “The number of iterations is λ”, the mean over the pixels of 2\big(y\ln(y/m) - (y - m)\big) (the first term is 0 where y = 0).
Then cargo run -p deconvolution -- richardson-lucy and open the pictures in out/richardson-lucy/. The program makes a fluorescence-like object from the micrograph (half size, inverted: bright structures on a dark background, 72 \times 72 pixels), blurs it with a Gaussian PSF (\sigma = 1.5 pixels) and records it as photon counts, 100 photons at brightness 1. It restores it with your Richardson–Lucy and prints, after k steps, the RMSE against the object, the misfit, the total and the smallest pixel. At the end it adds a background of 20 photons per pixel and restores without and with the background term.
C5. At which k is the error smallest? What do the misfit and the error do after that? Where would the rule “stop when the misfit reaches 1” stop? Check the last two columns against the promises of the lecture.
C6. Open rl-5.png, rl-20.png and rl-200.png next to truth.png and observed.png. Describe what happens as k grows.
C7. Compare the two restorations with the background of 20 photons per pixel (rl-20-background-ignored.png and rl-20-background-modelled.png): their RMSE, their total, and how they look.
The model solution is deconvolution/src/richardson_lucy/solution.rs in the download.
C5. The observed image (divided by 100) has an RMSE of 0.1119. RL reaches its smallest error, 0.0915, at k = 5, then the error grows again: 0.1031 at 20, 0.1313 at 50, 0.2270 at 200 (semi-convergence). The misfit keeps falling all the time (1.277 at k = 1, 0.938 at 5, 0.773 at 200): fitting the data ever better is not getting closer to the object. It first drops below 1 at k = 3 (0.990), close to the best k. The total stays 231 678 photons, exactly that of the data, and the smallest pixel stays positive (5.9 photons after one step, 0.023 after 200): RL keeps the photons and never goes negative.
C6. After 5 steps the photon noise is gone and the image is a little sharper than the observed one, but still soft. After 20 steps it is sharper but grainy. After 200 steps the noise has become a dense pattern of bright dots and dark gaps all over the image: the speckles of the slide “Artefacts”.
C7. Background ignored: RMSE 0.2251, total 335 358, the data’s photons including the 20 \times 72 \times 72 = 103\,680 background photons. The background is explained as object light, so the whole image is too bright and grainy. Background modelled: RMSE 0.0979, total 231 788, almost exactly the object’s photons, and the picture looks like the restoration without a background.
Where this leads
The next unit asks what to do when there is nothing to un-blur (classical denoisers compared by what they destroy) and, most importantly, what a restored image may be used for: which measurements survive deconvolution and which do not.