Exercise: total variation flow

Goal

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

  • compare heat, TV and Perona–Malik as penalties on the gradient (energies), with numbers;
  • predict how plateaus and discs evolve under TV flow: rate perimeter/area, time of disappearance;
  • relate TV flow time and the λ of TV denoising, with a 1D example by hand;
  • explain why explicit TV flow needs tiny steps;
  • (math) derive the Euler–Lagrange equations, the 2/r rate, and ROF as an implicit TV-flow step.

Background

Idea What it means
Energy E(u) = \sum\Psi(|\nabla u|): heat s^2/2, TV s, Perona–Malik \tfrac{K^2}2\ln(1 + s^2/K^2)
Flow gradient descent on E: u_t = \nabla\cdot(g\nabla u), g = \Psi'(s)/s
TV flow u_t = \nabla\cdot(\nabla u/|\nabla u|) = the curvature of the level line through each point
Plateau rule a flat plateau’s height changes at rate (length of its edges)/(its area), towards its surroundings
Disc contrast falls at 2/r, gone at t = c\,r/2
ROF \min \tfrac12\|u - y\|^2 + \lambda\,\mathrm{TV}(u) = one implicit TV-flow step with \Delta t = \lambda; in 1D equal to the flow for t = \lambda
Stiffness g \le 1/\varepsilon, explicit \Delta t \le \varepsilon/4 (2D)

By hand

B1. y = (0, 0, 0, 6, 6, 0, 0, 0), TV flow (equivalently ROF in 1D). (a) For t = \lambda = 2: how fast do the middle plateau and each outer plateau change? Give the result. Check that the mean is kept. (b) At which λ do the middle and the outer plateaus meet? What is the result for \lambda = 6? (c) What would quadratic smoothing do differently to the steps?

B2. Discs on a flat background, contrast 10. When does a disc of radius 5 disappear under TV flow? Radius 20? Does its radius change before that?

B3. One step of height 10 against ten steps of height 1 (a ramp). Compute the cost under heat (s^2/2), TV (s) and Perona–Malik with K = 2. Which energy prefers the ramp, which is indifferent, which (almost) prefers the step?

B4. Predict. A bright square under TV flow. Sketch its outline and its contrast after a while. Which parts change first?

B5. In 1D a plateau of width 8 at height 120 sits between two long regions at 60 (one jump on each side). At which rate does it sink? When does it reach the surroundings (ignore that they rise a little)? A plateau of width 2?

B6. An explicit TV-flow step in 2D with ε = 0.05. What is the largest stable step? How many steps to reach t = 20? And with ε = 0.5?

B1. (a) The middle plateau (width 2, two jumps) sinks at 2/2 = 1 per unit time; each outer plateau (width 3, one jump) rises at 1/3. After t = 2: (2/3, 2/3, 2/3, 4, 4, 2/3, 2/3, 2/3). Mean: (6 \cdot 2/3 + 2 \cdot 4)/8 = 12/8 = 1.5, as before. (b) They meet when 6 - \lambda = \lambda/3, i.e. \lambda = 4.5, at 1.5; from then on everything is flat: for \lambda = 6 the result is 1.5 everywhere. (c) Quadratic smoothing spreads the steps into ramps (values in between, e.g. a smooth bump); TV keeps the jumps and only moves the plateau heights.

B2. t = c\,r/2: radius 5 at t = 25, radius 20 at t = 100. The radius (and the sharpness of the edge) stays the same; only the contrast falls, linearly in time.

B3. Heat: 50 against 10 \cdot \tfrac12 = 5: prefers the ramp. TV: 10 against 10: indifferent. Perona–Malik (K = 2): 2\ln(1 + 25) = 6.52 against 10 \cdot 2\ln(1.25) = 4.46: the step costs only 1.5 times the ramp although it is ten times as steep: nearly prefers the step (and for larger steps the cost hardly grows at all).

B4. The straight sides stay where they are and sharp; the corners (very high curvature) round off first; the whole square loses contrast at about perimeter/area = 4/side, faster near the corners. After a long time it looks like a faint disc, then disappears.

B5. Perimeter/area in 1D: 2 jumps / width 8 = 0.25 per unit time; from 120 to 60 takes 60/0.25 = 240. Width 2: rate 1, reaches 60 after 60. (The surroundings rise a little, so it meets them slightly earlier.)

B6. \Delta t \le \varepsilon/4 = 0.0125; 20/0.0125 = 1600 steps. With ε = 0.5: \Delta t \le 0.125, 160 steps. Ten times smaller ε, ten times more steps for the same result time.

Math questions

B7. (math) Derive the Euler–Lagrange equation of E(u) = \int\Psi(|\nabla u|) (with no flux across the border) and the gradient-descent flows for \Psi = s^2/2, s and \tfrac{K^2}2\ln(1 + s^2/K^2). Where is the last one non-convex?

B8. (math) Derive the 2/r rate: integrate u_t = \nabla\cdot(\nabla u/|\nabla u|) over a bright disc with a sharp edge and use the divergence theorem. What does the same argument give for a square of side a, and why is it only approximate there?

B9. (math) Write the optimality condition of ROF and show that it is one implicit TV-flow step with step size λ. Verify it for the 1D example of B1 at λ = 2 using the discrete version u_i - y_i = \lambda\,(z_{i} - z_{i-1}) with z_i \in [-1, 1] and z_i = \mathrm{sign}(u_{i+1} - u_i) where u_{i+1} \ne u_i.

B7. \frac{d}{d\epsilon}E(u + \epsilon v)|_0 = \int\Psi'(|\nabla u|)\frac{\nabla u}{|\nabla u|}\cdot\nabla v = -\int\nabla\cdot\Big(\Psi'(|\nabla u|)\frac{\nabla u}{|\nabla u|}\Big)v (boundary term zero). Euler–Lagrange: \nabla\cdot(g\nabla u) = 0 with g = \Psi'(s)/s; descent: u_t = \nabla\cdot(g\nabla u). \Psi = s^2/2: g = 1, u_t = \Delta u. \Psi = s: g = 1/s, u_t = \nabla\cdot(\nabla u/|\nabla u|). \Psi = \tfrac{K^2}2\ln(1 + s^2/K^2): \Psi' = s/(1 + s^2/K^2), g = g_2. \Psi'' = (1 - s^2/K^2)/(1 + s^2/K^2)^2 < 0 for s > K: non-convex there.

B8. \pi r^2\,h'(t) = \int_D\nabla\cdot\frac{\nabla u}{|\nabla u|} = \oint_{\partial D}\frac{\nabla u}{|\nabla u|}\cdot n_{\text{out}}\,ds = -2\pi r (on the edge of a bright disc the gradient points inwards), so h' = -2/r. For a square: a^2 h' = -4a, h' = -4/a, if the square stayed a flat plateau. It does not: the curvature at the corners is larger than perimeter/area, the corners sink faster than the middle, and the square stops being a plateau (it is not a “calibrable” set).

B9. Optimality: 0 \in u - y + \lambda\,\partial\mathrm{TV}(u), i.e. u - y = \lambda\,\nabla\cdot(\nabla u/|\nabla u|) where \nabla u \ne 0: (u - y)/\lambda = \kappa(u), one backward-Euler step of u_t = \kappa from y. Check on B1 (u = (\tfrac23, \tfrac23, \tfrac23, 4, 4, \tfrac23, \tfrac23, \tfrac23), \lambda = 2, z_0 = 0 before pixel 1, z_8 = 0 after pixel 8): z_3 = +1 (rise between pixels 3 and 4), z_5 = -1 (fall between 5 and 6); in the flat parts z is interpolated: z_1 = 1/3, z_2 = 2/3 (so u_i - y_i = 2(z_i - z_{i-1}) = 2/3 for i = 1, 2, 3), z_4 = 0 (so u_4 - 6 = 2(0 - 1) = -2 and u_5 - 6 = 2(-1 - 0) = -2), z_6 = -2/3, z_7 = -1/3 (each outer pixel gets 2 \cdot 1/3 = 2/3). All |z_i| \le 1: the condition holds.

Explore

1D plateaus: plateaus of width 2, 4 and 8 (height 120 on 60) and a ramp, under explicit TV flow (\Delta t = \varepsilon/2.5, blue), ROF with λ = t (dashed red) and heat (orange); below, each plateau’s height against time with the predicted lines. Discs and shapes: discs of radius 3 to 12 (contrast 40), a square, a star and a bar under TV flow (repeated ROF steps), and each disc’s contrast against time (dashed: 40 - 2t/r). Image: TV, Perona–Malik and heat on a noisy image, each tuned so that it removes exactly as much as the noise.

E1. Preset plateaus sink at rate 2/width (t = 40). Read the three plateau heights and compare with 120 - 2t/w. How many explicit steps were needed?

E2. Preset flow time ≈ λ. Compare the blue flow and the dashed ROF (\lambda = t) at t = 10, 30 and 60 (use t + 5). Where do they differ, if at all?

E3. Preset small ε: ε = 0.05. How many steps to t = 20? Set ε = 0.5 and restart: how many now? Does the result change?

E4. Discs tab, preset small discs vanish first (t = 90). Which discs are gone? Play on and note roughly when each disc’s contrast reaches 0. Compare with t = 20r.

E5. Preset corners round, edges stay. Describe the square, the star and the bar (width 8) over time. Which feature of the star goes first?

E6. Image tab, presets TV vs PM vs heat and … on the micrograph. Read the PSNR of each method. On which image do the edge-preserving methods win clearly, and why not on the other?

E1. 80, 100 and 110: exactly 120 - 80/2, 120 - 80/4, 120 - 80/8. 200 steps of \Delta t = 0.2 (ε = 0.5).

E2. The plateau heights agree exactly at all three times (110, 115, 117.5 at t = 10; 90, 105, 112.5 at 30; at 60 the narrowest plateau has reached its rising surroundings, 67.4 for the flow against 68.6 for ROF). The curves differ by a few grey levels (at most 2 to 4) at the corners of the ramp, where the flow’s ε rounds the corners and ROF keeps them sharp.

E3. ε = 0.05: 1000 steps of \Delta t = 0.02. ε = 0.5: 100 steps. The plateau heights are the same (100, 110, 115 at t = 20); a larger ε only rounds the corners slightly.

E4. At t = 90 the discs of radius 3 and 4.5 are gone; radius 6 has 4.3 left, 9 has 15.9, 12 has 21.5. They reach 0 at about t \approx 50, 80, 102, 154 and 298 against the predicted 60, 90, 120, 180 and 240: small digital discs go a little faster than 2/r predicts (their staircase outline is longer than 2\pi r), the largest a little slower (the repeated ROF steps are not fully converged on large regions).

E5. The square keeps its straight sides, its corners round off, and it fades. The star loses its thin points first (highest curvature), turning into a rounder blob. The bar (60 × 8) fades at about perimeter/area = (2 \cdot 60 + 2 \cdot 8)/(60 \cdot 8) \approx 0.28 per unit time, close to a 1D plateau of width 8 (2/8): its contrast is 28 at t = 40, 4 at t = 120 and about 1 at t = 160; its ends round off.

E6. Test card (noise 15): TV 29.8, PM 28.4, heat 26.6 dB (noisy 24.5): the card is piecewise constant, exactly what TV and PM assume. Micrograph: TV 25.4, PM 25.4, heat 25.3 dB (noisy 24.5): a textured image without flat regions and sharp edges, where “piecewise constant” is no better a belief than “smooth”.

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/tv_flow/exercise.rs.

C1. energy_1d(row, penalty): the sum of \Psi(|u_{i+1} - u_i|) over all neighbour pairs, for the three penalties heat (s^2/2), TV (s) and Perona–Malik (\tfrac{K^2}{2}\ln(1 + s^2/K^2)). The check is B3.

C2. tv_step_1d(row, dt, eps): one explicit step of TV flow on a row, mirror border:

for every pair of neighbours (i, i+1):
    d = u[i+1] − u[i]
    flow[i] = d / sqrt(d² + ε²)              # about +1 or −1, whatever the size of the step
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 B1 (flowing to t = 2 with a small ε gives (2/3, 2/3, 2/3, 4, 4, 2/3, 2/3, 2/3)) and demo question E1 (plateaus of width 2, 4, 8 at 120 - 2t/w).

C3. tv_step(img, dt, eps): one explicit step in 2D:

for every pixel (x, y):
    ux = I(x+1, y) − I(x, y)        (0 in the last column)
    uy = I(x, y+1) − I(x, y)        (0 in the last row)
    m  = sqrt(ux² + uy² + ε²)
    px(x, y) = ux / m,   py(x, y) = uy / m
for every pixel (x, y):              # px(−1, y) = py(x, −1) = 0
    new(x, y) = I(x, y) + Δt · (px(x, y) − px(x−1, y) + py(x, y) − py(x, y−1))

The checks: a disc of radius 8 and contrast 40 keeps a sharp edge and has a contrast of about 40 - 2t/r = 30 at t = 40; the total is kept.

Then cargo run -p pdesmooth -- tv-flow and open the pictures in out/tv-flow/. The program works in grey levels 0–255, as the lecture does.

C4. The program runs the 1D plateaus to t = 20 with ε = 0.5 and ε = 0.05. How many steps does each need, and do the results differ? (Count the cost:) how many steps would ε = 0.005 need?

C5. shapes-t0.png to shapes-t240.png: discs of radius 3, 4.5, 6, 9 and 12 (contrast 40) and a square of side 48 under TV flow, with the measured disc contrasts and the prediction 40 - 2t/r. Do the discs follow the prediction? What happens to their edges, and to the square’s corners?

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

C4. ε = 0.5: 100 steps of \Delta t = 0.2, plateaus at 100.0, 110.1, 115.4. ε = 0.05: 1000 steps of 0.02, plateaus at 100.0, 110.0, 115.0. The results hardly differ, but ten times smaller ε costs ten times the steps: ε = 0.005 would need 10 000 steps (slide “Numerics, honestly: TV flow is stiff”).

C5. At t = 40 the contrasts are 10.7, 22.3, 26.2, 30.7, 32.7 against the predicted 13.3, 22.2, 26.7, 31.1, 33.3. At t = 80 the radius-3 disc is gone (0.1) and the others are at 3.0, 10.5, 19.7, 23.9 (predicted 4.4, 13.3, 22.2, 26.7); at t = 120 only the discs of radius 9 and 12 are left (8.4 and 15.0). They fade a little faster than predicted: a disc on the pixel grid has a jagged outline, longer than 2\pi r. The edges stay sharp while the contrast falls; only a faint halo spreads into the background, where ε turns the flow into slow heat flow. The square’s corners round off: at t = 240 its centre is at 83.8, a point 4 pixels inside a corner at 77.1, the background at 63.5.

Where this leads

The bilateral filter and non-local means average pixels by similarity rather than by a PDE; the next unit, neighbourhood filters in depth, shows that they too are diffusions, one with an edge-stopping weight like Perona–Malik’s, the other on a graph that connects look-alike patches.