Exercise: edge-stopping diffusion (Perona–Malik)

Goal

By the end of this exercise you should be able to:

  • compute edge-stopping conductivities and take one Perona–Malik step by hand;
  • predict which differences are smoothed and which are sharpened (the flux and its peak);
  • explain staircasing, the surviving spike and why pre-smoothing helps;
  • choose K and a stopping time from the noise level;
  • (math) derive the switch points of the flux, the discrete maximum principle and the split along and across an edge.

Background

Idea What it means
Perona–Malik step between each pair of neighbours flow \Delta t\,g(|d|)\,d, d = difference; heat when g = 1
Edge-stopping functions g_1(s) = e^{-(s/K)^2}, g_2(s) = 1/(1 + (s/K)^2)
Flux \phi(s) = s\,g(s): rises up to s = K (g_2) or K/\sqrt2 (g_1), then falls (backward range)
Pre-smoothing (Catté) compute g from G_\sigma * u (σ ≈ 1), apply the flows to u
Stability \Delta t \le 1/4 in 2D, 1/2 in 1D (as for heat, since g \le 1)
Stopping time a regularisation parameter: too short = noise left, too long = details merged

By hand

B1. g_2 with K = 5. Compute g_2 for differences of 1, 5 and 20. Which of them would you call noise, which edge?

B2. The row (10, 12, 10, 50, 50), mirror border, \Delta t = 0.25. (a) One heat step. (b) One Perona–Malik step with g_2, K = 5: list the four differences, their g_2 and their flows, then the new row. (c) Compare the third pixel in (a) and (b).

B3. The row (0, 2, 6, 10, 12), g_2 with K = 2, \Delta t = 0.25. (a) The four flows, and the new row. (b) Which neighbouring differences grew and which shrank? Why? (c) What would one heat step give for the middle two differences?

B4. Predict. A single bright pixel of height 30 on a flat background of 0 (2D), K = 5, \Delta t = 0.25. (a) With g_2: how much heat does it lose in the first step? Will it disappear eventually? (b) With g_1? (c) With g_1 and pre-smoothing σ = 1?

B5. Choosing K. An image with noise \sigma_n = 5 and the edges you care about have a height of about 40. (a) What is the spread of the difference of two noisy neighbours in a flat region? (b) Suggest a K for g_2 and give g_2 for a typical noise difference and for an edge. (c) Would you raise or lower K for a noisier image of the same object?

B6. For each problem, name the knob to turn and in which direction: (a) the result is still noisy inside large regions; (b) a faint but real boundary disappeared; (c) a ramp-like background became terraced; (d) isolated bright noisy pixels survive.

B1. g_2(1) = 1/1.04 = 0.96, g_2(5) = 0.5, g_2(20) = 1/17 = 0.059. A difference of 1 is treated as noise (almost full flow), 20 as an edge (6 % of the flow); 5 is exactly at the switch.

B2. (a) Heat: (10.5, 11, 20.5, 40, 50). (b) Differences 2, −2, 40, 0; g_2 = 0.862, 0.862, 0.0154, 1; flows 1.724, −1.724, 0.615, 0. New row: 10 + 0.25 \cdot 1.724 = 10.43; 12 + 0.25\,(-1.724 - 1.724) = 11.14; 10 + 0.25\,(1.724 + 0.615) = 10.58; 50 - 0.25 \cdot 0.615 = 49.85; 50. (c) Heat moves the third pixel to 20.5, halfway up the step; Perona–Malik to 10.58: it stays on its own side.

B3. (a) Differences 2, 4, 4, 2; g_2(2) = 0.5, g_2(4) = 0.2; flows 1, 0.8, 0.8, 1. New row: (0.25, 1.95, 6, 10.05, 11.75). (b) The outer differences shrank (2 → 1.7), the middle ones grew (4 → 4.05). The middle pixels pass 0.8 across the steep part but exchange 1 with the gentle shoulders: pixel 2 loses more to pixel 1 than it gets from pixel 3, pixel 4 gets more from pixel 5 than it loses to pixel 3. (c) Heat: (0.5, 2.5, 6, 9.5, 11.5): the middle differences shrink to 3.5. Heat always flattens; Perona–Malik sharpened.

B4. (a) Each of the four differences is 30: \phi(30) = 30/(1 + 36) = 0.81, so the pixel loses 0.25 \cdot 4 \cdot 0.81 = 0.81 in the first step (29.19 left). As it gets lower, its differences shrink towards K and the flux grows: it does disappear (the demo’s 2D test: 20.5 after 10 steps, 0.11 after 100). (b) \phi(30) = 30\,e^{-36} \approx 7 \cdot 10^{-15}: it is frozen at 30 forever. (c) Smoothed with σ = 1, the dot becomes a low bump whose gradients are well below K, so g \approx 1 around it and it spreads like heat: after one step about 4 is left, after 10 steps 1.5.

B5. (a) \sqrt2 \cdot 5 \approx 7.1. (b) Choose K between, e.g. K = 15: g_2(7) = 0.82 (noise smoothed), g_2(40) = 0.12 (edge kept). (c) Raise K (the noise differences grow), and expect a longer stopping time; if the edges are not much higher than the noise, no K separates them.

B6. (a) Run longer, or raise K a little. (b) Lower K (the boundary’s contrast is in the “noise” range), or stop earlier. (c) Raise K above the slope of the ramp (or accept it: it is the backward range). (d) Use pre-smoothing (σ ≈ 1), or g_2 instead of g_1.

Math questions

B7. (math) Derive \phi'(s) for g_1 and g_2 and the switch points K/\sqrt2 and K. Show that u_t = \partial_x\phi(u_x) equals \phi'(u_x)\,u_{xx} and explain why \phi' < 0 means “backward heat equation”.

B8. (math) Show that the 1D explicit Perona–Malik step is a convex combination of u_{i-1}, u_i, u_{i+1} when 0 \le g \le 1 and \Delta t \le 1/2. Conclude that the discrete scheme creates no new maxima, even though the continuous equation can sharpen.

B9. (math) In 2D write \nabla u = |\nabla u|\,\eta with the unit gradient direction η, and ξ ⟂ η. Using \Delta u = u_{\xi\xi} + u_{\eta\eta} and \nabla|\nabla u|\cdot\eta = u_{\eta\eta}, derive \nabla\cdot(g(|\nabla u|)\nabla u) = g\,u_{\xi\xi} + \phi'(|\nabla u|)\,u_{\eta\eta}.

B7. g_2: \phi = s/(1 + s^2/K^2), \phi' = \frac{(1 + s^2/K^2) - s \cdot 2s/K^2}{(1 + s^2/K^2)^2} = \frac{1 - s^2/K^2}{(1 + s^2/K^2)^2}, negative for s > K. g_1: \phi = s\,e^{-s^2/K^2}, \phi' = e^{-s^2/K^2}(1 - 2s^2/K^2), negative for s > K/\sqrt2. Chain rule: \partial_x\phi(u_x) = \phi'(u_x)\,u_{xx}. With a constant coefficient c < 0, u_t = c\,u_{xx} sends e^{i\omega x} to e^{|c|\omega^2 t}e^{i\omega x}: high frequencies grow without bound, the time-reversed heat equation, which has no stable solution for general data.

B8. u_i^{\text{new}} = u_i + \Delta t\,[g_+(u_{i+1} - u_i) - g_-(u_i - u_{i-1})] = \Delta t g_-\,u_{i-1} + (1 - \Delta t(g_- + g_+))\,u_i + \Delta t g_+\,u_{i+1}, with g_\pm the conductivities of the two pairs. The weights sum to 1; \Delta t g_\pm \ge 0, and 1 - \Delta t(g_- + g_+) \ge 1 - 2\Delta t \ge 0 for \Delta t \le 1/2. So u_i^{\text{new}} lies between the smallest and largest of its three old values, and the global maximum cannot grow. The discrete scheme sharpens only by moving values within that range (it is the grid that tames the ill-posed equation).

B9. \nabla\cdot(g\nabla u) = g\,\Delta u + \nabla g\cdot\nabla u = g\,\Delta u + g'(|\nabla u|)\,\nabla|\nabla u|\cdot\nabla u = g\,(u_{\xi\xi} + u_{\eta\eta}) + g'\,|\nabla u|\,u_{\eta\eta}. Since \phi'(s) = g(s) + s\,g'(s), this is g\,u_{\xi\xi} + \phi'(|\nabla u|)\,u_{\eta\eta}: along the level line the coefficient g > 0 always smooths; across it, \phi' smooths or sharpens.

Explore

2D: the noisy test card (or the micrograph): the current image, the conductivity map (white: heat flows), PSNR against time with the best value so far (red dot), and row 112 of the image (step, ramp, 1-px line, soft step) against the truth. 1D: a profile with a step, a soft step, a ramp and a spike; Perona–Malik (blue), heat for the same time (orange); below, the flux φ against the size of a difference with the histogram of the current differences; drag on that plot to set K; the shaded part is the backward range.

E1. Preset linear: plain blur, then edges stop the heat. Compare the PSNR curves: where is the best stopping time of each, and what is the best PSNR? Why does the linear curve only fall on this image?

E2. Presets K too small and K too large. Describe the image and the conductivity map in each case. Which one is “the heat equation again”?

E3. 1D tab, preset soft edge sharpens. Drag K upwards and restart each time. At which K does the soft step stop sharpening (look at the profile after 150 steps)? The steepest difference of the soft step is 19.3: compare.

E4. Preset ramp becomes a staircase. How many terraces appear after 600 steps, and how high are the jumps? Now set K = 8 and restart: what happens to the ramp?

E5. Presets a spike survives forever and pre-smoothing fixes the spike. What is the spike’s value after 150 steps in each case? Press play for a long time in the first preset: does the spike really survive forever, and what finally changes?

E6. 2D, preset edges stop the heat. Find the best stopping time for noise 6, 12 and 24 (move the noise slider, restart, play and watch the red dot). Does it move?

E7. Preset nuclei in the micrograph. What does the method keep, what does it remove? Set the pre-smoothing σ to 0: what changes?

E1. Linear: the PSNR only falls, from 26.5 dB (the noisy input) to 19.5 dB after 40 steps: the card is full of fine structures (lattice, stripes, 1-px line), and blurring them costs more than the noise removed. Perona–Malik (g_2, K = 10): the PSNR rises to 34.5 dB at t \approx 2.3 and then falls slowly as the lattice and the stripes (differences below K) are smoothed away.

E2. K = 2: almost every difference counts as an edge, the conductivity map is black nearly everywhere and the noise stays (after 60 steps the PSNR is still creeping up, 31.5 dB). K = 150: g \approx 1 everywhere (white map), the result is a plain blur: the heat equation again (best PSNR 28.3 dB right after the first step).

E3. It still sharpens at K = 11 (steepest difference after 150 steps: 40) and diffuses from K = 12 on (about 5). The rule “steeper than K sharpens” holds only locally: at the start only the very middle of the soft step is above K, and the gentle shoulders, in the forward range, spread the edge faster than the middle steepens. In practice K must be clearly below the steepest slope.

E4. Three terraces separated by jumps of about 21, 45 and 21 grey levels, with almost flat parts (differences of 0.2–0.4) between them. With K = 8 the slope 4 is in the forward range: the ramp stays a ramp (its corners round off).

E5. Classical (g_1, K = 10): the spike is still exactly 100 after 150 steps. Pre-smoothed (σ = 1.5): it is gone within 10 steps (65.6, close to the neighbours). Playing on: the spike does not diffuse, but the background around it rises as heat from the ramp spreads to it (after 300 steps the spike is 93.8 next to 79.5); once its differences fall below about K/\sqrt2 it melts. “Forever” means: as long as its own contrast stays above K.

E6. With K = 10 the best time is about t = 0.9 for noise 6 (39.7 dB), 2.3 for noise 12 (34.5 dB) and 5.4 for noise 24 (28.0 dB). It moves: more noise needs more smoothing time. (For noise 24 a larger K would also be needed: the noise differences are then about 34.)

E7. It keeps the nuclear envelope and other high-contrast outlines and flattens the fine texture inside the nucleus and the cytoplasm. Without pre-smoothing the texture’s own steep pixels count as edges: more speckle survives and the result looks grainier.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in pdesmooth/src/perona_malik/exercise.rs.

C1. g1(s, k) and g2(s, k): the two edge-stopping functions. The check is B1.

C2. pm_step_1d(row, dt, k, g): one Perona–Malik step on a row, mirror border (nothing flows out at the ends):

for every pair of neighbours (i, i+1):
    d = u[i+1] − u[i]
    flow[i] = g(|d|, K) · d                 # what moves from i+1 to i
for every position i:
    new[i] = u[i] + Δt · (flow[i] − flow[i−1])    # leave out a flow that does not exist at the ends

The checks are B2 (and, with a huge K, the heat step of B2 a), B3, and that the total is kept.

C3. pm_step(img, dt, k, g, sigma): one step on an image, four flows per pixel. With sigma > 0 the conductivity uses the differences of the image blurred with a Gaussian of that σ (already written for you); the flows still use the differences of the image itself:

S = Gaussian blur of I with σ        (S = I when σ = 0)
for every pixel p:
    change = 0
    for each of the 4 neighbours q:
        change = change + g(|S(q) − S(p)|, K) · (I(q) − I(p))
    new(p) = I(p) + Δt · change

The checks are B4 (the spike with g_2, with g_1, and with g_1 and pre-smoothing), and that a huge K gives the heat step.

C4. psnr(result, truth, peak): 10\log_{10}(\text{peak}^2/\text{mean squared error}).

Then cargo run -p pdesmooth -- perona-malik and open the pictures in out/perona-malik/. The program uses a test card with known truth (a square, discs, stripes, a ramp and a 1-pixel line) and noise of 12 grey levels.

C5. The program prints the PSNR of heat and of Perona–Malik (g_2, K = 10 grey levels, pre-smoothing σ = 1) against time. When is each one best? Compare pm-t6.png with heat-t6.png: what has Perona–Malik kept, and what has it lost?

C6. Compare conductivity-raw.png with conductivity-smoothed.png (g_2 of the gradient, white where heat flows). Which one would you use, and why?

C7. Read the three profile results: the soft edge, the noisy ramp of slope 4 with K = 3, and the spike with g_1. Which lecture slides do they illustrate? How many terraces does the ramp have after 600 steps?

The model solution is pdesmooth/src/perona_malik/solution.rs in the download.

C5. The noisy card has 26.6 dB. Heat is best after its first step (28.2 dB at t = 0.2) and then only gets worse (21.3 dB at t = 6). Perona–Malik rises to 34.2 dB at t = 0.4, then falls (23.5 dB at t = 6): it too must be stopped. At t = 6 heat has blurred every edge; Perona–Malik keeps the square, the discs and the ends of the ramp sharp and the background clean, but the stripes have merged into flat grey and the 1-pixel line has faded: their differences look small after the pre-smoothing.

C6. From the raw image the map is speckled black and white all over: the noise itself has large differences, so many flat pixels insulate. After smoothing with σ = 1 the map is white in flat regions and black along the outlines of the square, the discs, the line and the bands: heat flows where it should and stops at the edges. Use the smoothed one (slide “Price 2: ill-posed, and the cure”). It is darker over the stripes but no longer black, which is why they merge in C5.

C7. The soft edge’s steepest difference grows from 19.8 to 41.2 under Perona–Malik (K = 8) and falls to 5.3 under heat: backward diffusion sharpens it (slide “Backward diffusion: edges get sharper”). The ramp breaks into five terraces (at about 66, 87, 118, 155 and 168), separated by jumps of 10 to 34 (slide “Price 1: staircases”). The spike is still exactly 100 after 150 steps with g_1, and down to 62 (the background is 60) with pre-smoothing σ = 1.5 (slide “Price 2”).

Where this leads

Perona–Malik smooths by a rule; the next unit, total variation flow, shows that heat, Perona–Malik and TV are all “go downhill on an energy”, with three different penalties on the gradient, and that under TV flow small objects fade first at a rate set by their perimeter over area.