Image enhancement and filtering: A complete guide to Gaussian blur, median filtering, and histogram equalization
After adjusting the model late at night, the loss remains high, and edge detection frequently misses key contours. When I look back - the original image is densely packed with noise, or the contrast is so bad that even my mother can't recognize it? In the complete process of computer vision, image enhancement and filtering are often the "pre-savior": it can directly improve human readability, and can significantly improve the effects of downstream feature extraction and model training.
This article will take you through the most practical OpenCV algorithms, with reproducible code, a quick tuning table, and a 10-line code pipeline. After reading this, you will be able to easily solve 80% of daily image preprocessing problems.
📂 Stage: Stage 1 - Cornerstone of Image Processing (Traditional CV) 🔗 Related chapters: OpenCV 快速入门 · 边缘检测与轮廓提取
1. Basic preparation: all filtering ≈ sliding window operation with/without padding
Whether it is denoising, smoothing or enhancing details, the core logic behind it remains almost unchanged:
- Define a small matrix called "convolution kernel/template" (commonly used are 3×3, 5×5, and 7×7, and the side length is usually an odd number).
- Let this kernel slide through each pixel of the image (OpenCV uses
BORDER_DEFAULTSupplement the edges to ensure that the output size is consistent with the original image). - Calculate the pixel value in the kernel coverage area according to the rules and replace the center pixel.
📦 Simplified version without padding convolution demonstration
2. Linear filtering: a good helper for dealing with uniform Gaussian noise
The output of linear filtering is a weighted linear combination of neighborhood pixels, which is fast in calculation, but at the cost of edges being blurred. Simply put, it adds up the neighbors around each pixel according to a certain weight. The noise is averaged out, but the boundaries of the object also become "softer".
2.1 Gaussian blur (⭐️ The preferred smoothing method for beginners)
Gaussian blur uses a two-dimensional Gaussian function as a weight, and the pixels closer to the center have greater influence. It evenly removes Gaussian noise (such as film grain, electronic noise in low light) while retaining more detail than a simple mean filter.
2.2 Mean filtering (⚡️ only for introductory demonstration / ultra-fast batch blurring)
All neighborhood pixels have the same weight, which is the fastest to calculate, but also the most serious damage to edges. Generally it is only suitable for quickly blurring the background or unimportant areas.
3. Nonlinear filtering: intelligently retain edges and accurately remove noise
Nonlinear filtering does not follow a simple weighted summation rule. It "depends on the situation" - adaptive processing based on the color and distance differences between pixels. This way, flat areas are strongly smoothed, while near boundaries are carefully preserved.
3.1 Median filtering (😎 The exclusive nemesis of salt and pepper noise)
Replace the center pixel with the median of the neighborhood pixels. It can completely eliminate black and white salt and pepper noise (scratches in old photos, spots caused by transmission interference), and the edge preservation effect is much better than linear filtering. The principle is very simple: the gray value of salt and pepper noise is either 0 (black) or 255 (white), which is an extreme value. The median is naturally not affected by extreme values, so these noise points will be replaced by "normal" gray values in the neighborhood.
3.2 Bilateral filtering (✨ Portrait skin resurfacing / edge preservation artifact)
Bilateral filtering considers two factors at the same time:
- Spatial distance: The closer it is, the greater the weight.
- Color Distance: The closer the color, the greater the weight.
Therefore, smooth areas (face skin) can be severely blurred, while edge areas (eyebrow contours, hairline) are barely processed, achieving both denoising and detail preservation.
Bilateral filtering tuning cheat sheet:
4. Histogram technology: accurately enhance contrast
The image histogram shows the distribution of pixel values (0−255). Histogram equalization is to "stretch" the distribution that was originally concentrated in dark or bright parts to the entire grayscale range, making the image look more transparent and the details clearer.
4.1 Standard histogram equalization (simple and crude, but need to be used with caution)
It uniformly equalizes the entire picture, which is suitable for single-scene pictures that are overall dark or bright. The disadvantage is that it is easy to over-enhance local noise. Color images must first pull out the brightness channel separately and process it!
4.2 CLAHE (🎯 Contrast-limited Adaptive Equalization · Ultimate Recommendation)
CLAHE first divides the image into many small grids (default 8×8), performs equalization on each grid separately, and then uses bilinear interpolation to smoothly splice them.
Use at the same timeclipLimitLimit the improvement of local contrast, perfectly avoid over-enhancement, and all details in dark and bright parts can be clearly presented.
5. Practical combat: Universal image enhancement pipeline with 10 lines of code
Encapsulate commonly used enhancement and filtering methods into a class, use chain calls to quickly match and combine, and flexibly adapt to various daily preprocessing needs.

