opencv mean smoothing blur, median smoothing medianBlur, Gaussian smoothing GaussianBlur(), bilateral smoothing bilateralFilter()


1. All coefficients of the mean smoothing sliding window of mean smoothing blur() are 1/(window height * window width).

dst=cv2.blur(src, ksize[, dst[, anchor[, borderType]]])

src: source image, the number of channels is not limited, the data type must be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F;
ksize: kernel size, window size, tuple type, element value can be even or odd;
anchor: anchor point, The default is (-1,-1), acting on the center point of the sliding window;
borderType: border processing type;

img_ret = cv2.blur(img,(3,3))

2. Median smoothing medianBlur()
median smoothing also uses a sliding window like mean smoothing, but it does not calculate some weighted sum in the sliding window, but uses the median of all pixel values ​​sorted in the sliding window of the original image. value as the pixel value of the new image.
dst=cv2.medianBlur(src, ksize[, dst])
src: source image, the number of channels can be 1, 3 or 4. When ksize is 3 or 5, the data type can be CV_8U, CV_16U, CV_32F. When using a larger ksize, the data type can only be CV_8U; ksize
: Kernel size, window size, integer type, odd value greater than 1;

img_ret = cv2.medianBlur(img,3)

3. Pixel value contrast
smoothing is a type of image filtering, which can be regarded as low-pass filtering. It will eliminate the high-frequency "signal" of the image and make the image look blurry and smoother. By changing the image pixel values ​​before and after the change, This smoothing effect can be more visually observed by drawing a curve.

4. Gaussian smoothingGaussionBlur()
Gaussian smoothing has different weights depending on the distance from the center point. This method seems to be more "conventional".

The curve of the Gaussian distribution can be seen (when μ=0):
when x=0, the value of f(x) is the largest. When x changes to both sides, the value of f(x) becomes smaller and smaller;
this curve is at x The two sides of =0 are symmetrical, f(-1)=f(1), f(-2)=f(2); the
larger σ, the flatter the curve, and the lower the value when x=0.

Two-dimensional Gaussian distribution is used in OpenCV.

Plot a two-dimensional Gaussian distribution with matplotlib:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig) 
X = np.arange(-3, 3.1, 0.1,dtype=np.float64).reshape(-1,1)
Y = np.arange(-3, 3.1, 0.1,dtype=np.float64)
mux,muy=0,0
sigmax,sigmay = 0.8,0.8
expont = -0.5*(((X-mux)/sigmax)**2 + ((Y-muy)/sigmay)**2)
Z=np.exp(expont)/(2*np.pi*sigmax*sigmay)
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.viridis)
plt.show()

Interface form of GaussianBlur():
dst=cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])

src: The number of channels is arbitrary, the actual processing is divided into channels; the image depth can only be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F; ksize:
tuple type, window size, width and height can be different, but must be a positive odd number; If set to 0, it is calculated based on sigma.
sigmaX: the standard deviation of the image in
the When 0 is passed in, sigmaX and sigmaX are calculated based on ksize;
borderType: border processing method;

Note : The first letter of GaussianBlur is a capital G, which is slightly different from most other OpenCV function names that use small camel case naming style.

Because the center point of the sliding window is used as the origin, in order to ensure that the weight of the center point (x, y) = (0, 0) is the maximum value, μ1 and μ2 are both set to 0 in OpenCV's Gaussian smoothing, so that when calling When using the Gaussian smoothing function, you only need to pass in σ1 (sigmaX) and σ2 (sigmaY).

img_ret = cv2.GaussianBlur(img,(11,11),0)
imshow(cv2.cvtColor(img_ret,cv2.COLOR_BGR2RGB))

Corresponding to the characteristics of the Gaussian distribution curve, when the sigma is larger, the value of the origin is smaller and the values ​​of the surrounding points are larger. This corresponds to the lower the weight of the center point on the image and the higher the weight of the surrounding points, so the larger the sigma is. The image becomes blurry.
5. Bilateral smoothing bilateralFilter()
mean, median, and Gaussian smoothing denoising is an "indifferent attack". All pixels are affected by the same weighting coefficient, so the edges of the image will also be affected during the smoothing process. (Where pixel values ​​suddenly change), the bilateral filtering to be introduced next can remove noise while maintaining the edges of the image, which is "noising edge preservation".

Interface form of bilateralFilter():
dst=cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])

src: 8bit or floating point type; 1 or 3 channels;
d: window size, if it is a non-positive number, it is calculated according to sigmaSpace; when d>5, the speed will be slower. When the noise is serious, d>=9 can be selected, but This is not suitable for time-sensitive processing;
sigmaColor: sigma parameter of brightness difference;
sigmaSpace: sigma parameter of spatial distance, acting on the X and Y (row, column) directions of the image at the same time;
borderType: border processing method;

Among the parameters of the sliding window size, the ksize parameter of Gaussian smoothing is a tuple whose width and height can be unequal, but the parameter d used for bilateral smoothing is an integer type, which also determines that the width and height of the sliding window are equal.

If d is less than 0, use sigma_space*1.5 and then round to get the radius. If d is greater than 0, first divide by 2 to get the radius, then compare the radius with 1 to get the larger value, and finally multiply by 2 and add 1, so The obtained d can be guaranteed to be an odd number not less than 3.

When the sigmaColor and sigmaSpace parameters are relatively small (<10), the smoothing effect is not very obvious. When the parameters are relatively large or the image after multiple smoothings will look cartoonish.

img_ret = cv2.bilateralFilter(img,7,25,25)
img_edge=cv2.Sobel(img_ret,cv2.CV_8U,1,0,ksize=3)

Where there is a sudden change in color (edge), Gaussian smoothing only preserves about half of the border, while bilateral smoothing preserves almost all of the border, and the average edge brightness of Gaussian smoothing is not as high as bilateral smoothing.

Compared with mean and median smoothing, Gaussian smoothing is more in line with "conventional" values. The closer the pixels in spatial distance are to the greater the weight used to calculate the value of new pixels. Mean smoothing, median smoothing and Gaussian smoothing will achieve indiscriminate smoothing of the entire image. A sliding window with a fixed coefficient acts on the entire image, so although the noise is processed in the smoothed image, the edge parts will also be weakened. Bilateral smoothing is based on the coefficients used in Gaussian smoothing multiplied by the Gaussian function of the pixel difference. The greater the pixel difference at the center point, the smaller the entire coefficient value. Finally, the effect of noise removal and edge preservation can be achieved.

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129767116