高斯滤波卷积核

高斯滤波卷积核_第1张图片

print(cv2.getGaussianKernel(3, 0))
# 结果:[[0.25][0.5][0.25]]

高斯滤波卷积核_第2张图片

源码: https://github.com/ex2tron/OpenCV-Python-Tutorial/blob/master/10.%20%E5%B9%B3%E6%BB%91%E5%9B%BE%E5%83%8F/cv2_source_code_getGaussianKernel.cpp

 


import math
import cv2
import numpy as np
#二维高斯卷积核
def gaussian_kernel_2d_opencv(kernel_size = 3,sigma = 0):
    kx = cv2.getGaussianKernel(kernel_size,sigma)
    ky = cv2.getGaussianKernel(kernel_size,sigma)
    return np.multiply(kx,np.transpose(ky))

#有点偏差
def gaussian_2d_kernel(kernel_size = 3,sigma = 0):

    kernel = np.zeros([kernel_size,kernel_size])
    center = (kernel_size - 1) /2

    if sigma == 0:
        sigma = ((kernel_size-1)*0.5 - 1)*0.3 + 0.8

    s = 2*(sigma**2)
    sum_val = 0
    for i in range(0,kernel_size):
        for j in range(0,kernel_size):
            x = i-center
            y = j-center
            kernel[i,j] = np.exp(-(x  **2+y**2) / s)
            # print(kernel[i,j] )
            sum_val += kernel[i,j]
            #/(np.pi * s)
    sum_val = 1/sum_val
    return kernel*sum_val


print( gaussian_2d_kernel(3,0) )

 

你可能感兴趣的:(图像)