Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 54 ยท Image Processing

Blurring and Noise Reduction

Compare OpenCV's box, Gaussian, median and bilateral blurs, learn why kernel sizes are odd, and rebuild each filter in numpy to see how they treat noise.

Real images are noisy. Sensors add grain, JPEG compression adds blocks, and old scans collect dust. Smoothing, also called blurring, replaces each pixel with a value computed from its neighbors, which averages some of the noise away. It is usually the first step before thresholding, edge detection or feature detection, because those steps react strongly to every stray speck.

OpenCV offers four everyday smoothing functions, and each makes a different trade between removing noise and keeping edges. Keep in mind that cv2 cannot run on this site: the OpenCV calls below are read-only, and the live examples rebuild the filters in numpy so you can see exactly what they compute.

The four filters

Python
import cv2

img = cv2.imread("noisy.png")

box      = cv2.blur(img, (5, 5))
gauss    = cv2.GaussianBlur(img, (5, 5), 0)
median   = cv2.medianBlur(img, 5)
bilat    = cv2.bilateralFilter(img, 9, 75, 75)

Four ways to smooth

Box Plain average of the window. Fast, blurs edges.
Gaussian Weighted average, center counts most. The default choice.
Median Middle value of the window. Kills salt-and-pepper.
Bilateral Averages only similar-looking neighbors. Keeps edges.

Box, Gaussian and median use one window size. Bilateral adds two more settings.

1 / 2

All of them slide a small window, the kernel, over the image. The first three have a single size parameter. The bilateral filter additionally takes a color sigma and a space sigma, which we will meet shortly.

Why kernel sizes are odd

The window must have a center pixel, because the output replaces the pixel in the middle. A 3 by 3 or 5 by 5 window has one; a 4 by 4 window does not. That is why GaussianBlur and medianBlur require odd sizes (medianBlur takes a single integer; GaussianBlur takes a (width, height) tuple of odd numbers or zero, in which case it derives the size from sigma). cv2.blur accepts any size but you should follow the odd rule anyway, otherwise the result shifts by half a pixel. The kernel size is also a strength control: a bigger window averages more pixels and blurs more.

Convolution in numpy

The box and Gaussian filters are both convolutions with a kernel of weights that add up to 1. The next lesson covers convolution in depth. For now, here is a short, slow, readable version. It pads the image by repeating border pixels, multiplies each window by the kernel and sums.

For the Gaussian, the weights come from the bell curve exp(-xยฒ / 2ฯƒยฒ) along each axis, normalized, and the 2D kernel is the outer product of the 1D one with itself. When you pass sigma = 0, OpenCV derives it from the size as 0.3 * ((ksize - 1) * 0.5 - 1) + 0.8. For sizes 1, 3, 5 and 7 with sigma 0 it uses built-in tables of similar values, so the general formula matches only approximately for those small sizes.

Python Box and Gaussian kernels, applied by hand

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 impulse test is a handy trick: blur a single bright pixel and the output is the kernel (scaled by the pixel value). Box blur spreads it evenly, while the Gaussian keeps the strongest share at the center, which is why it looks smoother and has fewer blocky artifacts. A useful property is that a 2D Gaussian splits into two 1D passes (one along rows, one along columns), which is how OpenCV makes large Gaussian blurs fast.

Salt-and-pepper noise and the median filter

Salt-and-pepper noise flips random pixels to pure black or pure white. Averaging filters handle it poorly: one white pixel of 255 inside a window of dark values drags the average up, and the speck becomes a gray smudge instead of disappearing. The median filter sorts the window values and takes the middle one. An outlier ends up at an end of the sorted list and is ignored, and a median also never invents a value that was not in the window, so edges stay sharper.

Python Which filter removes salt-and-pepper 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.

The mean squared error drops from 1970.7 to 597.2 for the box and 554.5 for the Gaussian, but to 0.0 for the median: with only 62 isolated specks, every 3 by 3 window has a majority of clean pixels, so the median restores the image exactly and the edge stays a clean step. Denser noise would not be repaired this perfectly. The median still has a cost: it is slower than a box blur, and with a large window it can erase thin lines and sharp corners. For OpenCV's medianBlur, the 8-bit input with sizes 3 and 5 is the efficient case, and larger sizes take noticeably more time.

The bilateral filter

A Gaussian blur treats all neighbors by distance only. It therefore blurs across edges, which softens the picture's most important details. The bilateral filter adds a second weight: a neighbor counts less when its value is different from the center. Pixels on the other side of an edge have very different values, so they barely contribute, and the edge survives while flat areas are still smoothed.

Python
bilat = cv2.bilateralFilter(img, d=9, sigmaColor=75, sigmaSpace=75)

d is the neighborhood diameter (if it is not positive, OpenCV computes it from sigmaSpace). sigmaSpace controls how far away a pixel may be and still count, and sigmaColor controls how different its value may be: a larger value lets more dissimilar pixels mix. The price is speed, since weights must be computed for each pixel individually; it is much slower than a Gaussian, so use it when edge-preserving matters, such as a cartoon effect or before segmentation.

Python Bilateral versus Gaussian across an edge

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 "edge step" is the brightness difference across the boundary. The Gaussian drops it from 179.6 to 93.5 because it mixes both sides. The bilateral keeps it at 178.8 while lowering the flat-area noise from 6.96 to 1.85, nearly as much as the Gaussian (1.66). Note that setting the color sigma huge turns the bilateral filter back into a plain Gaussian, which is how the second function above is built.

Choosing a filter

  • Fine grain on a photo, nothing special: GaussianBlur with a 3 by 3 or 5 by 5 kernel.
  • Isolated black and white specks (dead pixels, scanner dust, transmission errors): medianBlur.
  • You need to keep edges, and speed is not critical: bilateralFilter.
  • Quick preview or heavy downsizing: blur, or better, resizing with INTER_AREA.
  • Do not blur more than you need. Blurring cannot be undone, and it removes detail that later steps might need.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do GaussianBlur and medianBlur require odd kernel sizes?

    The output replaces the pixel at the middle of the window. Only odd sizes have exactly one middle pixel.
  2. 2. An image has isolated pure white and pure black pixels scattered over it. Which filter is the best first choice?

    Extreme outliers pull an average toward them but are ignored by the median, which picks the middle sorted value.
  3. 3. What does the bilateral filter weight by, in addition to distance?

    A neighbor with a very different value, such as one across an edge, receives a tiny weight, which is why edges stay sharp.
  4. 4. When you blur a single bright pixel on a black background with a normalized kernel, what do you get?

    This is the impulse response. Convolving an impulse with a kernel reproduces the kernel, scaled by the impulse height.

Summary

  • Smoothing replaces each pixel by a value from its neighborhood; bigger odd kernels blur more.
  • blur is a plain average, GaussianBlur a weighted average (the usual default), medianBlur takes the middle value and removes salt-and-pepper noise, bilateralFilter keeps edges but is slower.
  • Box and Gaussian blurs are convolutions with a kernel whose weights sum to 1; the Gaussian is separable into two 1D passes.
  • With sigma = 0, the Gaussian sigma is derived from the kernel size.
  • Choose the weakest smoothing that solves the problem.

Next up: Kernels and filter2D.