Inverse filtering¶
| Fact | Value |
|---|---|
| Also called | Inverse filter, Direct inverse filtering, Pseudo-inverse filter |
| Status | Implemented and tested |
| Family | Restoration and reconstruction |
| Engine | Browser |
| Type | Linear, frequency, division by the transfer function |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/restoration/deconvolve.ts, inverseFilter |
| Operator code | web/src/core/filters/restoration/deconvolve.ts, inverseGain |
| Channels | RGB, Brightness only |
Overview¶
Inverse filtering undoes a known blur by dividing the spectrum of the image by the transform of the blur. Without noise it is exact. With noise it fails, because the blur leaves some frequencies almost at 0, and dividing raises their noise hugely. The filter therefore divides only where the blur kept enough of each frequency.
How it works¶
- Take the spectrum G of the blurred image and the transform H of the blur.
- Divide G by H where |H| reaches the smallest gain and the frequency is within the cutoff.
- Set the other frequencies to 0.
- Transform back.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(G, H, F\): The spectra of the blurred image, the PSF and the estimate
- \(D_0\): The cutoff radius in cycles per pixel
- \(\epsilon\): The smallest gain that is divided
A blur multiplies the spectrum F of the image by H, the transform of the PSF h, so G = H F. Dividing by H gives F back. Where |H| is very small, the blur removed the frequency and any noise is divided by almost 0. The filter therefore divides only where |H| reaches the threshold and within the cutoff radius, as Gonzalez and Woods limit the inverse filter to a radius. H is the FFT of h placed with its center at the origin of the grid. The PSF is the kernel of the Gaussian filter cut at 4 sigma, of the motion blur filter, or of the disk averaging filter.
A blur multiplies the spectrum of the image by H, so G = H F, and F = G / H. With noise N, G = H F + N, and G / H = F + N / H. Where |H| is small, N / H is huge. Gonzalez and Woods show this and limit the inverse filter to a radius around the center. This filter also skips frequencies where |H| is below the smallest gain. The result is a pseudo-inverse.
Kernel or operator¶
The Inspector lists the stages, with the PSF and the current cutoff and smallest gain.
Algorithm¶
- Extend the image with the border mode by half its size, at most 256 pixels, on each side.
- Take the 2D FFT of the image and of the PSF placed at the grid origin.
- Divide by H where |H| reaches the threshold within the cutoff, and set the rest to 0.
- Take the inverse FFT, keep the real part and crop the extension.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Blur psf |
Gaussian | Gaussian, Motion, Defocus | The point spread function, the picture a single bright point turns into. |
Sigma sigma |
2 | 0.3 to 10 px | The standard deviation of the Gaussian blur. The PSF ends at 4 sigma. |
Motion length length |
9 | 1 to 51 px | The length of the straight motion, for motion blur. |
Motion angle angle |
0 | 0 to 180 deg | The direction of the motion, counterclockwise from the x axis, as for motion blur. |
Defocus radius radius |
3 | 0.5 to 15 px | The radius of the defocus disk. |
Cutoff cutoff |
0.71 | 0.01 to 0.71 cycles/px | Frequencies farther than this from the center of the spectrum are set to 0. The default 0.71 keeps all of them. |
Smallest gain threshold |
0.05 | 0.001 to 0.5 | Frequencies where the blur kept less than this share, |
Border mode border |
Reflect | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
inverseFilter in web/src/core/filters/restoration/deconvolve.ts multiplies the spectrum by the gain of inverseGain. As for the frequency filters, the image is extended with the border mode by half its size, at most 256 pixels, on each side. H is the FFT of the PSF placed with its center at the origin of the FFT grid. The filter needs the whole spectrum, so it always runs on the whole image, and a selection is cut out of the result.
Worked example¶
| f_x (cycles per pixel) | 0 | 0.05 | 0.1 | 0.15 | 0.2 | 0.3 |
|---|---|---|---|---|---|---|
| H | 1 | 0.8209 | 0.4540 | 0.1692 | 0.0425 | 0.0008 |
| Gain 1 / H, smallest gain 0.05 | 1 | 1.22 | 2.20 | 5.91 | 0 | 0 |
The Gaussian PSF with sigma 2 along the f_x axis. The blur keeps less and less of each frequency. Dividing by H raises 0.15 cycles per pixel by about 20 times, and so its noise too. Below the smallest gain the frequency is set to 0.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels of the extended image.
Artifacts and limitations¶
Limitations
- Noise at frequencies where |H| is small is raised hugely.
- Needs the exact blur. A wrong PSF gives ringing and false detail.
Artifacts
- Ringing near edges and the image border.
- Strong noise when the threshold is low.
Tests¶
The reference builds the same PSF kernels, places them at the origin of the grid, takes their FFT with numpy.fft and filters the plane extended with numpy.pad. Before the cases are written, the script blurs a plane with a Gaussian PSF. It checks that Wiener deconvolution brings the center of the image at least twice as close to the original. The inverse cases cover a Gaussian and a motion PSF with a cutoff and a smallest gain.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5, image restoration and reconstruction.