Skip to content

Constrained least squares

Fact Value
Also called CLS filter, Constrained least squares filtering, CLS restoration
Status Implemented and tested
Family Restoration and reconstruction
Engine Browser
Type Nonlinear, frequency, constrained least squares
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/restoration/deconvolve.ts, clsFilter
Operator code web/src/core/filters/restoration/deconvolve.ts, clsGamma
Channels RGB, Brightness only

Overview

Constrained least squares undoes a known blur with a smoothness penalty, like Tikhonov regularization with the Laplacian. Instead of a weight chosen by hand, it takes the noise level. It sets the weight so the result, blurred again, differs from the image by as much as the noise does.

Three panels: disks with defocus blur and noise, the constrained least squares result with sharper disks, and the clean disks. Three panels: disks with defocus blur and noise, the constrained least squares result with sharper disks, and the clean disks.
Constrained least squares with sigma 4 chooses its own weight from the noise level.

How it works

  1. Take the spectrum G of the blurred image and the transform H of the blur.
  2. Try weights gamma by bisection until the residual of the result matches the noise.
  3. Multiply each frequency by conj(H) / (|H|^2 + gamma |P|^2), with P the Laplacian.
  4. Transform back.

Mathematical foundation

\[ \hat{F}(f) = \frac{\overline{H(f)}}{|H(f)|^2 + \gamma |P(f)|^2}\, G(f),\quad \frac{1}{N} \| g - h * \hat{f} \|^2 = \sigma^2,\quad \sigma = 5 \]

The formula shows the default parameter values.

where

  • \(G, H, F\): The spectra of the blurred image, the PSF and the estimate
  • \(P\): The transform of the Laplacian kernel
  • \(\gamma\): The weight found from the noise level
  • \(N\): The number of FFT samples
  • \(\sigma\): The noise standard deviation

Constrained least squares follows Gonzalez and Woods. It minimizes the roughness |p * f|^2. The constraint is that the blurred estimate differs from the image by as much as the noise. P is the transform of the 4-neighbor Laplacian kernel p. The solution has the form of the Tikhonov filter with weight gamma. Gamma is chosen so that the mean square of the residual g - h * f, per sample of the FFT grid, equals sigma^2. Gonzalez and Woods adjust gamma step by step. This filter finds it by 60 bisection steps on log10 gamma from -10 to 4, its own search. The residual grows with gamma, so the bisection always converges. If no gamma in the range fits, the nearest end is used. The PSF is the kernel of the Gaussian filter cut at 4 sigma, of the motion blur filter, or of the disk averaging filter.

Gonzalez and Woods minimize the roughness |p * f|^2, with p the Laplacian, under the constraint |g - h * f|^2 = |n|^2, the size of the noise. The solution is the Tikhonov form with a weight gamma, the Lagrange multiplier of the constraint. The residual grows with gamma. So a bisection on log10 gamma finds the gamma whose residual, as a mean square per FFT sample, equals sigma^2. Gonzalez and Woods adjust gamma step by step instead. The bisection is this filter's own search.

Kernel or operator

The Inspector lists the stages, with the PSF and the noise level.

Algorithm

  1. Extend the image with the border mode by half its size, at most 256 pixels, on each side.
  2. Take the 2D FFT of the image and of the PSF placed at the grid origin.
  3. Find gamma by bisection so the residual matches sigma^2.
  4. Multiply each frequency by conj(H) / (|H|^2 + gamma |P|^2).
  5. 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.
Noise sigma noiseSigma 5 0.1 to 50 levels The standard deviation sigma of the noise, in 8-bit levels.
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

clsFilter in web/src/core/filters/restoration/deconvolve.ts finds gamma with clsGamma and multiplies the spectrum by the gain of regularizedGain with the Laplacian. 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

Image PSNR against the clean disks (dB)
Blur and noise 25.75
Constrained least squares, sigma 4 27.91
Tikhonov, gradient, lambda 0.01 27.52

For the figure, with defocus of radius 3 and noise of sigma 4, the bisection finds gamma = 0.100 on the red channel. The residual of the result then has a mean square of 16, the noise power.

Complexity

  • Time: \(O(N log N + 60 N)\)
  • Memory: \(O(N)\)

N is the number of pixels of the extended image. Each bisection step sums over the spectrum once.

Artifacts and limitations

Limitations

  • Needs the noise level and the blur.
  • The residual is measured on the extended image, so the border mode affects gamma a little.

Artifacts

  • Ringing near strong edges and the image border.

Tests

The reference runs the same bisection with NumPy on a plane that is blurred and rounded first. The cases cover two noise levels and two border modes.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5, image restoration and reconstruction.