Exercise: running the model backwards
Before this unit: Resolution, sampling and measuring the PSF (previous unit); Day 1: Images, filters and frequencies.
See also: Day 3: Bit depth and noise; Day 2: Edges, blobs and ridges.
Goal
By the end of this exercise you should be able to:
- compute the gains 1/H of the inverse filter and what they do to noise,
- explain why the inverse filter is exact without noise and explodes with it,
- construct two different objects with the same image (invisible objects),
- say why deblurring is ill-posed, in words,
- (math) find the eigenvalues of a circulant blur and the condition number of a blur matrix.
This is a short exercise (about 30 minutes): the pen-and-paper part, then the demo. Questions marked (math) are for those who want the linear-algebra view.
Background
| Word | Meaning |
|---|---|
| Inverse problem | from the data (image) back to the cause (object), given the forward model |
| Inverse filter | \hat F = G/H: divide each frequency by the OTF |
| Gain | 1/H: the factor by which the inverse multiplies a frequency (and its noise) |
| Invisible object | a pattern with H = 0 at all its frequencies; its image is zero |
| Well-posed | a solution exists, is unique, and depends stably on the data |
| Circulant matrix | each row is the previous one shifted by one place; blur with periodic borders |
| Condition number | \kappa = \max\lvert H\rvert / \min\lvert H\rvert for a circulant blur |
By hand
B1. The inverse filter divides each frequency by H. (a) Give the gain for H = 1, 0.5, 0.1, 0.01. (b) The image has noise of 1 % at each of these frequencies. How large is the noise in the restored image at each? (c) Which frequency’s noise dominates the restored image?
B2. A periodic row of 4 pixels is blurred with (1, 2, 1)/4 (each pixel: ¼ left + ½ itself + ¼ right, wrapping around). (a) Blur the object \mathbf f = (3, 1, 2, 2). (b) Blur \mathbf f' = \mathbf f + (1, -1, 1, -1) = (4, 0, 3, 1). (c) What do you conclude? Construct a third object, different from both, with the same image.
B3. Sketch a pattern that an NA 1.4 objective (λ = 520 nm) cannot transmit at all. Would a restoration method that “shows” this pattern in a real image be trustworthy?
B4. Two-pixel blurs. Any pair of pixels can be written as a sum part (s, s) plus a difference part (d, -d). (a) Show that A = \begin{bmatrix}0.75 & 0.25\\0.25 & 0.75\end{bmatrix} multiplies the sum part by 1 and the difference part by 0.5. What does B = \begin{bmatrix}0.55 & 0.45\\0.45 & 0.55\end{bmatrix} do? (b) The data have an error of \pm0.01 in the difference part, (0.01, -0.01). How large is the error after inverting A, and after inverting B?
B5. Doubling the exposure time halves nothing in the inverse filter’s trouble. Explain, with the gains of B1 and the \sqrt N rule of shot noise.
B6. A motion-blurred image (motion L along x) is restored with the inverse filter. What happens at the frequencies k = 1/L, 2/L, \dots, and what will the restored image look like near them?
B1. (a) 1, 2, 10, 100. (b) 1 %, 2 %, 10 %, 100 %. (c) The one with the smallest H (here 0.01): its noise is as large as the signal. In a real blur the OTF keeps falling towards the finest frequencies, so their noise dominates: a fine-grained pattern over the whole image.
B2. (a) g_1 = \tfrac14\cdot2 + \tfrac12\cdot3 + \tfrac14\cdot1 = 2.25, g_2 = \tfrac14\cdot3 + \tfrac12\cdot1 + \tfrac14\cdot2 = 1.75, g_3 = 1.75, g_4 = 2.25: \mathbf g = (2.25, 1.75, 1.75, 2.25). (b) (4, 0, 3, 1): \tfrac14\cdot1 + \tfrac12\cdot4 + 0 = 2.25, 1 + 0 + 0.75 = 1.75, 0 + 1.5 + 0.25 = 1.75, 0.75 + 0.5 + 1 = 2.25: the same image. (c) The fastest wave (1, -1, 1, -1) is blurred to zero (each pixel: \tfrac14(-1) + \tfrac12(1) + \tfrac14(-1) = 0): it is invisible. Any \mathbf f + c(1, -1, 1, -1) has the same image, e.g. (3.5, 0.5, 2.5, 1.5) for c = 0.5. The image cannot tell them apart.
B3. Any pattern made only of frequencies above 2\mathrm{NA}/\lambda = 5.4 cycles/µm, e.g. a grating with a period of 150 nm (anything below 186 nm). Its image is exactly zero. A method that shows such a pattern did not get it from the data: it was put there by the method’s assumptions (or by noise). Not trustworthy as a measurement.
B4. (a) A(s, s) = (0.75s + 0.25s, \dots) = (s, s); A(d, -d) = (0.75d - 0.25d, \dots) = (0.5d, -0.5d). B: sum part × (0.55 + 0.45) = 1, difference part × (0.55 - 0.45) = 0.1. (b) Inverting divides the difference part by 0.5 or 0.1: the error becomes (0.02, -0.02) for A, and (0.1, -0.1) for B. The stronger the blur mixes the two pixels, the more an error in their difference is amplified.
B5. Doubling the photons reduces relative shot noise by \sqrt2 = 1.41. The gains of the inverse filter are 10, 100, and for real blurs 10^3 to 10^9; dividing the noise by 1.41 leaves it amplified by nearly the same factors. To compensate a gain of G one would need about G^2 times more photons.
B6. There H = 0: nothing to divide by (a careful implementation sets those frequencies to zero; a naive one divides by a tiny number and explodes). Near them H is small and the gains are huge: the restored image shows strong stripes along x with periods near L, L/2, …, built from amplified noise.
Math questions
B7. (math) A periodic 4-pixel blur has the kernel (0.2, 0.6, 0.2) (left, centre, right). (a) Write the circulant matrix. (b) Show that the waves (1, 1, 1, 1), (1, 0, -1, 0), (0, 1, 0, -1) and (1, -1, 1, -1) are eigenvectors, and find the eigenvalues. (c) What is the condition number? Which wave is amplified most by the inverse, and by how much?
B8. (math) Show that every n \times n circulant matrix A with first row (a_0, a_1, \dots, a_{n-1}) has the eigenvector \mathbf w_k = (1, \omega^k, \omega^{2k}, \dots), \omega = e^{2\pi i/n}, and give its eigenvalue. Why does this make “convolution = multiplication in frequency space” a statement about diagonalisation?
B9. (math) Compute the condition numbers of the two-pixel blurs A and B of B4, and check the bound \|\delta\mathbf f\|/\|\mathbf f\| \le \kappa\,\|\delta\mathbf g\|/\|\mathbf g\| for \mathbf f = (1, 1) and a data error \delta\mathbf g = (0.01, -0.01).
B7. (a) A = \begin{bmatrix}0.6 & 0.2 & 0 & 0.2\\0.2 & 0.6 & 0.2 & 0\\0 & 0.2 & 0.6 & 0.2\\0.2 & 0 & 0.2 & 0.6\end{bmatrix}. (b) A(1,1,1,1) = (1,1,1,1): eigenvalue 1. A(1,0,-1,0) = (0.6, 0, -0.6, 0): 0.6; likewise (0,1,0,-1): 0.6. A(1,-1,1,-1) = (0.6 - 0.4, \dots) = (0.2, -0.2, 0.2, -0.2): 0.2. (In general H(k) = 0.6 + 0.4\cos(2\pi k/4) = 1, 0.6, 0.2, 0.6.) (c) \kappa = 1/0.2 = 5. The fastest wave (1,-1,1,-1), gain 5.
B8. Row j of A is the first row shifted j places: A_{jm} = a_{(m - j) \bmod n}. Then (A\mathbf w_k)_j = \sum_m a_{(m-j) \bmod n}\,\omega^{km} = \omega^{kj}\sum_{l} a_l\,\omega^{kl} (with l = m - j, using \omega^n = 1). So \mathbf w_k is an eigenvector with eigenvalue \lambda_k = \sum_l a_l\,\omega^{kl}, the DFT of the kernel (up to the sign convention of the exponent). The matrix of DFT vectors W therefore diagonalises every circulant matrix at once: A = W\,\mathrm{diag}(\lambda)\,W^{-1}. Applying A = transform, multiply each frequency by \lambda_k, transform back: the convolution theorem.
B9. A: eigenvalues 1 and 0.5, \kappa = 2. B: 1 and 0.1, \kappa = 10. With \mathbf f = (1, 1): \mathbf g = (1, 1) for both, \|\mathbf g\| = \sqrt2. \|\delta\mathbf g\| = 0.01\sqrt2, relative 0.01. \delta\mathbf f = (0.02, -0.02) for A and (0.1, -0.1) for B: relative errors 0.02 and 0.1, exactly \kappa \cdot 0.01. The bound is reached: the object lies along the largest eigenvalue and the error along the smallest.
Explore
The deconvolution day’s lab in its inverse-only mode. A 128 × 128 test object (cell phantom, Siemens star, beads, or the micrograph) is blurred with a PSF (Gaussian σ, Airy with a given NA at 520 nm and 65 nm pixels, or horizontal motion), noise is added (Gaussian σn in % of the peak, or photon counts; seeded), and the inverse filter divides by H. Panels: truth, observed, restored, error. The plot shows \lvert H\rvert (blue), the inverse gain 1/\lvert H\rvert (red) and the noise floor (dashed); the readout gives the RMSE against the truth and the largest gain.
E1. Preset no noise: inverse is perfect. What are the RMSE of the restored and of the observed image? What is the largest gain? Why does such a huge gain not matter here?
E2. Preset 0.1 % noise: inverse explodes. RMSE, fraction of negative pixels? Look at the error image: which spatial frequencies does the error consist of?
E3. Set the PSF to Gaussian σ = 0.7 px and the noise to 0. Raise the noise slowly. At which noise level does the restored image become worse than the observed one (larger RMSE)? Repeat for σ = 0.5 px and σ = 1.0 px.
E4. Back to 0.1 % noise (σ = 1.5 px). Switch the noise to photon counts and try 1000, 10 000 and 100 000 photons. How much does the RMSE improve per tenfold more photons? Compare with Q6 of the quiz.
E5. Preset motion blur: zeros (9 px along x, 1 % noise). Describe the restored image. Set the noise to 0: what happens?
E6. Preset an invisible grating (Airy PSF with NA 1.0, no noise). Tick and untick “add a grating beyond the cut-off”. What happens to the observed image, and to the RMSE of the restored one? Explain.
E1. Restored RMSE 0.0000 (PSNR 164 dB: the error is about 6 \cdot 10^{-9}, rounding only), observed 0.0245. Largest gain 1.1 \cdot 10^9. Without noise the frequencies with small H carry only the (tiny) blurred signal, which the gain restores exactly; there is no noise for the gain to multiply.
E2. RMSE 4.8 \cdot 10^4 (the observed image: 0.0245), about 50 % negative pixels. The error image is a fine checkerboard-like texture: the highest frequencies, where \lvert H\rvert is smallest and the gain largest (up to 10^9).
E3. σ = 0.7 px: the restored image is better than the observed one up to about 0.135 % noise and worse from about 0.14 % (both RMSEs about 0.011 there). σ = 0.5 px: the crossover is near 0.37 %. σ = 1.0 px: already at the smallest non-zero noise the slider allows (0.0056 %) the restored image is worse (RMSE 0.029 against 0.017): the largest gain is 4.8 \cdot 10^3. The wider the blur, the smaller the OTF at high frequencies, and the less noise the inverse filter tolerates.
E4. RMSE about 5.9 \cdot 10^5 (1000 photons), 2.3 \cdot 10^5 (10 000) and 6.4 \cdot 10^4 (100 000): 2.5 to 3.6 times better per tenfold photons, about the \sqrt{10} = 3.2 of shot noise, but still millions of times worse than the observed image. With gains up to 10^9, no realistic photon budget makes the inverse usable.
E5. RMSE 0.96 (observed 0.156): strong vertical stripes (periods near the motion length and its fractions) cover the star, about 37 % of the pixels are negative; the largest gain is 702 (the exact zeros k/L fall between the frequencies of the 128-pixel grid). With no noise the restoration is perfect (RMSE about 10^{-14}): as in E1, the gains only hurt when there is noise to amplify.
E6. The observed image does not change at all (the readout: the largest change of the observed image is about 2 \cdot 10^{-16}, rounding). The truth now contains the grating, but the restoration cannot: its RMSE grows from 0.0138 to 0.0376. The grating’s frequency is beyond the cut-off (H = 0), so it is invisible, and the inverse filter (like any method) has no data about it.
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 imageformation/src/illposed/exercise.rs.
C1. circular_blur(f, kernel): blur a periodic row, where the right end continues at the left: g_i = \sum_j k_j\, f_{i + j - r}, positions taken modulo n. The checks are B2: the three objects with the same image, and the invisible wave (1, -1, 1, -1).
C2. transfer(kernel, n): the gain H(k) of a symmetric kernel for each wave k = 0, \dots, n - 1 on a periodic row of n pixels, the eigenvalues of the circulant matrix:
for k = 0 .. n − 1:
H(k) = sum over j of kernel[j] · cos(2π k (j − r) / n) # r = the middle of the kernel
The checks: (1, 0.5, 0, 0.5) for (1, 2, 1)/4, B7 for (0.2, 0.6, 0.2), and blurring each wave multiplies it by its H(k).
C3. condition_number(kernel, n): \max_k |H(k)| / \min_k |H(k)|, infinite when some H(k) is 0. The check is B7(c).
C4. otf2(psf, w, h): the OTF of a PSF for a periodic w \times h image. Put the PSF into an image of zeros with its centre on pixel (0, 0), wrapping around the edges (the pixel to the left of the centre goes to the right edge), then take the 2D Fourier transform. The checks: H is 1 at frequency 0 and real for a symmetric PSF, and a point blurred with it comes out as the PSF, not shifted.
C5. inverse_filter(g, otf): the inverse filter of the slide “Divide by H”:
G = Fourier transform of g
for every frequency:
if |H| = 0: F̂ = 0 # nothing to divide by
else: F̂ = G / H
f̂ = real part of the inverse Fourier transform of F̂
The checks: without noise it restores the object exactly, and an object with an invisible checkerboard added gives the same image and the same restoration.
Then cargo run -p imageformation -- illposed and open the pictures in out/illposed/. The program blurs the micrograph with Gaussian PSFs of \sigma = 0.5 and 1 pixel, adds noise of standard deviation 0 to 0.01 (grey values, 1 = white), and restores with your inverse filter. It prints the largest gain 1/|H| and the RMSE of the observed and the restored image against the object. The pictures are for \sigma = 1 pixel.
C6. For \sigma = 1 pixel, look at the rows with noise 0.0001 and 0.001. Predict the RMSE of the restoration with noise 0.01 before you look at that row. Why is the prediction so simple? Why does the blur with \sigma = 0.5 pixel never get into trouble?
C7. Open restored-noise-0.001.png and error-noise-0.001.png. What does the error look like, and which frequencies does it consist of?
C8. The last part adds a checkerboard of ±0.1 to the micrograph and blurs both with the kernel (1, 2, 1)/4 along x and y. Compare object-with-checkerboard.png with image-with-checkerboard.png, and read the printed difference between the two images.
The model solution is imageformation/src/illposed/solution.rs in the download.
C6. RMSE 0.0550 with noise 0.0001 and 0.5498 with 0.001: ten times the noise, ten times the error. So 0.01 gives about 5.5 (printed: 5.4981), 80 times worse than the observed image (0.0708). The inverse filter is linear: the restored image is the object plus the noise multiplied by the gains 1/H, so the error is proportional to the noise. Without noise the restoration is exact (RMSE 0.0000), although the largest gain is 5013. With \sigma = 0.5 pixel the largest gain is only 3: the noise is at most tripled, and the restoration beats the observed image at every noise level (0.0181 against 0.0295 at 0.01).
C7. The restored image is buried under coarse grain that is clipped to black and white; the nucleus can just be guessed. The error image is a fine, even checkerboard-like pattern all over the image: the highest frequencies, where H is smallest and the gain largest (up to 5013), as in E2 of the demo.
C8. The object with the checkerboard is covered with a fine grid of dots; its image looks exactly like the blurred micrograph without it. The two images differ by at most 3.6 \cdot 10^{-7}, rounding error: the checkerboard is the 2D version of the wave (1, -1, 1, -1) of B2, with H = 0. Nothing in the data shows it, so no restoration can bring it back.
Where this leads
Today’s diagnosis: the forward model alone does not determine the object. The deconvolution day shows how to add what we know about the object and the noise, how to choose how much to add (regularisation), and what may be measured in a restored image afterwards (denoising and measurement). Tomography meets the same diagnosis with line integrals instead of a PSF (reconstruction).