Exercise: linear deconvolution
Before this unit: Day 4: Image formation; Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- compute inverse, truncated and Wiener gains for given H and SNR, and the noise they let through,
- solve Tikhonov on a two-pixel blur for several λ, and say what the energy and roughness penalties do,
- predict a restored image from its gain curve, and diagnose noise, ringing and halos,
- derive the Wiener filter and the Tikhonov normal equations (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 |
|---|---|
| Forward model | g = h \ast f + n; per frequency G = HF + N |
| Inverse filter | \hat F = G/H: gain 1/H |
| Truncated inverse | gain 1/H where \lvert H\rvert > \varepsilon, else 0 |
| Wiener filter | gain H^*/(\lvert H\rvert^2 + 1/\mathrm{SNR}); SNR = signal power / noise power at that frequency |
| Tikhonov | minimise \lVert A\mathbf x - \mathbf g\rVert^2 + \lambda\lVert D\mathbf x\rVert^2; per frequency H^* G/(\lvert H\rvert^2 + \lambda\lvert\hat D\rvert^2) |
| Penalties | energy: D = I, \hat D = 1; roughness: D = Laplacian, \hat D(0) = 0 |
| Effective transfer | \lvert WH\rvert: what optics and restoration pass together |
By hand
B1. Inverse and Wiener gains, real H, SNR = 100 at every frequency. (a) Fill in the inverse gain 1/H and the Wiener gain for H = 1, 0.5, 0.1, 0.05, 0.01. (b) The data contain white noise of 1 % of the image’s brightness at every frequency. How large is the noise after the inverse filter at each of these frequencies? (c) At which H is the Wiener gain largest? Why does it fall again below that?
B2. A truncated inverse with ε = 0.1. The OTF takes the values H = 1, 0.5, 0.2, 0.1, 0.05 at five frequencies. Write the gain at each. What is the largest factor by which any noise can be amplified? What is lost?
B3. The two-pixel blur A = \begin{bmatrix} 0.75 & 0.25 \\ 0.25 & 0.75 \end{bmatrix} with data \mathbf g = (1, 0.5). (a) Check that (1, 1) and (1, -1) are eigenvectors of A, with eigenvalues 1 and 0.5. Split \mathbf g into a sum part and a difference part. (b) Tikhonov with the energy penalty multiplies each part by s/(s^2 + \lambda). Compute \hat{\mathbf x} for λ = 0, 0.1 and 1. (c) What happens to the sum \hat x_1 + \hat x_2 as λ grows? Would a penalty on the difference only, \lambda(x_1 - x_2)^2, change the sum?
B4. Diagnose. (a) A Wiener result is covered in fine speckles. (b) A result has dark rims around every bright cell. (c) A result looks like the observed image, only slightly sharper. For each, name the likely cause and the setting to change.
B5. A Gaussian PSF with σ = 2 pixels has H(f) = e^{-2\pi^2\sigma^2 f^2} (f in cycles per pixel). (a) Compute H at f = 0.1, 0.2 and 0.25. (b) Compute the Wiener gain (SNR = 100) and the effective transfer WH at these frequencies. (c) Up to about which frequency does the restoration pass detail? Compare with the optics alone (say, where H > 0.5).
B6. Tikhonov with the energy penalty and λ = 0.1 is applied to an image whose mean is 0.1575. What will the mean of the restored image be (the PSF sums to 1)? And with the roughness penalty?
B1. (a)
| H | 1 | 0.5 | 0.1 | 0.05 | 0.01 |
|---|---|---|---|---|---|
| inverse 1/H | 1 | 2 | 10 | 20 | 100 |
| Wiener H/(H^2 + 0.01) | 0.99 | 1.92 | 5.0 | 4.0 | 0.99 |
- The inverse multiplies the 1 % noise by 1/H: 1 %, 2 %, 10 %, 20 % and 100 %. At H = 0.01 the noise is as large as the image itself.
- The Wiener gain peaks at H = 0.1 (where H^2 = 1/\mathrm{SNR}; there the gain is 1/(2H) = 5, half the inverse). Below that the frequency is mostly noise, and the filter damps it instead of amplifying it.
B2. Gains 1, 2, 5, 0, 0 (|H| = 0.1 is not larger than ε, so it is dropped). No noise is amplified by more than 1/\varepsilon = 10, and here by at most 5. Lost: everything at the two dropped frequencies, including the true signal there.
B3. (a) A(1,1)^\top = (1, 1)^\top and A(1,-1)^\top = (0.5, -0.5)^\top. \mathbf g = 0.75\,(1, 1) + 0.25\,(1, -1): sum part (0.75, 0.75), difference part (0.25, -0.25). (b) λ = 0: gains 1/1 = 1 and 1/0.5 = 2: \hat{\mathbf x} = (0.75, 0.75) + (0.5, -0.5) = (1.25, 0.25), the exact inverse. λ = 0.1: 1/1.1 = 0.909 and 0.5/0.35 = 1.429: (0.682 + 0.357, 0.682 - 0.357) = (1.04, 0.33). λ = 1: 1/2 = 0.5 and 0.5/1.25 = 0.4: (0.375 + 0.1, 0.375 - 0.1) = (0.48, 0.28). (c) The sum is 2 \times 0.75 \times (sum gain): 1.5, 1.36 and 0.75: the energy penalty shrinks it. A penalty on the difference only acts like the roughness penalty: it leaves the sum part alone (gain 1/1 = 1 for every λ) and damps only the difference.
B4. (a) Too little regularisation: SNR set too high (or λ too small, ε too small); noise at high frequencies is amplified. Lower the SNR. (b) PSF used is too wide (over-sharpening subtracts light around bright objects), or a sharp frequency cut (truncated inverse) rings. Check the PSF width; use a smooth filter. (c) Too much regularisation (SNR too low, λ too large), or a PSF that is too narrow. Raise the SNR or check the PSF.
B5. (a) H(0.1) = e^{-0.790} = 0.454, H(0.2) = e^{-3.16} = 0.0425, H(0.25) = e^{-4.93} = 0.0072. (b) Wiener gains 0.454/(0.206 + 0.01) = 2.10, 0.0425/(0.0018 + 0.01) = 3.60, 0.0072/(0.00005 + 0.01) = 0.72. Effective transfer WH: 0.954, 0.153, 0.005. (c) The restoration passes almost everything up to f \approx 0.15 (WH = 0.74 there) and some detail up to about 0.2; the optics alone fall below 0.5 already at f \approx 0.1. Beyond about 0.2 the noise wins and nothing is restored.
B6. Energy penalty: the gain at f = 0 is 1/(1 + \lambda) = 0.909, so the mean becomes 0.909 \times 0.1575 = 0.143. (The demo’s preset “too much regularisation” shows 0.1432.) Roughness penalty: gain 1 at f = 0, mean kept at 0.1575 (apart from the small effect of noise on the mean of the data).
Math questions
B7. (math) Derive the Wiener filter: with G = HF + N, F and N independent with zero mean and powers S_f, S_n, minimise E|F - WG|^2 over the complex number W. Show that the remaining error is S_f S_n/(|H|^2 S_f + S_n).
B8. (math) Derive the normal equations of \min_{\mathbf x}\|A\mathbf x - \mathbf g\|^2 + \lambda\|D\mathbf x\|^2. For a circular A and D, show that the solution in Fourier space is \hat X = H^* G/(|H|^2 + \lambda|\hat D|^2), and that it equals the Wiener filter when \lambda|\hat D|^2 = S_n/S_f.
B9. (math) Show that the roughness penalty with the 5-point Laplacian L keeps the mean of the image, while the energy penalty multiplies it by 1/(1 + \lambda) (assume the PSF sums to 1).
B7. E|F - WG|^2 = E|(1 - WH)F - WN|^2 = |1 - WH|^2 S_f + |W|^2 S_n, since the cross terms vanish (F, N independent, zero mean). Treat W and W^* as independent variables; the derivative with respect to W^* is -(1 - WH)H^* S_f + W S_n = 0, so W(|H|^2 S_f + S_n) = H^* S_f and W = H^* S_f/(|H|^2 S_f + S_n). Then 1 - WH = S_n/(|H|^2 S_f + S_n) and the error is \frac{S_n^2 S_f + |H|^2 S_f^2 S_n}{(|H|^2 S_f + S_n)^2} = \frac{S_f S_n}{|H|^2 S_f + S_n}.
B8. The objective J(\mathbf x) = (A\mathbf x - \mathbf g)^\top(A\mathbf x - \mathbf g) + \lambda\mathbf x^\top D^\top D\mathbf x has gradient 2A^\top(A\mathbf x - \mathbf g) + 2\lambda D^\top D\mathbf x; zero gives (A^\top A + \lambda D^\top D)\mathbf x = A^\top\mathbf g. A circular matrix is diagonalised by the DFT: A = F^{-1}\operatorname{diag}(H)F, A^\top = F^{-1}\operatorname{diag}(H^*)F (real A), and likewise for D. In Fourier space: (|H|^2 + \lambda|\hat D|^2)\hat X = H^* G at every frequency. Comparing with W = H^*/(|H|^2 + S_n/S_f) gives the condition \lambda|\hat D|^2 = S_n/S_f.
B9. The mean is the f = 0 coefficient divided by the number of pixels. At f = 0, H(0) = \sum h = 1. The Laplacian’s symbol is \hat L(f) = 4 - 2\cos 2\pi f_x - 2\cos 2\pi f_y, which is 0 at f = 0 (the Laplacian of a constant is 0). So the roughness gain at f = 0 is 1/(1 + 0) = 1. The energy penalty has \hat D = 1: gain 1/(1 + \lambda).
Explore
The demo blurs a test object with a PSF, adds noise (fixed seed) and restores it. Four images: truth, observed, restored, and the error restored − truth (blue negative, red positive). The upper plot shows, against frequency: the optics |H| (blue), the filter gain |W| (red), the effective transfer |WH| (green) and the noise floor (dashed), below which the data at that frequency are mostly noise. For motion blur the plot is a cut along x instead of a ring average. The lower plot shows the error (RMSE against the truth, image values 0..1) for 30 values of the method’s parameter, computed in the background: red line = the current value, green dot = the best. PSF used × makes the restoration PSF wider or narrower than the true one; borders chooses periodic or padded-and-tapered; realistic blur lets the scene continue past the border and blurs it on a finer grid.
E1. Preset 0.1% noise: inverse explodes. What are the RMSE and the share of negative pixels? Look at the gain plot: from which frequency is the red curve far above 1, and where does the blue curve cross the noise floor?
E2. Preset Wiener at the right SNR. Which SNR is best on the sweep, and with which RMSE? Compare it with the RMSE of the observed image (in brackets in the readout) and with the true SNR in the readout. Why is the best flat SNR so different from the true SNR?
E3. Preset Wiener with the wrong SNR (2000). Describe the image and give the RMSE and the share of negative pixels. Where on the gain plot do you see the problem?
E4. Preset 1/f² beats flat (Siemens star, motion blur of 9 pixels). Read the best RMSE for the 1/f^2 model from the sweep; then switch the signal model to flat and read its best RMSE. Which is better, and why does the motion blur make the difference large?
E5. Preset Tikhonov = Wiener with a flat spectrum. Compare the RMSE with E2. Then preset too much regularisation: blur: give the RMSE and the means of truth and restored image, and explain the ratio. Finally roughness penalty: RMSE and means.
E6. Preset wrong PSF width (beads, PSF used ×1.3). Describe what you see around the beads. Set PSF used to 1.00 and to 0.80 and compare the RMSE.
E7. Preset periodic borders ring. Where do the artefacts start, and what is the RMSE? Switch borders to padded + tapered.
E1. RMSE about 1.0e6 and 50.5 % negative pixels: the image is pure amplified noise. With σ = 2 px the red curve (1/|H| for this method) passes 10 near f = 0.17 and leaves the plot (10⁴) near f = 0.34; the blue |H| drops below the noise floor at about f = 0.23 (where 1/|H| \approx 65). Beyond that every frequency is mostly noise, amplified 65 times and more.
E2. The best SNR on the sweep is 45.2 with RMSE 0.0244. The observed image has RMSE 0.0318: the restoration reduces the error by about a quarter. The true SNR in the readout is 537. One flat SNR must serve all frequencies: at low frequencies the real SNR is far higher (the image’s power sits there), at high ones far lower. The best compromise is the SNR around the frequencies where the filter decides, where |H| is about the noise floor, not the image-wide average.
E3. Fine bright and dark speckles everywhere; RMSE 0.0641 (worse than the observed 0.0318) and 14.4 % negative pixels. On the gain plot the red curve peaks near f = 0.22 with a gain of about 22, beyond the frequency (f \approx 0.16) where the blue curve fell below the noise floor: noise is amplified.
E4. 1/f^2: best RMSE 0.0714 (at SNR 304); flat: best 0.0817 (at SNR 161). The 1/f^2 model is better. The motion blur has near-zeros at many frequencies along x (every 1/9 cycle per pixel), also at high frequencies where a flat model with a high SNR would amplify noise. The 1/f^2 model trusts low frequencies more and high frequencies less, which matches both the image and the noise.
E5. λ = 1/45 gives RMSE 0.0244, exactly the Wiener result of E2: same gain at every frequency. Too much regularisation (energy, λ = 0.1): RMSE 0.0341, blurred, worse than the observed image; mean of the truth 0.1575, restored 0.1432: the ratio 0.909 is 1/(1 + \lambda), the energy penalty’s gain at f = 0. Roughness penalty (λ = 0.033): RMSE 0.0237, the best of the three linear methods here, and the mean is kept (0.1575).
E6. Dark rims around the bright beads, below the background level, and ripples: the restoration subtracts more blur than there was. RMSE 0.0465 at ×1.3, 0.0412 at ×1.00, 0.0445 at ×0.80. Too narrow is milder: it leaves blur (the beads stay wide) but makes no rims.
E7. Stripes start at the left and right borders and run into the image; RMSE 1.30 (the image is ruined). The step edge runs off the left and right borders and the blur brought light in from outside the field, neither of which the periodic model knows. Padded + tapered: RMSE 0.0497 and the stripes are gone.
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/deconv/exercise.rs.
C1. The gain of each method at a frequency where the OTF is H (real, for a symmetric PSF): truncated_gain(h, eps) (1/H where |H| > \varepsilon, else 0), wiener_gain(h, snr) (H/(H^2 + 1/\mathrm{SNR})) and tikhonov_gain(h, lambda, d2) (H/(H^2 + \lambda\, d^2), with d^2 = 1 for the energy penalty). The checks are your tables of B1, B2 and B5(b), and the gains of B3 (sum part s = 1, difference part s = 0.5).
C2. gaussian_otf(sigma, w, h): the OTF H = e^{-2\pi^2\sigma^2(f_x^2 + f_y^2)} of a Gaussian PSF for a w \times h image, at every frequency, in the layout of the 2D Fourier transform (frequency 0 at pixel (0, 0), the negative frequencies in the upper half). The check is B5(a). laplacian_symbol(fx, fy): what the Laplacian does to the wave of frequency (f_x, f_y), \hat L = 4 - 2\cos 2\pi f_x - 2\cos 2\pi f_y; 0 at frequency 0 (B9).
C3. apply_gain(g, gain): filter in frequency space.
G = Fourier transform of g
for every frequency k: X(k) = gain(k) · G(k)
x = real part of the inverse Fourier transform of X
C4. restore(g, otf, method): every method of the lecture is C3 with its own gain:
for every frequency (fx, fy), with H = otf(fx, fy):
inverse: gain = 1/H (0 where H = 0)
truncated inverse: gain = 1/H if |H| > ε, else 0
Wiener: gain = H / (H² + 1/SNR)
Tikhonov, energy: gain = H / (H² + λ)
Tikhonov, roughness: gain = H / (H² + λ · L(fx, fy)²)
restored = apply_gain(g, gain)
The checks: B6 (the energy penalty divides the mean by 1 + \lambda, the roughness penalty keeps it), Tikhonov with the energy penalty and \lambda = 1/45 gives exactly Wiener with SNR 45, and the inverse restores a noise-free image.
Then cargo run -p deconvolution -- deconv and open the pictures in out/deconv/. The program blurs the micrograph with a Gaussian PSF (\sigma = 2 pixels), adds noise of 0.01 (1 % of white), and restores it with every method and several settings. It prints the RMSE against the object, the mean and the share of negative pixels.
C5. Which method and setting gives the smallest error, and how much better is it than the observed image? Find two lines of the table that are exactly equal, and explain why.
C6. The program prints the image’s own SNR, the mean of the squared object over \sigma_n^2. Compare it with the best Wiener SNR. Then explain the column “mean” for Wiener with SNR 3 and for the roughness penalty.
C7. Compare truncated.png (\varepsilon = 0.15), wiener-snr300.png, wiener-snr10000.png and tikhonov-roughness.png (\lambda = 0.001) with observed.png. Name the artefact in each, and say which line of the gain curves it comes from.
The model solution is deconvolution/src/deconv/solution.rs in the download.
C5. The observed image has RMSE 0.1013. The best are Wiener with SNR 300 (0.0800) and the roughness penalty with \lambda = 0.001 (0.0802), about 20 % better; the truncated inverse reaches 0.0831 at \varepsilon = 0.05. The inverse filter gives 1.7 \cdot 10^{13} with half the pixels negative. Tikhonov with the energy penalty and \lambda = 0.1 gives exactly the line of Wiener with SNR 10 (RMSE 0.1140, mean 0.6002), and \lambda = 0.01 and 0.001 those of SNR 100 and 1000: with \hat D = 1 the two gains are the same formula, with \lambda = 1/\mathrm{SNR}.
C6. The image’s own SNR is 4891, but the best flat SNR is about 300: one SNR for all frequencies is a compromise, and it must suit the high frequencies, where the signal of a natural image is much weaker than its average (the slide “What Wiener needs to know”). At SNR 3 the mean drops from 0.6604 to 0.4952, the factor 1/(1 + 1/3) = 0.75 of the gain at frequency 0 (B6); the roughness penalty keeps 0.6603 for every \lambda, because \hat L(0) = 0.
C7. Truncated: ripples run parallel to the outline of the nucleus and around the dark granules (ringing from the sharp cut of the gain). Wiener SNR 300 and roughness \lambda = 0.001 look alike: sharper than the observed image, with a little grain; the outline of the nucleus is a dark line again. Wiener SNR 10000 is covered in a dense pattern of dots and short worms of the same size (RMSE 0.1554, worse than the observed image): the gain still rises where |H| is small, so the noise at those frequencies is amplified. The inverse is pure black-and-white noise.
Where this leads
Every method today had one number to choose: ε, the SNR, or λ. The next unit asks the general question: what is it we add to the data (the penalty, or prior knowledge), how much (choosing λ), and why stopping an iteration early does the same job. Unit 3 then replaces the linear filters by Richardson–Lucy, which knows that photons are counted and never negative.