Skip to content

Total variation restoration

Fact Value
Also called TV deconvolution, Total variation deblurring, ROF deconvolution
Status Implemented and tested
Family Restoration and reconstruction
Engine Science engine
Type Nonlinear, iterative, total variation regularized deconvolution
Output Image
Operator shown as iterative (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/restoration.py, tv_deblur
Operator code engine/src/pixelnexus_engine/filters/restoration.py, tv_representation
Packages SciPy
Channels RGB, Brightness only

Overview

Total variation restoration undoes a known blur. It looks for the sharp image whose blurred copy matches the photo. Among all such images, it prefers the one with the least total variation, the sum of the gradient length. That keeps flat areas flat and edges sharp, so noise does not grow the way it does with plain deconvolution. The filter minimizes this energy with the primal-dual algorithm of Chambolle and Pock. It runs in the PixelNexus engine on your computer.

Four panels: blurred disks with fine grain; total variation with lambda 0.3, sharper disks with some grain left; lambda 3, sharp disks on a clean flat ground; and lambda 30, flat disks whose smallest one has faded. Four panels: blurred disks with fine grain; total variation with lambda 0.3, sharper disks with some grain left; lambda 3, sharp disks on a clean flat ground; and lambda 30, flat disks whose smallest one has faded.
Disks blurred with a Gaussian of sigma 2 and Gaussian noise of sigma 5, and total variation restoration with lambda 0.3, 3 and 30, each with 100 iterations.

How it works

  1. Extend each channel with the border mode, as the frequency filters do.
  2. Start with the blurred photo as the estimate, and a dual field of zero vectors.
  3. Move the dual field along the gradient of the estimate, and pull each vector back to length lambda at most.
  4. Move the estimate along the divergence of the dual field, then solve the data term exactly with the FFT.
  5. Repeat, then crop the estimate to the image.

Mathematical foundation

\[ \hat{f} = \arg\min_f\ \tfrac{1}{2} \lVert h * f - g \rVert^2 + \lambda \sum_{x} \lvert \nabla f(x) \rvert, \qquad p \leftarrow \frac{p + \sigma \nabla \bar{f}}{\max(1,\, \lvert p + \sigma \nabla \bar{f} \rvert / \lambda)}, \quad f' = \mathcal{F}^{-1}\!\left(\frac{\mathcal{F}(f + \tau \operatorname{div} p) + \tau \overline{H}\, \mathcal{F} g}{1 + \tau \lvert H \rvert^2}\right), \quad \bar{f} = 2 f' - f, \qquad \lambda = 3,\ K = 100 \]

The formula shows the default parameter values.

where

  • \(g\): The blurred image extended with the border mode
  • \(f\): The estimate of the sharp image
  • \(h, H\): The PSF and its Fourier transform
  • \(p\): The dual field, one 2-D vector per pixel
  • \(\lambda\): The weight of the total variation term
  • \(\tau, \sigma\): The primal and dual step sizes, both 1 / sqrt(8)

The filter looks for the image f whose blurred copy h * f is close to the photo g. Many images fit a blurred photo, and most are full of noise. The total variation term, the sum of the gradient length over all pixels, picks the one with the least jumping around. Rudin, Osher and Fatemi used this term to remove noise. It keeps sharp edges, since one edge costs the same as a slow ramp of the same height. The sum has no gradient where the image is flat, so the filter uses the primal-dual algorithm of Chambolle and Pock. A dual field p, one 2-D vector per pixel, lives in the disk of radius lambda. Each step moves p along the gradient of the extrapolated image f bar and pulls it back into the disk. Then f moves along the divergence of p, and the data term is solved exactly with the FFT. H is the transform of the PSF. The steps tau and sigma are both 1 / sqrt(8), since the squared norm of the gradient is at most 8. Differences wrap around at the edges of the extended image. The first estimate is the photo, and p starts at 0. The PSF is the same as for Richardson-Lucy.

Deconvolution is unstable. The blur removes fine detail almost completely, so many sharp images fit the same photo, and dividing by the blur in the frequency domain blows up the noise. A penalty picks one answer. Rudin, Osher and Fatemi chose total variation to remove noise, because it favors flat areas with sharp steps. A step and a slow ramp of the same height cost the same, so edges are not smeared. Total variation has no gradient where the image is flat, so plain gradient descent does not work. Chambolle and Pock wrote the problem as a saddle point. The dual field p, one vector per pixel, stays in a disk of radius lambda, and each step updates p and f in turn. Here the data term has an exact solution in the Fourier domain, so every step is cheap.

Kernel or operator

The Inspector asks the engine for the stages with the current settings. They are the PSF, the energy with the value of lambda, the dual step and the primal step.

Algorithm

  1. Extend the image with the border mode by half its size, at most 256 pixels, on each side.
  2. Start with f equal to the extended image g and p equal to 0.
  3. Move p along the gradient of f bar and pull every vector back into the disk of radius lambda.
  4. Move f along the divergence of p and solve the data term exactly with the FFT.
  5. Set f bar to 2 f' - f, and repeat for the chosen number of iterations.
  6. Crop the last estimate to the image.

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.
Lambda lambda 3 0 to 50 The weight of the total variation term, in pixel values. 0 leaves only the data term.
Iterations iterations 100 1 to 1000 How many primal-dual steps to run.
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

tv_deblur in engine/src/pixelnexus_engine/filters/restoration.py runs tv_deblur_plane on each channel the scope names. The PSF comes from engine/src/pixelnexus_engine/psf.py, which builds the same Gaussian, motion and defocus kernels as the browser deconvolution filters. tv_deconvolve holds the iteration, with gradient and divergence written with numpy.roll. There is no live preview. The result arrives when you click Apply.

Worked example

Image Blurred Lambda 0.3 Lambda 1 Lambda 3 Lambda 10 Lambda 30 300 iterations
Disks 24.81 28.63 29.82 30.92 31.10 30.31 33.03
Siemens star 15.85 19.13 19.35 19.42 18.74 16.82 20.42

The PSNR in dB against the sharp image. Both images are 160 by 160 pixels, blurred with the Gaussian PSF of sigma 2 and the reflect border, with Gaussian noise of sigma 5 from NumPy's default_rng with seed 7. The lambda columns use 100 iterations. The last column uses the default lambda of 3 and 300 iterations, which still gains, so 100 iterations stop short of the minimum. The disks are flat shapes, which total variation suits, so they gain more than the fine spokes of the star.

Complexity

  • Time: \(O(K N log N)\)
  • Memory: \(O(N)\)

N is the number of pixels of the extended image and K the number of iterations. Each iteration takes two FFTs.

Artifacts and limitations

Limitations

  • Fine texture and gentle shading turn into flat patches with large lambda (staircasing).
  • The PSF must be known.
  • Slower than Wiener deconvolution.

Artifacts

  • Flat patches with sharp steps, like a cartoon, where the image has smooth shading.
  • Faint ringing next to strong edges with small lambda.

Tests

The engine PSFs are compared with the fixture references that the browser tests also use, and the extended grid with the grid of the browser frequency filters. The divergence must be the negative adjoint of the gradient. On a small image, 3000 iterations must reach the minimum energy that L-BFGS finds on a slightly smoothed version of the same energy, to within 0.1 percent. With lambda 0, each step must be the exact proximal step of the data term. A blurred noisy image must gain at least 2.5 dB. The engine API tests run jobs and check the stages of the representation.

References

  • Rudin, L. I., Osher, S., and Fatemi, E. Nonlinear total variation based noise removal algorithms. Physica D 60(1 to 4), 259 to 268, 1992. DOI 10.1016/0167-2789(92)90242-F.
  • Chambolle, A., and Pock, T. A first-order primal-dual algorithm for convex problems with applications to imaging. Journal of Mathematical Imaging and Vision 40(1), 120 to 145, 2011. DOI 10.1007/s10851-010-0251-1.
  • Chambolle, A. An algorithm for total variation minimization and applications. Journal of Mathematical Imaging and Vision 20(1 to 2), 89 to 97, 2004. DOI 10.1023/B:JMIV.0000011325.36760.1e.