CV Image Filtering and Blurring
Image filtering is the process of modifying an image by passing a small mathematical grid — called a kernel or filter — over every pixel. Filters reduce noise, sharpen details, detect edges, or blur an image. They are one of the oldest and most essential tools in computer vision.
What Is a Kernel?
A kernel is a small grid of numbers, usually 3×3 or 5×5. You slide this grid over the image, position it on each pixel, multiply the kernel values with the pixel values underneath, and sum the results. That sum becomes the new value for the center pixel.
How a Kernel Slides Over an Image
IMAGE (simplified 5×5): KERNEL (3×3): +----+----+----+----+----+ +---+---+---+ | 10 | 20 | 30 | 40 | 50 | | 1 | 1 | 1 | +----+----+----+----+----+ +---+---+---+ | 60 | 70 | 80 | 90 |100 | | 1 | 1 | 1 | +----+----+----+----+----+ +---+---+---+ |110 |120 |130 |140 |150 | | 1 | 1 | 1 | ÷ 9 +----+----+----+----+----+ +---+---+---+ |160 |170 |180 |190 |200 | +----+----+----+----+----+ Kernel placed on center pixel (130): Sum = 70+80+90 + 120+130+140 + 170+180+190 = 1170 New center pixel = 1170 ÷ 9 = 130 (average of neighbors) Move kernel one step right → repeat for next pixel.
This operation — sliding and computing — is called convolution, and it forms the mathematical heart of both classical and deep learning-based computer vision.
Types of Filters
1. Box Filter (Average Blur)
The box filter replaces each pixel with the average value of its neighbors. All kernel weights are equal (usually 1/9 for a 3×3 kernel). The result is a uniformly blurred image.
Box Kernel (3×3): +-------+-------+-------+ | 1/9 | 1/9 | 1/9 | +-------+-------+-------+ | 1/9 | 1/9 | 1/9 | +-------+-------+-------+ | 1/9 | 1/9 | 1/9 | +-------+-------+-------+ Effect: Each pixel becomes the average of itself and its 8 neighbors. Visual result: Slight blur, like looking through slightly foggy glass.
2. Gaussian Blur
The Gaussian blur gives more weight to pixels that are closer to the center and less weight to pixels farther away. This creates a smoother, more natural-looking blur than a box filter.
Gaussian Kernel (3×3, approximate): +-------+-------+-------+ | 1/16 | 2/16 | 1/16 | +-------+-------+-------+ | 2/16 | 4/16 | 2/16 | +-------+-------+-------+ | 1/16 | 2/16 | 1/16 | +-------+-------+-------+ Center gets weight 4/16 (most). Corners get weight 1/16 (least). Visual result: Smooth, soft blur — like slightly defocused camera lens. Common use: Removing noise before edge detection.
3. Median Filter
The median filter replaces each pixel with the median (middle value) of its neighbors rather than the average. This removes "salt-and-pepper" noise — random black and white dots — without blurring edges.
Salt-and-pepper noise example: Original pixel neighborhood values: [10, 15, 200, 12, 14, 11, 8, 220, 13] Sorted: [8, 10, 11, 12, 13, 14, 15, 200, 220] Median (middle value): 13 The noise spikes (200, 220) disappear. Average would give: (503 ÷ 9) = 55.9 → wrong! Median gives: 13 → correct.
4. Sharpening Filter
A sharpening filter does the opposite of blurring — it increases contrast between a pixel and its neighbors, making edges appear crisper and more defined.
Sharpening Kernel (3×3): +----+----+----+ | 0 | -1 | 0 | +----+----+----+ | -1 | 5 | -1 | +----+----+----+ | 0 | -1 | 0 | +----+----+----+ Center weight = 5 (amplifies the pixel itself). Neighbor weights = -1 (subtracts surrounding values). Net effect: Edges and fine details become more pronounced.
Why Blur Before Processing?
Raw images from cameras contain noise — tiny random variations in pixel values caused by the camera sensor, lighting, or compression. This noise confuses edge detection and feature extraction algorithms. Applying a blur first removes the noise and makes subsequent steps more accurate.
Processing Pipeline with Blur
[Raw Image with noise]
↓
[Gaussian Blur applied] ← Removes noise
↓
[Edge Detection] ← Now finds real edges, not noise edges
↓
[Feature Extraction] ← Cleaner, more accurate features
Kernel Size and Its Effect
A larger kernel considers more neighboring pixels and produces stronger blurring. A smaller kernel is more subtle. Choosing the right kernel size depends on how much noise you have and how much detail you want to preserve.
Kernel Size Comparison
| Kernel Size | Blur Strength | Detail Preserved | Use Case |
|---|---|---|---|
| 3×3 | Light | High | Slight noise removal |
| 5×5 | Medium | Medium | Moderate noise, real photos |
| 11×11 | Strong | Low | Heavy noise, background smoothing |
| 21×21 | Very strong | Very low | Background blur (portrait mode) |
Real-World Analogy: Photo Editing
SHARPENING FILTER → Like the "Sharpen" slider in photo editors.
Makes crisp product photos for online stores.
GAUSSIAN BLUR → Like the "Bokeh" (background blur) on phone cameras.
Focuses attention on the subject.
MEDIAN FILTER → Like "Remove Blemishes" in portrait apps.
Removes skin spots without smearing the skin tone.
Key Takeaways
- A kernel is a small grid of numbers slid over the image to transform it.
- Box blur averages neighbors equally — simple but creates harsh blur.
- Gaussian blur weights neighbors by distance — smoother, more natural result.
- Median filter removes salt-and-pepper noise without blurring edges.
- Sharpening kernels amplify the center pixel and subtract neighbors — increases edge contrast.
- Blurring before edge detection removes noise and improves accuracy.
