Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 46 ยท Filtering and Edges

Blurring and Denoising

Compare box, Gaussian and median filters on Gaussian and salt-and-pepper noise, measure the results with PSNR, and learn how a bilateral filter keeps edges.

Every real image contains noise: sensor grain in a dim room, compression artifacts, a few dead pixels. Blurring is the basic tool for getting rid of it. A blur replaces each pixel by a combination of its neighbours, and since noise is random from pixel to pixel while the picture itself usually changes slowly, the combination keeps the picture and cancels some of the noise. The price is detail: edges and fine texture get blurred too.

This lesson compares the three workhorse filters, the box filter, the Gaussian filter and the median filter, on two kinds of noise, measures how well each one recovers a clean image, and then builds a bilateral filter from scratch to see how an edge-preserving blur works. You should know how kernels slide over images from Convolution and Correlation, and where noise comes from in Resolution, Sampling and Noise.

Three filters, three ideas

All three filters look at a small neighbourhood around each pixel, but they combine it differently.

flowchart TD
    N["neighbourhood of a pixel"] --> A["Box: plain average<br/>all weights equal"]
    N --> B["Gaussian: weighted average<br/>weights fall off with distance"]
    N --> C["Median: sort the values<br/>take the middle one"]
    A --> L1["linear, fast, blurs edges"]
    B --> L2["linear, smooth, separable"]
    C --> L3["non-linear, removes outliers,<br/>keeps edges sharper"]

Box and Gaussian are weighted sums (convolutions). The median is not: it cannot be written as a kernel.

  • Box filter (mean filter). Every pixel in the window gets the same weight. It is the cheapest filter, but it treats a far-away neighbour as important as the pixel itself and leaves a blocky look. In SciPy: ndimage.uniform_filter.
  • Gaussian filter. Weights follow a bell curve: nearby pixels count more. The parameter sigma is the standard deviation of the bell in pixels and controls how strong the blur is. In SciPy: ndimage.gaussian_filter.
  • Median filter. Collect the values in the window, sort them, take the middle one. An extreme value (a white or black speck) never reaches the middle of a sorted list. In SciPy: ndimage.median_filter.

Box and Gaussian are linear: each output is a weighted sum, so they are convolutions and can be separable. The median is non-linear, which is exactly why it behaves so differently near outliers and edges.

Building a Gaussian kernel

A Gaussian kernel in one dimension is exp(-x^2 / (2 * sigma^2)) for integer offsets x, divided by its sum so that the weights add up to 1. A common choice is a radius of about three sigma, because the bell is almost zero beyond that distance.

Python The Gaussian kernel and the width of the blur

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A sigma of 1 needs 7 weights, and the centre weight (0.399) is the largest, falling away on both sides. A larger sigma doubles the radius and widens a blurred edge from 2 to 6 to 10 pixels for sigma 1, 2 and 4. Choosing sigma is therefore choosing how much detail you are willing to lose.

Gaussian noise versus salt-and-pepper noise

The two classic kinds of noise need different cures.

  • Gaussian noise nudges every pixel by a small random amount. Averaging works well, because the positive and negative nudges cancel.
  • Salt-and-pepper noise sets a few random pixels to the extremes (0 or 255) and leaves the rest untouched. Averaging handles it badly: one white speck (255) among eight dark pixels (value 60) raises the 3x3 average to 82, so each speck smears into a grey blob that is nine times larger.

To compare filters fairly we need a number. PSNR (peak signal-to-noise ratio) compares a result with the clean reference: 10 * log10(255^2 / MSE), where MSE is the mean squared difference. Higher is better, and every 3 dB roughly halves the error energy.

Python Which filter wins on which noise?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On salt-and-pepper noise the median filter is far ahead (27.7 dB for the 3x3 median against 20.6 dB for the 3x3 box and 21.3 dB for the Gaussian). On Gaussian noise the picture is closer: the linear filters help by several dB, and the 3x3 median is better still (25.0 dB). That last result deserves a caution. The test image is made of flat regions with sharp edges, which is the best case for a median; on a photograph with fine texture the Gaussian often wins on pure Gaussian noise. Pick the filter by the kind of noise, not by habit.

The larger 5x5 windows score worse than the 3x3 ones. They remove more noise but also destroy more of the real edges, which the PSNR counts as error. This is the central trade-off of denoising: more smoothing means less noise and less detail.

Tip: If you see isolated black and white dots, use a median filter first. If the image has a fine grain everywhere, a Gaussian with a small sigma (0.8 to 1.5) is the usual starting point. Judge by the output image, not only by one number.

Why the median keeps edges

Consider one row of an image: a dark region (10), a bright region (100), a white speck of 255 in the dark side and a black speck of 0 in the bright side. A mean filter turns each speck into a bump and softens the true step as well. A median ignores the specks and keeps the step exactly where it was.

Python Mean versus median on a signal with outliers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The mean output is 92 around the white speck and 67 around the black one, and the step turns into a ramp (40, 70, 100). The median output is exactly 10 ... 10 100 ... 100, with a clean step and no specks. The reason is simple: a window of three values can hold at most one speck, so the speck is always the largest or the smallest value and never the middle one. For a window of n values a median ignores up to (n - 1) / 2 outliers.

The median has costs: it is slower (sorting per window), it can erase thin lines that are only one or two pixels wide, and it rounds off sharp corners of small objects.

The bilateral idea: blur but respect edges

Linear filters blur across edges because they only look at distance. A bilateral filter adds a second question: how similar is this neighbour in brightness? Each neighbour's weight is the product of two Gaussians:

TEXT
weight = exp(-distance^2 / (2 * sigma_space^2)) * exp(-(brightness difference)^2 / (2 * sigma_range^2))

A neighbour on the other side of an edge differs strongly in brightness, so its range weight is nearly zero and it hardly contributes. Pixels inside a flat region are all similar, so they average each other and noise disappears. The result smooths flat areas and leaves edges sharp.

flowchart LR
    A["neighbour pixel"] --> B["spatial weight<br/>close = high"]
    A --> C["range weight<br/>similar brightness = high"]
    B --> D["multiply<br/>the two weights"]
    C --> D
    D --> E["weighted average<br/>of the window"]

A bilateral weight is high only when a neighbour is both close and similar.

The bilateral filter has no fast one-line SciPy version, but the idea is short enough to write directly. The loop below is slow for big images, but fine for a 24x24 demo. We compare it with a Gaussian of similar strength on a noisy vertical step edge.

Python A bilateral filter from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both filters cut the noise in the flat region from about 8 to about 2. The difference is at the edge. The Gaussian drags the edge across four pixels of intermediate grey and lowers the contrast between the two sides from 99 to about 94, while the bilateral result keeps the full contrast (99.0) and a step with no in-between pixels. The price of the bilateral filter is speed: it needs a weight computation per neighbour per pixel and cannot be made separable in a simple way. In practice you would use an optimised library implementation, and in this course the loop version is for understanding.

Note: Both bilateral parameters matter. If sigma_range is much larger than the noise and the edge height, the range weight is almost 1 everywhere and the filter behaves like a plain Gaussian. If it is much smaller than the noise, nothing is averaged. A good start is sigma_range of two to three times the noise level.

Poisson (shot) noise: the noise depends on the signal

Gaussian noise has the same spread everywhere. Real camera noise mostly does not. Light arrives as discrete photons, and the number counted by a pixel in a short exposure varies randomly even for a perfectly steady scene. That variation follows a Poisson distribution, whose variance equals its mean. Bright pixels are noisier in absolute terms (their standard deviation is the square root of the photon count), but darker pixels have worse relative noise (signal to noise ratio is the square root of the count). That is why photos taken in dim light look grainy: few photons, poor signal to noise.

Sensors add other noise too (read noise, fixed-pattern noise), but photon shot noise dominates in medical X-ray, astronomy, microscopy and low-light photography. Because the noise depends on the brightness, a single Gaussian filter strength is wrong everywhere. A common fix is a variance-stabilizing transform: the Anscombe transform 2 * sqrt(x + 3/8) turns Poisson data into data with a nearly constant noise level of about 1, so you can denoise with a Gaussian-noise method and transform back.

Python Poisson noise and the Anscombe transform

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The noise standard deviation grows as the square root of the level (2, 4, 8, 16, 32 for these means), yet after the Anscombe transform it is about 1 at every brightness (the approximation gets worse only for counts of one or two photons, where the Poisson distribution is least like a Gaussian). Denoising in the transformed domain treats dark and bright regions fairly.

Non-local means: average similar patches, not nearby pixels

The Gaussian averages neighbours and the bilateral filter averages neighbours with similar brightness. The non-local means (NLM) filter makes a bigger leap: to denoise a pixel, it looks at the small patch around it (say 5 by 5), then searches a larger window for patches that look like it, wherever they are, and averages the centre pixels of those patches, with weights w = exp(-d^2 / h^2) where d is the patch distance and h the filter strength. Natural images are full of repeated structure (brick rows, grass, edges running along a line), so there are plenty of similar patches, even far away, and averaging them cancels the noise without blurring the structure.

Python Non-local means on a small image

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

NLM reaches a lower error than the Gaussian and keeps the step crisp: across the edge the Gaussian result climbs gradually, while the non-local average jumps, because patches on the dark side only resemble other dark-side patches. The cost is time (every pixel compares a whole search window of patches) so library versions use integral images and approximations. Modern denoisers (BM3D, deep networks) are descendants of the same patch-similarity idea.

Choosing a filter

Situation First choice Why
Isolated black/white dots median (3x3) outliers never reach the middle of the sorted window
Fine sensor grain on a photo Gaussian, sigma about 1 smooth, fast, separable
Denoising while keeping edges bilateral (or non-local means) similarity-aware weights
Quick preview, large blur box, or Gaussian approximated by box filters cheapest
Preparing for edge detection Gaussian derivatives of noisy data amplify noise

Smoothing is also a standard first step before thresholding and before the gradient step in Gradients and Edge Detection. Blur before you downsample, too, to avoid the aliasing described earlier in the course. Whatever filter you pick, remember that denoising cannot recover information that noise has destroyed; it only makes a smart guess.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A scan has a few hundred isolated pure-white and pure-black pixels. Which filter should you try first?

    The median ignores outliers: a single extreme value in a window never becomes the middle value. Averaging filters smear each speck into a grey blob instead.
  2. 2. What does the sigma parameter of a Gaussian filter control?

    Sigma is the standard deviation of the Gaussian in pixels. A larger sigma gives a wider kernel and a stronger blur.
  3. 3. Why can't a median filter be written as a convolution kernel?

    Convolution computes weighted sums, which are linear. The median depends on the ordering of the values, so no fixed weights reproduce it.
  4. 4. What extra information does a bilateral filter use compared with a Gaussian filter?

    A bilateral weight multiplies a spatial Gaussian by a range Gaussian of the brightness difference, so neighbours across an edge get almost no weight.
  5. 5. You increase a median filter from 3x3 to 5x5 on a mostly clean image. What is the most likely effect?

    Larger windows remove more noise but erase thin lines and round corners, which is the standard denoising trade-off.

Summary

  • Blur filters replace each pixel with a combination of its neighbours. Noise tends to cancel out, but detail is lost as well, so every denoiser is a trade-off.
  • The box filter averages equally, the Gaussian weights neighbours by a bell curve controlled by sigma, and the median takes the middle of the sorted window.
  • Box and Gaussian are linear and fast (the Gaussian is separable). The median is non-linear.
  • Use a median for salt-and-pepper noise; averaging smears the specks. For Gaussian noise a Gaussian or median does well, depending on the image content.
  • PSNR measures how close a result is to the clean image: higher is better, but always look at the picture too.
  • A bilateral filter multiplies a distance weight and a similarity weight, so it smooths flat regions and keeps edges sharp, at a higher cost.

Next up: turn the idea of "difference between neighbours" into edge maps in Gradients and Edge Detection.