Sub topic 4
Smoothing with Box, Gaussian, Median, and Bilateral Filters
Prerequisites
This chapter assumes basic Python, NumPy array indexing, grayscale images, and an OpenCV installation that can read images. Smoothing reduces small intensity changes so that later processing is less affected by noise.
A smoothing filter replaces each pixel with a value computed from nearby pixels. The nearby pixels are selected by a kernel, also called a filter window. A 5 × 5 kernel examines 25 pixels around each output location.
Four common smoothing filters
Box filtering
A box filter gives every pixel in the window the same weight. For a 5 × 5 window, it computes the average of 25 values. This reduces random noise, but it also blurs edges because pixels on both sides of an edge are averaged together.
Gaussian filtering
A Gaussian filter gives the greatest weight to the center pixel and gradually smaller weights farther away. It usually produces smoother and more natural-looking blur than a box filter. Gaussian smoothing is a common first step before edge detection because it reduces small noise without producing as many harsh artifacts.
Median filtering
A median filter sorts the values in the window and selects the middle value. It is especially useful for salt-and-pepper noise, which appears as isolated bright or dark pixels. Unlike an average, the median is not strongly affected by one extreme value. Median filtering can remove fine details when its window is too large.
Bilateral filtering
A bilateral filter considers both distance and intensity difference. Nearby pixels with similar intensity influence one another, while pixels across a strong edge have less influence. This can smooth flat regions while preserving object boundaries. Bilateral filtering is slower and its parameters are more sensitive than those of the other filters.
| Filter | Main strength | Common weakness |
|---|---|---|
| Box | Fast general averaging | Blurs edges strongly |
| Gaussian | Smooth, predictable noise reduction | Blurs edges |
| Median | Removes isolated impulse noise | Can remove small details |
| Bilateral | Preserves many edges | Slower and harder to tune |
import cv2
import numpy as np
img = cv2.imread("input.jpg") # BGR, uint8
if img is None:
raise FileNotFoundError("input.jpg could not be read")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# A hand-built averaging kernel and OpenCV's equivalent box filter.
kernel = np.ones((5, 5), np.float32) / 25.0
manual_box = cv2.filter2D(gray, -1, kernel)
box = cv2.blur(gray, (5, 5))
gauss = cv2.GaussianBlur(gray, (5, 5), 0)
median = cv2.medianBlur(gray, 5)
bilateral = cv2.bilateralFilter(gray, 9, 75, 75)
print("input:", gray.shape, gray.dtype)
print("box:", box.shape, box.dtype)
print("gaussian:", gauss.shape, gauss.dtype)
print("median:", median.shape, median.dtype)
print("bilateral:", bilateral.shape, bilateral.dtype)
cv2.imwrite("box.png", box)
cv2.imwrite("gaussian.png", gauss)
cv2.imwrite("median.png", median)
cv2.imwrite("bilateral.png", bilateral)The Gaussian kernel size must be positive and odd, such as (5, 5). A larger window creates stronger smoothing but removes more detail. For median filtering, 5 means a 5 × 5 neighborhood. In bilateral filtering, the final two values control how strongly spatial distance and intensity differences affect the result.
Choosing a filter
Use a Gaussian filter for general sensor noise and as a preparation step for edge detection. Use a median filter when the image contains isolated black or white specks. Use a bilateral filter when preserving visible boundaries matters more than processing speed. Use a box filter when you need simple, fast averaging and edge preservation is not important.
Exercise
An image contains isolated white pixels, and object boundaries must remain reasonably clear. Which filter should you try first, and why?
Show answer
Try a median filter first because it removes isolated extreme pixels without averaging them into nearby boundaries. If boundary preservation is still insufficient, compare its result with a bilateral filter.