Python-openCV高斯平滑

对于可分离式卷积核可通过先进行x方向卷积再进行y方向卷积进行图像卷积计算
#coding=utf-8
import cv2
import  numpy as np
from scipy import signal
def gauss_blur(img,sigma,H,W):

    gauss_kenrnel1=cv2.getGaussianKernel(W,sigma,cv2.CV_64F)
    gauss_kenrnel1=np.transpose(gauss_kenrnel1) #转置
    gauss_blur1=signal.convolve2d(img,gauss_kenrnel1,mode='full',boundary='fill',fillvalue=0)
    gauss_kenrnel2 = cv2.getGaussianKernel(H, sigma,cv2.CV_64F)
    gauss_blur2 = signal.convolve2d(gauss_blur1, gauss_kenrnel2, mode='full', boundary='fill', fillvalue=0)
    return gauss_blur2

img=cv2.imread('timg.jpeg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
blur_img=gauss_blur(img,4,5,5)
blur_img=np.round(blur_img)
blur_img=blur_img.astype(np.uint8)
cv2.imshow('img',img)
cv2.imshow('blur_img',blur_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(Python)