Skip to content

Anisotropic diffusion

Fact Value
Also called Perona-Malik diffusion
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, iterative, nonlinear diffusion
Output Image
Operator shown as iterative (exact)
Support radius iterations
Code web/src/core/filters/nonlinear/diffusion.ts, anisotropicDiffusionFilter
Operator code web/src/core/filters/nonlinear/diffusion.ts, conduction
Channels RGB, Brightness only

Overview

Anisotropic diffusion smooths an image by letting values flow between neighbors, like heat. Unlike heat, the flow is blocked where the difference is large, at edges. After a number of steps, noise in flat areas has evened out while edges are still sharp. Perona and Malik proposed it in 1990, and it is also called Perona-Malik diffusion.

Four panels in two rows: disks with Gaussian noise, the result after 5 iterations with less noise, after 20 iterations with flat areas and sharp edges, and a Gaussian blur with sigma 2 that blurs the edges. Four panels in two rows: disks with Gaussian noise, the result after 5 iterations with less noise, after 20 iterations with flat areas and sharp edges, and a Gaussian blur with sigma 2 that blurs the edges.
More iterations remove more noise. The disk edges stay sharp, unlike with a Gaussian blur.

How it works

  1. Find the difference between each pixel and its four neighbors.
  2. Turn each difference into a conduction: near 1 for small differences, near 0 for large ones.
  3. Move the pixel toward its neighbors by the step size times the differences times their conductions.
  4. Repeat for the chosen number of iterations.

Mathematical foundation

\[ I^{t+1} = I^{t} + \lambda \sum_{n \in \{N,S,E,W\}} g\bigl(|\nabla_n I^{t}|\bigr)\, \nabla_n I^{t}, \qquad g(d) = e^{-(d/K)^2} \;\text{ or }\; \frac{1}{1 + (d/K)^2} \]

The formula shows the default parameter values.

where

  • \(\nabla_n I\): The difference to the neighbor in direction n
  • \(g\): The conduction function
  • \(K\): The edge threshold
  • \(\lambda\): The step size
  • \(t\): The step number

nabla_N I is the difference to the north neighbor, I(x, y - 1) - I(x, y), and the same for south, east and west. Each step moves a value toward its neighbors by lambda times the differences, weighted by the conduction g. Small differences have g near 1 and are smoothed. Large differences, edges, have g near 0 and are kept.

With a constant conduction, the update is the heat equation, and many steps give a Gaussian blur. Perona and Malik let the conduction fall with the gradient, with one of two functions of the difference d and the edge threshold K. Where |d| is much larger than K, the flow stops, so edges are not smoothed. The explicit scheme with four neighbors is stable for step sizes up to 0.25. The reflect border is the zero-flux boundary of the paper: no value flows in or out of the image.

Kernel or operator

The Inspector shows the update step with the current parameters and the number of iterations. The worked example below runs one step for one pixel.

Algorithm

  1. Extend the image by one pixel per iteration with the border mode.
  2. For each pixel, compute the differences to its four neighbors.
  3. Add lambda times the sum of each difference times its conduction.
  4. Repeat for the given number of iterations, then cut the extension off.

Parameters

Parameter Default Values Meaning
Iterations iterations 15 1 to 100 How many update steps run.
Edge threshold K kappa 20 1 to 100 The difference, in 8-bit levels, at which the conduction falls off.
Step size lambda 0.2 0.01 to 0.25 How far each step moves a value toward its neighbors. At most 0.25 keeps the scheme stable.
Conduction conduction Exponential, exp(-(d/K)^2) Exponential, exp(-(d/K)^2), Rational, 1 / (1 + (d/K)^2) The function g that turns a difference d into a conduction. Both come from Perona and Malik.
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.

Implementation

anisotropicDiffusionFilter in web/src/core/filters/nonlinear/diffusion.ts extends the plane once by one pixel per iteration with the border mode, runs all steps in float64, and cuts the extension off. Information moves one pixel per step, so the extension holds every value a pixel can reach. For the reflect modes and wrap, this equals applying the border rule at every step. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts. It handles the border mode, the channel scope, alpha and the rounding to 8 bits. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.

Worked example

\[ W = \begin{bmatrix} 60 & 64 & 60 \\ 100 & 70 & 200 \\ 60 & 78 & 60 \end{bmatrix} \]

One step for the center pixel 70, with K = 20, lambda = 0.2 and the exponential conduction:

Neighbor Difference d g(d) g(d) d
North -6 0.9139 -5.484
South 8 0.8521 6.817
East 130 0 0
West 30 0.1054 3.162

The sum is 4.496, so the new value is 70 + 0.2 x 4.496 = 70.899. The east neighbor 200 is across an edge, so its conduction is almost 0 and it does not pull the pixel. The small differences to the north and south flow freely.

Complexity

  • Time: \(O(N\,T)\)
  • Memory: \(O(N)\)

N is the number of pixels and T the number of iterations.

Artifacts and limitations

Limitations

  • Noise spikes larger than K are kept as if they were edges.
  • Many iterations are slow and flatten fine texture into patches.

Artifacts

  • Flat patches with sharp borders, a staircase look, after many iterations.

Tests

The reference runs the same steps with NumPy on the extended plane. Before it is used, the script checks that extending once equals applying the mirror rule at every step. The test covers both conduction functions for all five border modes.

References

  • Perona, P. and Malik, J. Scale-space and edge detection using anisotropic diffusion. IEEE Transactions on Pattern Analysis and Machine Intelligence 12(7), 629 to 639, 1990. DOI 10.1109/34.56205.