Lesson 4 / 26
Filtering and Denoising
Smooth noise with small neighbourhood operations.
Kernels over neighbourhoods
A filter replaces each pixel with a function of its neighbourhood. A Gaussian blur takes a weighted average (nearer pixels count more), reducing random noise but also softening edges. A median filter takes the median, which removes salt-and-pepper noise while preserving edges better. Bilateral and non-local-means filters smooth while keeping edges sharp. Blurring is also used before edge detection and downsampling to avoid amplifying noise or creating aliasing.
Filters, edges, regions, keypoints
Hand-designed operations remain fast, explainable tools and are often the first step in a pipeline.
Gaussian versus median blur on a noisy photo, run
I ran this on CPU with Python 3, OpenCV 5.0.0, scikit-image 0.26.0, PyTorch 2.14.1 and torchvision 0.29.1, using scikit-image's bundled sample photos and torchvision's published pretrained weights. Adding Gaussian noise gives a mean absolute error of 19.0 against the clean photo. A 5x5 Gaussian blur reduces it to 8.0 and a 5x5 median blur to 8.3; for this kind of noise they perform similarly.
import cv2, numpy as np
from skimage import data
img = data.camera().astype(np.float32)
rng = np.random.default_rng(0)
noisy = np.clip(img + rng.normal(0, 25, img.shape), 0, 255).astype(np.float32)
for name, out in [("noisy", noisy),
("Gaussian blur 5x5", cv2.GaussianBlur(noisy, (5, 5), 0)),
("median blur 5x5", cv2.medianBlur(noisy.astype(np.uint8), 5).astype(np.float32))]:
print(f"{name:<18} mean abs error vs clean image: {np.abs(out - img).mean():.1f}")
Output:
noisy mean abs error vs clean image: 19.0 Gaussian blur 5x5 mean abs error vs clean image: 8.0 median blur 5x5 mean abs error vs clean image: 8.3
Match the filter to the noise
Use median filters for impulse (salt-and-pepper) noise and Gaussian filters for sensor-like random noise.
Quick check: What is a trade-off of Gaussian blurring?
- It sharpens every edge
- It reduces noise but also softens edges
- It changes image size
- It converts the image to colour
Answer
It reduces noise but also softens edges — Smoothing removes fine detail too.