Opencv notes—mean filter, Gaussian filter, median filter simple understanding and application

Image noise:

  • Salt and pepper noise (impulse noise):, 随机出现的噪声may be caused by sudden strong interference of the image signal, analog-to-digital converter or bit transmission error. For example, a failed sensor causes the pixel value to be the minimum value, and a saturated sensor causes the pixel value to be the maximum value.
  • Gaussian noise: 噪声密度函数服从高斯分布a type of noise. Due to the mathematical ease of Gaussian noise in the space and frequency domain, this noise (also called normal noise) model is often used in practice. The probability density function of Gaussian random variable z is given by:

Insert picture description hereMean filter:
Use the mean filter template to filter out image noise. Let Sxy denote the coordinate group of the rectangular sub-image window with the center at the point (x, y) and the size of m×n. Completed by a normalized convolution box. It is just a 平均值来代替中心元素
Insert picture description herecalling method of covering all pixels in the area with a convolution box :

cv.blur(src, ksize, anchor, borderType)
src::图像
ksize:卷积核大小
anchor:默认值(-1-1),表示核中心
borderType:边界条件

Gaussian filter:
Two-dimensional Gaussian filter is the basis for constructing Gaussian filter. The probability distribution function is as follows. The normal distribution is a kind of bell-shaped curve. When calculating the smoothing result, you only need to use the "center point" as the origin, and assign weights to other points according to their positions on the normal curve, and you can get a weighted average.
Insert picture description hereCalling method:

cv2.GaussianBlur(src,ksize,sigmaX,sigmay,borderType)
 - src: 输入图像 
 - ksize:高斯卷积核的大小,注意 : 卷积核的宽度和高度都应为奇数,且可以不同
 - sigmaX: 水平方向的标准差
 - sigmaY: 垂直方向的标准差,默认值为0,表示与sigmaX相同 borderType:填充边界类型

Median filtering: The
basic idea is to use the 像素点邻域灰度值的中值来代替该像素点的灰度值
calling method:

cv.medianBlur(src, ksize )
src:输入图像
ksize:卷积核的大小

Three kinds of filtering implementation codes:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# 画图配置
plt.rcParams['font.sans-serif']=['SimHei']      # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False        # 用来正常显示负号

# 图像载入
img = cv.imread('img/img7.png')
# 均值滤波
img1 = cv.blur(img,(5,5))
# 高斯滤波
img2 = cv.GaussianBlur(img,(3,3),1)
# 中值滤波
img3 = cv.medianBlur(img,5)
# 图像显示
fig, axes = plt.subplots(nrows=2,ncols=2,figsize=(10,8),dpi=100)
axes[0][0].imshow(img[:,:,::-1])
axes[0,0].set_title("原图")
axes[0][1].imshow(img1[:,:,::-1])
axes[0,1].set_title("均值滤波")
axes[1][0].imshow(img2[:,:,::-1])
axes[1,0].set_title("高斯滤波")
axes[1][1].imshow(img3[:,:,::-1])
axes[1,1].set_title("中值滤波")
plt.show()

Running results:
Insert picture description herecv Xiaobaiwang big guy pointing

Guess you like

Origin blog.csdn.net/weixin_45666249/article/details/115044875