``参考链接
#高斯卷积算子
def getGaussKernel(sigma,H,W):
#构建高斯矩阵,得到中心点位置
gaussMatrix = np.zeros([H,W],np.float32)
cH = (H-1)/2
cW = (W-1)/2
for r in range(H):
for c in range(W):
norm2 = math.pow(r-cH,2) + math.pow(c-cW,2)
gaussMatrix[r][c] = math.exp(-norm2/(2*math.pow(sigma,2)))
#计算高斯矩阵的和
sumGM = np.sum(gaussMatrix)
#归一化
gaussKernel = gaussMatrix/sumGM
return gaussKernel
#高斯卷积核旋转180度不变,可以分离一维垂直和一维水平
#opencv提供了一维垂直高斯卷积核
cv2.getGaussianKernel(int ksize,double sigma,int ktype=cv_64F)
# ksize 一维垂直方向上高斯核行数,正奇数
#标准差
#返回数据类型cv_32F或cv_64F(默认)
#高斯平滑
import numpy as np
from scipy import signal
import cv2
def gaussBlur(image,sigma,H,W,_boundary = 'fill',_fillvalue = 0):
#水平卷积核
gaussKenrnel_x = cv2.getGaussianKernel(sigma,W,)
#专置
gaussKenrnel_x = np.transpose(gaussKenrnel_x)
#图像矩阵与水平卷积核
gaussBlur_x = signal.convolve2d(image,gaussKenrnel_x,mode='same',boundary = _boundary,fillvalue = _fillvalue)
#垂直卷积核
gaussKenrnel_y = cv2.getGaussianKernel(sigma,H,)
gaussBlur_xy = signal.convolve2d(gaussBlur_x,gaussKenrnel_y,mode='same',boundary = _boundary,fillvalue = _fillvalue)
return gaussBlur_xy
if __name__ =='__main__':
image = cv2.imread('E:\\sy2\\5\\img3.jpg',cv2.IMREAD_GRAYSCALE)
blurImage = gaussBlur(image,9,11,11,'symm')
blurImage = np.round(blurImage)
blurImage = blurImage.astype(np.uint8)
cv2.imshow('image',image)
cv2.imshow('GaussBlur',blurImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
#增大卷积核和标准差,图像越模糊在这里插入代码片