Edge-stopping diffusion: Perona–Malik

Image analysis

About this lecture

Last unit: heat flow is a Gaussian blur, built step by step. A blur removes noise and edges. Now we let the image decide how much heat may flow (Perona & Malik, 1990).

  • Edge-stopping: the conductivity g falls where the gradient is large; a contrast parameter K separates noise from edges.
  • Backward diffusion: edges steeper than K get sharper.
  • The prices: staircases on ramps, spikes that never go, and the cure: compute g from a slightly smoothed image.
  • When to stop: the stopping time is a regularisation parameter.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

The wish: smooth inside, not across

Make the heat step image-dependent: between two neighbours, the flow is

g(|\text{difference}|) \times \text{difference},

with a conductivity g between 0 and 1:

  • g \approx 1 where the image is flat: heat flows, noise is averaged away;
  • g \approx 0 across a strong edge: an insulator, the two sides do not mix.

Continuous form: u_t = \nabla\cdot\big(g(|\nabla u|)\,\nabla u\big). With g = 1 it is the heat equation again.

Left: the noisy test card. Middle: the conductivity g computed from the raw gradient with K = 10, speckled black and white in flat regions because the noise itself has large gradients. Right: g computed from the image smoothed with sigma 1: white (heat flows) in flat regions, black lines (insulators) along every edge.

White: heat flows. Black: insulates.

Edge-stopping functions

Two classical choices, with a contrast parameter K:

g_1(s) = e^{-(s/K)^2}, \qquad g_2(s) = \frac{1}{1 + (s/K)^2}

  • Differences much smaller than K: g \approx 1, treated as noise.
  • Differences much larger than K: g \approx 0, treated as edges.
  • g_1 stops more sharply; g_2 lets a little heat through even at strong edges.

K is a threshold in disguise, like the hysteresis thresholds of Canny: it has to sit between the typical noise difference and the typical edge height.

Conductivity against the gradient size divided by K: linear diffusion has g = 1 everywhere; g1 = exp(−(s/K)²) falls to 0.37 at s = K and almost 0 by s = 2K; g2 = 1/(1 + (s/K)²) falls to 0.5 at s = K and keeps a long tail.

Perona–Malik as pseudocode

repeat for every step:
    for every pixel p:
        change = 0
        for each of the 4 neighbours q:
            d = I(q) − I(p)                       # outside: mirror border
            change = change + g(|d|, K) · d       # g = 1 gives the heat step
        new(p) = I(p) + Δt · change
    I = new
  • One line differs from the heat step: the flow is multiplied by g.
  • With pre-smoothing, |d| inside g is taken from the blurred image; the flow still uses d.

One step by hand

Row (10, 12, 10, 50, 50), g_2 with K = 5, \Delta t = 0.25, mirror border.

pair difference g_2 flow g \cdot d
1–2 2 0.862 1.72
2–3 −2 0.862 −1.72
3–4 40 0.015 0.62
4–5 0 1 0
  • Heat: (10.5, 11, 20.5, 40, 50): the step is already smeared.
  • Perona–Malik: (10.43, 11.14, 10.58, 49.85, 50): the small wiggle is smoothed, the step hardly moves.

Bar charts of the row 10 12 10 50 50: after one heat step 10.5 11 20.5 40 50, the step smeared; after one Perona–Malik step with K = 5: 10.43 11.14 10.58 49.85 50, the step intact.

The math: divergence form and flux

  • In 1D: u_t = \partial_x\big(\phi(u_x)\big) with the flux function \phi(s) = s\,g(|s|).
  • The explicit scheme on a grid: u_i \leftarrow u_i + \Delta t\,\big[\phi(u_{i+1} - u_i) - \phi(u_i - u_{i-1})\big]; what leaves one pixel enters its neighbour, so the total is kept (mirror border).
  • In 2D the same flux on each of the four pairs of neighbours (the original scheme of Perona and Malik). Since 0 \le g \le 1, the heat limit \Delta t \le 1/4 still applies.
  • Expanding the derivative: u_t = \phi'(u_x)\,u_{xx}: the local diffusion coefficient is \phi', not g. This is the key to the next slides.

Heat and Perona–Malik side by side

The noisy test card (\sigma_n = 12) under heat and under Perona–Malik (g_2, K = 10) for the same time.

  • Early (t = 1.8): heat already blurs every edge; Perona–Malik has removed the noise inside the regions and kept the square, the discs, the line.
  • Later: heat is a Gaussian blur of growing σ; Perona–Malik flattens each region, keeps the large contrasts, and removes the lattice and the stripes (their differences are too small).
  • Much later: everything merges, slowly. Insulation is never perfect.

Top row: heat at t = 1.8, 13.5 and 90, increasingly blurred. Bottom row: Perona–Malik at the same times: at 1.8 the card is clean with sharp edges; at 13.5 the lattice dots and stripes have faded but the square, discs and line remain sharp; at 90 only the square and the bright regions remain.

Try it: edge-stopping diffusion

Backward diffusion: edges get sharper

Look at the flow \phi(s) = s\,g(s), not at g:

  • for small differences the flow grows with the difference: ordinary smoothing;
  • beyond s = K (g_2; K/\sqrt2 for g_1) it falls: a steeper edge lets less heat through.

Across a soft edge whose middle is steeper than K, the middle passes less heat than its gentler shoulders. The shoulders flatten, the heat piles up on both sides of the middle, and the middle gets steeper: heat flows “uphill”. This is backward diffusion, an un-blurring.

The histogram below: noise differences sit in the forward range, the step in the backward one.

Top: the flux s times g2(s) for K = 5 rises to a maximum of 2.5 at s = 5 (green, forward range) and then falls (red, backward range). Bottom: the histogram of neighbour differences of a noisy step: most noise differences are below 5; the single step difference is about 40.

Sharpening by hand

Row (0, 2, 6, 10, 12), g_2 with K = 2, \Delta t = 0.25. Differences 2, 4, 4, 2; flows \phi(2) = 1 (at the peak) and \phi(4) = 0.8 (past it).

After one step: (0.25, 1.95, 6, 10.05, 11.75). The shoulders moved towards each other (differences 2 → 1.7), the middle moved apart (4 → 4.05). A heat step would have done the opposite in the middle.

Left: the row 0 2 6 10 12 and after one Perona–Malik step: 0.25 1.95 6 10.05 11.75. Right: a soft edge from 60 to 180 with maximum slope 20 under g2 with K = 8: after 20 steps it is a sharp step with flat sides, after 150 steps still a step; the heat equation for 150 steps has blurred it into a wide ramp.

The math: where the switch happens

  • g_2: \phi(s) = \dfrac{s}{1 + s^2/K^2}, \phi'(s) = \dfrac{1 - s^2/K^2}{(1 + s^2/K^2)^2} < 0 \iff s > K.
  • g_1: \phi(s) = s\,e^{-s^2/K^2}, \phi'(s) = (1 - 2s^2/K^2)\,e^{-s^2/K^2} < 0 \iff s > K/\sqrt2.
  • Where \phi' < 0, u_t = \phi'(u_x)\,u_{xx} is a backward heat equation: a mode e^{i\omega x} grows like e^{|\phi'|\omega^2 t}. Ill-posed: the finest wiggles grow fastest.
  • In 2D, split at an edge pixel into the gradient direction η and the level-line direction ξ: u_t = g(|\nabla u|)\,u_{\xi\xi} + \phi'(|\nabla u|)\,u_{\eta\eta}. Along the edge it always smooths (g > 0); across it, it smooths or sharpens.

A soft diagonal edge with its level lines (cyan). At a point on the edge, the red arrow eta points across the edge along the gradient, the yellow arrow xi points along the level line.

Price 1: staircases

A ramp whose slope is larger than K sits in the backward range everywhere. Tiny noise decides where it starts to sharpen first; neighbouring parts sharpen into separate small edges, the parts in between flatten.

The ramp breaks into terraces: a staircase, at random places.

  • A ramp gentler than K is smoothed (forward range), so K also decides which slopes count as “edges”.
  • TV, in the next unit, has the same artefact for a different reason.

A ramp of slope 4 with noise of size 1 under Perona–Malik with K = 3: after 50 steps it has small kinks; after 500 steps it is a staircase of flat terraces separated by sharp steps.

Try it: sharpening and staircases in 1D

Price 2: ill-posed, and the cure

Backward diffusion amplifies whatever is steep, including noise:

  • an isolated spike higher than about K is seen as an edge: its flux is almost zero, so it is kept (with g_1, forever);
  • noisy pixels with large differences can sharpen into speckles instead of being smoothed.

The result depends on tiny details of the input, and on the grid.

Cure (Catté, Lions, Morel & Coll, 1992): compute g from a slightly smoothed image, g\big(|\nabla(G_\sigma * u)|\big), with σ about 1 (the Canny σ). A single-pixel spike has no large gradient after smoothing, a real edge still does.

A spike of height 40 on a flat profile under Perona–Malik with g1 and K = 10. Classical (blue): after 800 steps the spike is unchanged at 100. Pre-smoothed with sigma 1.5 (orange dashed): the spike is gone, the profile is flat at about 61.

The math: maximum principle and well-posedness

  • In 1D the explicit step is u_i \leftarrow (1 - \Delta t\,(g_{i-\frac12} + g_{i+\frac12}))\,u_i + \Delta t\,g_{i-\frac12}\,u_{i-1} + \Delta t\,g_{i+\frac12}\,u_{i+1}. With 0 \le g \le 1 and \Delta t \le 1/2 all weights are \ge 0 and sum to 1: a convex combination, no new extrema (in 2D: \Delta t \le 1/4).
  • So the discrete scheme is stable even where the continuous PDE is ill-posed: the grid itself regularises it, and the result depends on the pixel size.
  • With g computed from G_\sigma * u the problem has a unique solution that depends continuously on the input (Catté et al., stated without proof).

When to stop?

Perona–Malik has no steady state worth having: insulation is never perfect (g > 0), so if you run forever every region drifts to the mean of the image.

The stopping time is a regularisation parameter, like λ or the number of iterations in the deconvolution day (stopping early regularises):

  • too early: noise left;
  • too late: small regions and fine detail merged;
  • the PSNR against time has a maximum (here 34.5 dB at t \approx 2.3, from 26.5 dB). On this card the heat equation hardly helps: from its first step on, its PSNR only falls, because the fine detail it blurs costs more than the noise it removes.

PSNR against time on a log axis for the noisy test card: Perona–Malik with g2 and K = 10 rises from 26.5 to a maximum of 34.5 dB around t = 2.3 and then falls; the heat equation only falls.

Choosing K and the other knobs

  • K between noise and edges: with noise \sigma_n, differences of two noisy neighbours have spread \sqrt2\,\sigma_n. For \sigma_n = 5 that is about 7; edges of 40 should be stopped: K \approx 10–15 gives g_2 \approx 0.8 for typical noise differences and \approx 0.1 at the edges.
  • Pre-smoothing σ: about 1 pixel; larger σ also moves edges and merges close ones (as in Canny).
  • Time step: the heat limit, \Delta t \le 1/4 in 2D, because g \le 1.
  • Stopping time: grows with the noise. In the demo with K = 10: best at t \approx 0.9 for \sigma_n = 6, 2.3 for 12, 5.4 for 24.

Image libraries usually call this filter “anisotropic diffusion”.

Isotropic or anisotropic?

  • Perona and Malik called their method anisotropic diffusion. Strictly it is not: the conductivity is one number per pixel, and the flow still points down the gradient. It is better called nonlinear isotropic diffusion.
  • True anisotropy smooths along an edge but not across it: the conductivity becomes a 2 × 2 tensor built from the structure tensor (edge- and coherence-enhancing diffusion; it closes gaps in fibres and membranes).
  • The 2D math slide shows that even the scalar version treats the two directions differently through \phi'; that is where the name comes from.
  • The same edge-stopping g appears in the level-set day (geodesic active contours): there it slows a moving front at edges, here it stops heat.

Summary

  • Perona–Malik: u_t = \nabla\cdot(g(|\nabla u|)\nabla u), a heat flow with an edge-stopping conductivity g_1 or g_2 and a contrast parameter K.
  • Differences below K are smoothed as noise; above K the flux falls and edges are kept or even sharpened (backward diffusion).
  • The prices: ramps steeper than K become staircases; isolated spikes survive; the continuous problem is ill-posed. Computing g from a slightly smoothed image fixes the worst of it.
  • There is no useful steady state: the stopping time regularises, and the best one grows with the noise.
  • Explicit steps need the same limit as heat: \Delta t \le 1/4 in 2D.