Introduction
Every camera adds noise. In dim light the sensor's random fluctuations become visible grain; a faulty sensor pixel or a transmission error produces isolated white or black dots. Noise confuses later steps such as edge detection and thresholding, so cleaning it up is often the first stage of a vision pipeline. The trick is to remove noise without destroying the detail you care about. This lesson compares three filters that do this in different ways.
How it works
Two kinds of noise.
- Gaussian noise: every pixel is off by a small random amount. The image looks grainy.
- Salt-and-pepper noise: a few pixels are completely wrong, stuck at 255 (salt) or 0 (pepper).
Mean filter (box blur). Replace each pixel by the average of its 3 × 3 neighbourhood. Random errors partly cancel out, so Gaussian noise drops. But a single salt pixel of 255 among neighbours of 100 does not cancel: it raises all nine averages around it to about 117, turning one dot into a blotch. Edges are also smeared, because pixels on both sides are averaged together.
Gaussian blur. A weighted average where close neighbours count more. The weight at distance i is
w(i) = exp(−i² / (2σ²))
then all weights are divided by their sum so they add up to 1. Sigma (σ) sets the strength: a larger σ spreads the weights and blurs more. A common rule takes a kernel radius of about 3σ. The 2-D Gaussian is the product of a horizontal and a vertical 1-D Gaussian, so it is separable: blur every row with the 1-D kernel, then every column. For a k × k kernel this needs 2k multiplications per pixel instead of k². The small integer kernel [1 2 1] / 4 (and its 2-D version [1 2 1; 2 4 2; 1 2 1] / 16) is a good approximation for small σ.
Median filter. Sort the nine values in the neighbourhood and take the middle one (index 4). One or two extreme values end up at the ends of the sorted list and are simply ignored, so salt-and-pepper noise disappears completely. At an edge, the middle value comes from whichever side holds the majority, so the edge stays sharp. The median is slower (it sorts) and is not a weighted sum, so it is not a convolution.
Choosing. Grainy noise: Gaussian blur. Isolated wrong pixels: median. Need to keep edges very sharp: median, or edge-preserving filters such as the bilateral filter.
Example
img = [
[100, 100, 100, 100, 100],
[100, 255, 100, 100, 100],
[100, 100, 100, 100, 100],
[100, 100, 100, 0, 100],
[100, 100, 100, 100, 100],
]
def window(img, r, c):
return [img[r + i][c + j] for i in (-1, 0, 1) for j in (-1, 0, 1)]
def mean_filter(img):
return [[(sum(window(img, r, c)) + 4) // 9 for c in range(1, 4)] for r in range(1, 4)]
def median_filter(img):
return [[sorted(window(img, r, c))[4] for c in range(1, 4)] for r in range(1, 4)]
print("window around row 2, column 2:", window(img, 2, 2))
print("sorted, middle value is", sorted(window(img, 2, 2))[4])
mean = mean_filter(img)
median = median_filter(img)
print("mean filter, inner 3 x 3: ", mean)
print("median filter, inner 3 x 3:", median)
print("pixels not 100 after the mean: ", sum([v != 100 for row in mean for v in row]))
print("pixels not 100 after the median:", sum([v != 100 for row in median for v in row]))Line by line
- The 5 × 5 image is flat grey (100) with one salt pixel (255) at row 1, column 1 and one pepper pixel (0) at row 3, column 3.
windowcollects the 3 × 3 neighbourhood of a pixel as a list of nine values.- The window around the centre holds both bad pixels. Sorted, they sit at the two ends, and the middle value is a clean 100.
- The mean filter adds 4 before dividing by 9 to round to the nearest whole number. Near the salt pixel the mean is 117, near the pepper 89, and where both are in the window, 106: the noise has spread to 7 of the 9 pixels.
- The median filter returns 100 everywhere: both bad pixels are gone and nothing else changed.
Expected result
window around row 2, column 2: [255, 100, 100, 100, 100, 100, 100, 100, 0]
sorted, middle value is 100
mean filter, inner 3 x 3: [[117, 117, 100], [117, 106, 89], [100, 89, 89]]
median filter, inner 3 x 3: [[100, 100, 100], [100, 100, 100], [100, 100, 100]]
pixels not 100 after the mean: 7
pixels not 100 after the median: 0
In the real world
Phones combine several frames and apply smart blurs to clean up night photos. Scanned documents and old films are cleaned with median filters, and Gaussian blur is the standard first step before edge detection, because it stops the edge detector from reacting to noise. In OpenCV (shown as text; it does not run on this site):
import cv2
box = cv2.blur(img, (3, 3))
gauss = cv2.GaussianBlur(img, (5, 5), 1.0) # 5 x 5 kernel, sigma 1
median = cv2.medianBlur(img, 3) # 3 x 3 median
Common mistakes
- Using a mean filter on salt-and-pepper noise, which spreads each bad pixel into a blotch.
- Blurring too much. A large σ removes noise and detail together; check the edges you need.
- Forgetting to normalise a Gaussian kernel, which changes the overall brightness.
- Filtering in place, so later windows read already-filtered values.
- Taking the median of an even number of values without deciding which middle to use.
Best practices
- Look at the noise first: grain calls for a Gaussian, specks call for a median.
- Start with a small kernel (3 × 3) and increase only if needed.
- Use the separable form for Gaussian blurs; it is much faster for large kernels.
- Measure: compare the filtered image with a clean reference when you have one.
Summary
Noise is random error in pixel values: small everywhere (Gaussian) or large at a few pixels (salt and pepper). Mean and Gaussian filters average it away but blur edges and spread specks; a Gaussian weights nearer pixels more and can be applied as two 1-D passes. The median filter takes the middle of the sorted neighbourhood, removing specks completely while keeping edges sharp.
Quick check
Key takeaways
- Averaging filters reduce random noise but spread single bad pixels into blotches and blur edges.
- The median ignores extreme values, so it removes salt-and-pepper noise and keeps edges sharp.
- A Gaussian blur weights near pixels more than far ones; it is separable, so two 1-D passes replace one 2-D pass.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.