Sub topic 5
Matching the Filter to the Noise
Assume you know basic Python, NumPy array indexing and dtypes, pixels, grayscale images, and how to read images with OpenCV. This chapter focuses on choosing a smoothing filter after identifying the noise in an image.
Noise is unwanted variation in pixel values. A filter reduces noise by replacing each pixel with a value calculated from nearby pixels. The best filter depends on how the noise is distributed and whether edges must remain sharp.
Identify the noise first
Inspect both the image and its histogram. Zoom in on flat regions, such as a clear sky or a wall. Noise often appears as:
- Gaussian-like noise: small, random brightness changes spread across the image.
- Salt-and-pepper noise: isolated black or white pixels caused by impulse errors.
- Speckle noise: grain that changes with the brightness of the underlying surface.
- Uneven lighting: a slow brightness change across the image. This is not random noise, so smoothing alone may not fix it.
Use a small test region when comparing filters. A filter that looks good on a textured object may erase useful detail elsewhere.
| Noise or goal | First filter to try | Main trade-off |
|---|---|---|
| Small random brightness changes | Gaussian blur | Softens edges and fine texture |
| Isolated black or white pixels | Median blur | Can remove thin lines and small objects |
| Simple general smoothing | Box blur | Fast, but often produces less natural blur |
| Noise near important edges | Bilateral filter | Preserves edges, but is slower and parameter-sensitive |
How the filters behave
A box filter gives every pixel in its neighborhood equal weight. It is fast and predictable, but sharp edges become visibly soft.
A Gaussian filter gives nearby pixels more weight than distant pixels. It usually produces smoother, more natural results for Gaussian-like noise. The kernel size must be positive and odd, such as (5, 5). A larger kernel removes more noise but also removes more detail.
A median filter sorts the neighborhood values and selects the middle value. An extreme pixel therefore has little influence. This makes median filtering a strong choice for salt-and-pepper noise. It is less suitable when fine lines or small bright features are important.
A bilateral filter considers both spatial distance and intensity difference. Pixels that are close and have similar brightness influence one another. Pixels across a strong edge have less influence, so edges can remain clearer. Bilateral filtering can be slow on large images, and large values for sigmaColor or sigmaSpace can create flat-looking regions.
import cv2
image = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
if image is None:
raise FileNotFoundError("input.jpg could not be read")
box = cv2.blur(image, (5, 5))
gaussian = cv2.GaussianBlur(image, (5, 5), 0)
median = cv2.medianBlur(image, 5)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)
outputs = {
"box": box,
"gaussian": gaussian,
"median": median,
"bilateral": bilateral,
}
for name, result in outputs.items():
cv2.imwrite(f"{name}.png", result)A practical selection process
Compare results at the same display scale and measure the effect on the later task. A filter that looks attractive may reduce edge detection or threshold accuracy. Blur before Canny edge detection to suppress false edges, but avoid blurring so much that real boundaries disappear.
Exercise
An image contains isolated white pixels on a dark background, and thin dark lines must remain visible. Which filter should you try first, and what risk should you check?
Show answer
Try median blur first. Check whether its kernel is large enough to erase the thin lines or small objects you need.