Exercise: all smoothers on one image
Before this unit: Filling holes: diffusion and exemplar inpainting (previous unit); Day 5: Deconvolution; Day 2: Edges, blobs and ridges.
See also: Day 28: Ising model and Markov random fields; Day 31: Level sets.
Goal
By the end of this exercise you should be able to:
- name the belief behind each of eight smoothers and predict where it fails;
- recognise a method from what it does to a profile or from its method noise;
- choose a score that matches a question, and a method that matches an image;
- (math) classify the methods as linear or not, convex or not, local or not.
Background
| method | belief | typical failure |
|---|---|---|
| Gaussian (heat) | smooth | blurs every edge and fine detail |
| median | outliers rare, structures wider than the window | removes thin lines and dots |
| Perona–Malik | piecewise smooth, edges above K | staircases ramps, keeps spikes |
| TV | piecewise constant | staircases, lowers small-object contrast, flattens texture |
| bilateral | nearby similar values belong together | fails when noise ≈ edge height; noisy rim at edges |
| non-local means | the image repeats | no help on unique details |
| wavelet shrinkage | few large coefficients | ringing near edges, loses medium-size texture |
| Wiener | known power spectrum | blurs sharp, sparse structures like a Gaussian |
By hand
B1. Match the profile. A row with a step, a ramp, a 1-pixel line and a soft step, smoothed by six methods. Which method made each description? (a) Step and line slightly blurred, ramp smooth, everything a little noisy. (b) Step sharp, line gone completely, ramp smooth. (c) Step sharp, line kept, ramp turned into a few terraces. (d) Step sharp, line kept, a soft step made sharper, flat parts very clean after many repetitions. (e) Everything sharp and clean, except a unique feature that is as noisy as before. (f) Small ripples (overshoots) next to the step.
B2. Choose and justify (name the belief): (a) a crystal lattice in electron microscopy; (b) a segmentation map that is nearly piecewise constant (a few flat regions); (c) tissue with smooth intensity gradients; (d) an image with hot pixels.
B3. Two methods give the same PSNR on a test card. You will count small spots of radius 2–3. Which scores do you compare? Which tab of the demo shows the answer at a glance?
B4. After smoothing you threshold the image to find cells, and then report each cell’s integrated intensity. On which image do you threshold, and on which do you integrate? Why?
B1. (a) Gaussian (or Wiener): linear blur at a small σ. (b) Median: keeps steps, removes structures thinner than the window. (c) TV (or Perona–Malik with K below the ramp’s slope): staircasing. (d) Iterated bilateral (or long Perona–Malik): cartoon, backward-diffusion-like sharpening. (e) Non-local means: the unique feature has no copies. (f) Wavelet shrinkage (or Wiener): oscillations near a step from removed high-frequency or detail coefficients.
B2. (a) Non-local means: the image repeats itself. (b) TV: piecewise constant with short boundaries. (c) Gaussian, Wiener or a gentle Perona–Malik with large K: the image is smooth; TV would staircase the gradients. (d) Median: outliers are rare and isolated.
B3. The contrast kept for small discs (“disc kept”) and the method noise around small objects; also the edge width if spot size matters. The scores tab gives the numbers, the method noise tab shows whether the spots appear in what was removed.
B4. Threshold on the smoothed (preferably edge-preserving) image: cleaner masks. Integrate on the raw image inside the masks: every smoother moves intensity between objects and their surroundings (TV lowers small-object contrast at rate perimeter/area, Gaussians spread it out), while the raw image is an unbiased, if noisy, measurement.
Math questions
B5. (math) Classify the eight methods as linear or non-linear, as minimising a convex energy, a non-convex one or no energy, and as local or non-local. Give a one-line reason each.
B6. (math) Soft thresholding of a coefficient c with threshold t minimises \tfrac12(x - c)^2 + t|x|. Show this, and explain why with an orthogonal wavelet transform W it minimises \tfrac12\|Wu - Wy\|^2 + t\|Wu\|_1 for the whole image.
B5. Gaussian: linear (a convolution), convex (one implicit heat step = quadratic smoothing), local. Median: non-linear (order statistic), convex per window (minimises \sum|I_q - m|), local. Perona–Malik: non-linear, non-convex energy (flattening penalty), local. TV: non-linear, convex (ROF), local. Bilateral: non-linear (weights depend on the image), no single convex energy (one reweighting step of a robust, flattening penalty), local. NLM: non-linear, no energy (one heat step on a graph built from the image), non-local. Wavelet shrinkage: non-linear (thresholding), convex (\ell_1 on coefficients), local in space and scale. Wiener: linear, convex (minimum mean squared error among linear filters), global (it acts on the whole spectrum).
B6. For x > 0 the derivative is x - c + t, zero at x = c - t (valid if c > t); for x < 0, x = c + t (valid if c < -t); otherwise the minimum is at the kink x = 0 (the subgradient [-t, t] contains c). So x = \mathrm{sign}(c)\max(|c| - t, 0). With W orthogonal, \|Wu - Wy\|^2 = \|u - y\|^2, and the energy separates into one such problem per coefficient: thresholding each coefficient minimises the whole.
Explore
The test card (or the micrograph) with seeded Gaussian, Poisson or salt-and-pepper noise, smoothed by all eight methods, each started at its best-PSNR setting for the preset noise levels (they compute one after the other). Click a result to select it; its parameter slider is in the panel (“back to best PSNR” resets). Tabs: results (with a synchronised 40 × 40 zoom), method noise, profiles along row 112, and scores (best per column in green). Threshold marks the pixels above 120.
E1. Preset Gaussian noise, moderate, scores tab. Which method keeps the 1-pixel line best among those with a PSNR of at least 30 dB? Which keeps the small disc best?
E2. Presets salt and pepper and Poisson, few photons. Which method wins the PSNR in each? Look at the method noise of the median for salt and pepper.
E3. Preset strong noise: all fail differently (σn = 40). Rank the methods by PSNR. Then look at the results tab: describe how three of them fail.
E4. Presets texture: NLM and piecewise constant: TV (zoomed). Which method looks best in each crop, and which looks worst?
E5. Preset best PSNR vs best look (micrograph, settings chosen by eye). Compare the PSNR with the best-PSNR settings (button “back to best PSNR”). Which would you show in a figure, and why?
E6. Preset smoothing before thresholding (σn = 40). Look at the red masks. Select TV and raise λ to about 45; select the Gaussian and raise σ to 2. Which mask is cleaner on the square and the discs?
E1. Among PSNR ≥ 30 dB (TV 30.6, bilateral 30.1, NLM 34.6): bilateral and NLM keep the line fully (1.00), TV 0.85. The small disc (radius 3): the Gaussian keeps 0.98 of its contrast at its (very mild) best setting, Wiener 0.98, Perona–Malik 0.96; TV 0.88 (perimeter/area loss) and bilateral 0.86.
E2. Salt and pepper (5 %): the median wins (26.3 dB; NLM 25.1, Perona–Malik 24.2, Gaussian 23.5); its method noise shows the removed outliers and, unfortunately, the line and the lattice dots. Poisson (30 photons at 255): NLM 26.8, TV 25.9, Perona–Malik 25.4, Gaussian 24.4.
E3. NLM 24.6, TV 24.1, Perona–Malik 23.2, Gaussian 22.7, wavelets 22.7, bilateral 22.6, Wiener 22.6, median 21.5 dB. Typical failures: the Gaussian and Wiener leave blotchy noise and blurred edges; the median loses the line and the lattice; TV turns the noise in flat regions into patches and loses the smallest discs; NLM loses the unique ring and the rims of discs (no copies).
E4. Lattice crop: NLM gives clean, identical dots; TV and the median flatten or lose the dots; Gaussian and Wiener leave them blurry and noisy. Square crop: TV gives a flat interior with crisp edges and corners; NLM is nearly as good; the Gaussian and Wiener are noisy and soft.
E5. On the micrograph the best-PSNR settings give about 26.4 dB (Gaussian), 26.5 (TV), 26.2 (bilateral), 25.7 (NLM); the “by eye” settings 21.8, 24.3, 24.4, 23.3: lower PSNR, cleaner look. For a figure the stronger smoothing may be acceptable if stated; for analysis the less smoothed one loses less real structure. Neither replaces measuring on the raw image.
E6. At the best-PSNR settings all masks are speckled (they smooth too little for a threshold at σn = 40). With TV λ ≈ 45 the square and discs come out crisp with clean flat regions (105 wrong pixels in that part of the card, flat-region noise 2.0); with a Gaussian of σ = 2 the shapes are rounded and the smallest disc is lost (151 wrong, noise 4.7); Perona–Malik (time 2) is in between (132).
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/smoothers/exercise.rs. Heat, Perona–Malik, TV, the bilateral filter and non-local means are the coding exercises of the earlier units of this day; here come the scores and two more smoothers. A Gaussian blur is already written for you.
C1. psnr(result, truth): 10\log_{10}(1/\text{mean squared error}), for grey values in [0, 1].
C2. median_filter(img, radius): the median of the (2\,\text{radius} + 1)^2 window around every pixel (window pixels outside the image are left out):
for every pixel p:
list = the values of the window around p, sorted
out(p) = the middle value of list (even count: the mean of the two middle ones)
The checks: a hot pixel and a 1-pixel line disappear, a straight step stays exactly as it is.
C3. soft_threshold(c, t) (B6) and haar_shrink(img, t): wavelet shrinkage with one level of the 2D Haar transform:
for every 2 × 2 block a b
c d :
s = (a + b + c + d)/2 # the mean part
h = (a − b + c − d)/2, v = (a + b − c − d)/2, g = (a − b − c + d)/2 # the details
h, v, g = soft_threshold(h, t), soft_threshold(v, t), soft_threshold(g, t)
a, b, c, d = (s + h + v + g)/2, (s − h + v − g)/2, (s + h − v − g)/2, (s − h − v + g)/2
The checks: t = 0 gives the image back; a huge t leaves the mean of each block.
C4. threshold_errors(result, truth, level): the number of pixels where “above the level” differs between the result and the truth.
Then cargo run -p pdesmooth -- smoothers and open the pictures in out/smoothers/. The program uses a test card with known truth (a square, discs, a dot lattice, stripes, a ramp and a 1-pixel line) with noise of 15 grey levels, and gives each smoother its best PSNR over a few settings.
C5. Read the PSNR and “line kept” (the share of the 1-pixel line’s contrast left) of the Gaussian, the median and the wavelet shrinkage, and look at the three -method-noise.png pictures (what each one removed, 0 = grey). Which belief of the table at the top of this page does each picture show?
C6. Salt and pepper: which wins now, the median or the Gaussian? Why?
C7. Thresholding at 120 after noise of 40 grey levels: read the wrong-pixel counts with no smoothing, with a Gaussian of σ = 2 and with a median of radius 2. Why do both help so much, although their PSNR on the card was not much better than the noisy input?
The model solution is pdesmooth/src/smoothers/solution.rs in the download.
C5. The noisy card has 24.6 dB. Gaussian (best σ = 0.5): 27.0 dB, line kept 0.75; its method noise shows the outlines of every edge, the discs, the lattice and the stripes: it believes the image is smooth and blurs all structure a little. Median (radius 1): 24.1 dB, worse than the input, line kept 0.03: its method noise shows the lattice dots, the stripes and the line in full, because they are thinner than the window. Haar shrinkage (best t = 30 grey levels): 30.2 dB, line kept 0.87: few large coefficients carry the card’s edges. Here it is helped by luck: the card’s dots and stripes start on even pixels and fit the 2 \times 2 blocks exactly.
C6. The median: 24.2 dB against 22.5 for the Gaussian (noisy: 17.5). Isolated outliers never reach the middle of the sorted window, so the median ignores them; a Gaussian spreads every outlier into a grey blot. (The median still removes the line and the lattice, as in C5.)
C7. 1835 wrong pixels without smoothing, 589 after the Gaussian, 539 after the median. A threshold only asks on which side of 120 a pixel lies: strong smoothing removes the speckle in the large flat regions, and it does not matter much that small structures and edges suffer, since they are few pixels. The best settings for a score depend on the question (slide “Best PSNR, wrong look”).
Where this leads
The day’s methods all smooth with a hand-made belief. Learned denoisers (the deconvolution day’s pointer in denoise, and the generative-model days: diffusion priors) learn the belief from data; they are judged with exactly the same tools as here: a test card with truth, method noise, feature scores, and the rule that measurements come from the raw image.
The sparse representations day, next, adds one more hand-made belief, “few large coefficients in a basis or a patch dictionary”, and uses it beyond denoising: deblurring, inpainting and measuring less. Much later, heat flow returns inside a cell-segmentation network as its training target (flow fields to objects).