Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Cleaning and outlining pictures
Many image tasks start with simple, local fixes. Blurring averages each pixel with its neighbours to hide noise. A median filter takes the middle value of the neighbourhood, so isolated white and black specks disappear but sharp boundaries survive. Edges are where brightness changes quickly, so measuring the local slope outlines objects. For black and white masks, ‘erosion’ shaves a pixel off every shape and ‘dilation’ adds one; doing one after the other removes specks or fills small holes. The demo lets you add noise and try each operation.
Star trackers, crater detectors and runway-edge finders on aircraft still run classical filters on board: they are fast, predictable and easy to certify.
- 1. A median filter is best against…
- 2. An edge is where brightness…
- 3. Erosion of a binary shape…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Linear filtering: output = kernel-weighted sum of the neighbourhood (cross-correlation; convolution flips the kernel). Borders need a rule: zero, reflect, replicate or wrap.
- Smoothing: box filter (mean), Gaussian with standard deviation σ (truncated at about 3σ). The Gaussian is separable, so a k × k filter costs 2k instead of k² multiplies; box sums cost four lookups in an integral image (Crow, 1984; Viola and Jones, 2001).
- Non-linear filters: median and other rank filters remove impulse noise and keep edges; bilateral filtering (Tomasi and Manduchi, 1998) and non-local means (Buades et al., 2005) average only similar pixels.
- Derivatives and edges: Prewitt and Sobel kernels estimate the gradient; magnitude and direction follow. Marr and Hildreth (1980) used zero crossings of the Laplacian of Gaussian. Canny (1986): Gaussian smoothing, gradient, non-maximum suppression along the gradient, hysteresis thresholds.
- Sharpening: unsharp masking adds back img − blur; the Laplacian enhances fine detail and noise alike.
- Thresholding: global (Otsu, 1979, maximises between-class variance), adaptive (local mean or Gaussian), then connected components.
- Morphology (Matheron and Serra, 1960s): dilation and erosion with a structuring element; opening = erosion then dilation removes small bright specks; closing fills small holes; morphological gradient, top-hat, skeletons and the distance transform build on them.
- Tools: OpenCV, scikit-image, scipy.ndimage, Kornia (differentiable, on GPU).
- 1. A separable 11 × 11 Gaussian costs per pixel about…
- 2. Canny thins edges with…
- 3. Opening is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Building blocks in code
Exercise 1.
- Same-size 2-D filtering with reflect padding.
- Gaussian kernels.
- Sobel gradients.
- Binary erosion and dilation.
Practice.
- Convert to float before filtering; clip or rescale before converting back to uint8.
- Pick the smoothing scale from the noise level and the size of the smallest structure you must keep.
- Use the median for impulse noise (dead pixels, cosmic-ray hits), Gaussian or bilateral for sensor noise.
- For masks, choose the structuring element smaller than the thinnest feature to keep; compare with a majority (binary median) filter.
- Use cv2.Canny with automatic thresholds (for example from the median intensity) and check results at several scales.
import cv2, numpy as np
img = cv2.imread("runway.png", cv2.IMREAD_GRAYSCALE)
smooth = cv2.GaussianBlur(img, (0, 0), sigmaX=2)
edges = cv2.Canny(smooth, 50, 150) # hysteresis thresholds
clean = cv2.medianBlur(img, 3) # impulse noise
_, mask = cv2.threshold(clean, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
mask = cv2.morphologyEx(cv2.morphologyEx(mask, cv2.MORPH_OPEN, k), cv2.MORPH_CLOSE, k)
n, labels = cv2.connectedComponents(mask)- 1. Impulse noise from dead pixels is best removed by…
- 2. A structuring element larger than a feature will…
- 3. Before filtering a uint8 image, convert to…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Scale and noise. A derivative filter amplifies high frequencies, where white noise has as much power as anywhere else. Smoothing with a Gaussian of width σ before differentiating suppresses noise variance roughly as 1/σ⁴ for the gradient, while edge localisation error grows with σ and nearby structures merge. Canny’s criteria (detection, localisation, single response) formalise the trade-off; the best σ grows with the noise level.
Why the median works on impulses. A rank filter ignores outliers as long as fewer than half the window is corrupted, and a step edge is a fixed point of the median: on each side the majority of the window shares the true value. Linear filters instead spread each impulse over the whole kernel.
Morphology as set operations. Dilation is the Minkowski sum A ⊕ B and erosion the Minkowski difference A ⊖ B. Opening (A ⊖ B) ⊕ B is the union of all translates of B that fit inside A: anything narrower than B disappears. Opening and closing are idempotent and increasing, which is why they are called morphological filters.
- 1. The best smoothing scale for edges…
- 2. A step edge under a median filter…
- 3. Opening removes anything…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How should smoothing, edge detection and morphology be tuned to the noise?
History.
- G. Matheron and J. Serra, mathematical morphology (Fontainebleau, 1960s); J. Serra, Image Analysis and Mathematical Morphology (1982).
- D. Marr and E. Hildreth, “Theory of edge detection” (1980).
- J. Canny, “A computational approach to edge detection” (1986).
- N. Otsu, “A threshold selection method from gray-level histograms” (1979).
Exercise 2. A 96 × 96 scene: a bright rectangle, a mid-grey disk, a 4-pixel bar and a 2-pixel line on a dark background.
- Edges: Gaussian noise of standard deviation 0.1, 0.2 and 0.3; Sobel magnitude with non-maximum suppression, after no smoothing, Gaussian σ = 1, 2, 3 or a 3 × 3 median; best F1 over thresholds with 1-pixel tolerance.
- Denoising: PSNR after Gaussian σ = 1 or a 3 × 3 median, for Gaussian noise 0.2 and for 10% salt and pepper.
- Masks: threshold a noisy image (noise 0.15) and clean it with 3 × 3 opening, closing, opening then closing, or a 3 × 3 majority filter; count wrong pixels.
| noise | raw Sobel | σ = 1 | σ = 2 | σ = 3 | median 3 × 3 |
|---|---|---|---|---|---|
| 0.1 | 0.992 | 0.995 | 0.927 | 0.85 | 0.992 |
| 0.2 | 0.862 | 0.975 | 0.926 | 0.852 | 0.899 |
| 0.3 | 0.64 | 0.874 | 0.906 | 0.84 | 0.741 |
| denoising (PSNR, dB) | noisy | Gaussian σ = 1 | median 3 × 3 |
|---|---|---|---|
| Gaussian noise 0.2 | 13.9 | 22.4 | 20.6 |
| salt and pepper 10% | 14.7 | 21.9 | 28.7 |
| mask cleaning | threshold | open | close | open + close | majority 3 × 3 |
|---|---|---|---|---|---|
| wrong pixels | 931 | 502 | 1870 | 382 | 95 |
- The best smoothing scale grew with the noise: σ = 1 at noise 0.2, σ = 2 at noise 0.3 (seeds 1 and 2: 0.902 and 0.876 for σ = 2 against 0.824 and 0.866 for σ = 1). Over-smoothing cost about 0.15 F1 at low noise.
- Median versus Gaussian: each won on its own noise type, by 6.8 dB for the median on impulses and 1.8 dB for the Gaussian on Gaussian noise.
- Morphology was not the best mask cleaner here: closing alone more than doubled the errors, opening then closing removed most specks but erased the 2-pixel line entirely, and the 3 × 3 majority filter left 4× fewer errors while keeping 99% of the line. Seeds 1 and 2 gave the same ordering (258 and 284 errors for opening then closing, 93 and 103 for the majority).
- Caveat: oracle thresholds and a synthetic, piecewise-constant scene; real edges are blurred and textured.
Classical image processing in AI, robotics and aerospace
- Navigation: star trackers threshold and centroid star images; terrain-relative navigation (for example on NASA’s Mars 2020 lander, 2021) matches edge-like landmarks to maps.
- Earth observation: morphological profiles and top-hat filters extract buildings and roads; median filters remove speckle-like outliers before classification.
- Deep learning: fixed Sobel or Gaussian layers, anti-aliasing blur before striding and morphology on predicted masks remain common pre- and post-processing.
References
- J. Canny (1986). “A computational approach to edge detection.” IEEE TPAMI.
- D. Marr and E. Hildreth (1980). “Theory of edge detection.” Proc. Royal Society B.
- N. Otsu (1979). “A threshold selection method from gray-level histograms.” IEEE Trans. SMC.
- J. Serra (1982). Image Analysis and Mathematical Morphology. Academic Press.
- C. Tomasi and R. Manduchi (1998). “Bilateral filtering for gray and color images.” ICCV.
- A. Buades, B. Coll and J.-M. Morel (2005). “A non-local algorithm for image denoising.” CVPR.