Filter blur of image processing algorithm (based on OpenCV)


1、 Linear filtering

1.1 mean filtering

As the name suggests, the average value of the target pixel and the surrounding pixels is calculated instead of the original pixel value. The figure below shows a 3 × 3 filter template

void cv::boxFilter(

1.2 Gaussian filtering

The most commonly used filter

Let’s start with Gaussian noise

Gaussian noise

Gaussian noise refers to the noise whose probability density function of amplitude obeys Gaussian distribution. If its power spectral density obeys uniform distribution, it is Gaussian white noise.

The main source of Gaussian noise in digital image appears during the acquisition period. Sensor noise due to poor lighting and / or high temperature. In digital image processing, spatial filter can be used to reduce Gaussian noise, but when the image is smoothed, the result may lead to the blurring of fine scaled image edges and details, because they also correspond to the blocked high frequency.

Gauss function

This is a two-dimensional Gaussian function


Gaussian filtering in spatial domain uses discrete window (convolution kernel) sliding image for convolution operation, while Fourier transform is needed in frequency domain.

Compared with the mean filter, Gaussian filter weights the average image, and the weighted coefficient decreases with the distance from the core center.

The figure below shows a Gaussian filter template


(1) In general, the edge direction of an image is not known in advance, so it is impossible to determine that one direction needs more smoothing than the other before filtering. Rotational symmetry means that the Gaussian smoothing filter will not be biased in subsequent edge detection In either direction

(2) Gaussian function is a single valued function. This shows that the Gaussian filter uses the weighted mean value of the neighborhood of a pixel to replace the pixel value of the point, and the weight of each neighborhood pixel increases and decreases monotonously with the distance between the point and the center point. This property is very important, because the edge is a local feature of the image. If the smoothing operation is carried out, the pixel far away from the center of the operator will still be greatly affected If it is used, the smoothing operation will distort the image

(3) The Fourier transform spectrum of Gaussian function is single lobe. This property is a direct inference of the fact that the Fourier transform of Gaussian function is equal to the Gaussian function itself. The image is often polluted by unwanted high-frequency signals (noise and fine texture). The desired image features (such as edges) contain both low-frequency and high-frequency components Lobes mean that the smoothed image is not contaminated by unwanted high frequency signals, while retaining most of the desired signals

(4) The width of Gaussian filter (which determines the degree of smoothness) is represented by the parameter σ, and the relationship between σ and the degree of smoothness is very simple. The larger the σ is, the wider the band of Gaussian filter is, and the higher the degree of smoothness is (closer to the mean filter). By adjusting the parameter σ of the degree of smoothness, the image features can be over blurred (over smooth) and smoothed due to noise and fine texture There is a trade-off between too many undesirable abrupt variables (under smooth)

(5) Because of the separability of Gaussian function, large size Gaussian filter can be realized effectively. Separable filter can transform multi-dimensional convolution into multi-dimensional convolution. Specific to the two-dimensional Gaussian filtering, that is, first do one-dimensional convolution on the row, and then do one-dimensional convolution on the column. In this way, the computational complexity can be reduced from O (MMNN) to o (2mmn). M and N are the window sizes of image and filter respectively. Therefore, the computational complexity of the two-dimensional Gaussian filter increases linearly with the width of the filter template rather than squarely.

Opencv function:

void cv::GaussiBlur(

2、 Nonlinear filtering

2.1 median filtering

Each pixel is replaced by the “median” pixel in all the surrounding pixels, which can eliminate the outliers with abnormal values (too large or too small), such as salt and pepper noise. Gaussian filter and mean filter can not eliminate salt and pepper noise well, and can only soften it to a certain extent. Using median filter can get better filtering effect

Because it is nonlinear, it cannot express its template explicitly.

Opencv function:

medianBlur(inputArray src,OutputArray dst,int ksize);

2.2 bilateral filtering

Gaussian filtering only considers the spatial position of the pixel (Euclidean distance from the center) to determine its weighting coefficient, so Gaussian filtering will destroy the edge information and blur the image edge. Bilateral filtering is based on Gaussian filtering, considering the gray (color) intensity difference to correct the weight.

The weight is composed of two parts, one is equal to the weight of Gaussian filter (coordinate space); the second part is also the form of Gaussian weight, but the distance difference in the Gaussian weight is replaced by the gray intensity difference (color space). The expression formula is as follows:


Where K and L are the central coordinates of the template, I and j are other positions of the template, D (I, J, K, l) and R (I, J, K, l) are the first part and the second part respectively, and the multiplication result is the form on the right side of the formula, f (x, y) represents the gray value at (x, y), sigma_ D and sigma_ R is the Gauss standard deviation of coordinate space and color space.

Note: Generally speaking, it is not necessary to use the Gaussian function, but the Gaussian distribution function is used in the implementation of bilateral filtering in OpenCV

Opencv function:

void cv::bilateralFilter(

The size of D has a great impact on the efficiency of the algorithm, and it is generally set to a value of no more than 5 when it requires high speed.

Experimental code and results

int main()

The following are four kinds of filtering results compared with the original image, the left side is the original image.



Recommended Today

DOS file delete del command

The use of del command is very simple, just use the format of “del file path”. For example, if you want to delete the test.txt , using del D:\ test.txt That’s it.However, not all files can be done with del command, such as files with hidden attribute, read-only attribute or system attribute\ test.txt It has […]