python 高斯滤波

一维高斯函数

import numpy as np
import matplotlib.pyplot as plt

mu=0        #均值
sigma=0.8   #标准差
x=np.linspace(-3,3,60)
y=np.exp((-(x-mu)**2)/(2*(sigma**2)))/(np.sqrt(2*np.pi)*sigma)
plt.plot(x,y,"b-",)
plt.grid(True)
plt.show()

 二维高斯函数

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()

构建一个5*5高斯卷积核

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
X = np.arange(-2, 3, 1,dtype=np.float64).reshape(-1,1)#转置
Y = np.arange(-2, 3, 1,dtype=np.float64)  #[-2. -1.  0.  1.  2.]
mux,muy=0,0
sigmax,sigmay = 0.8,0.8
h=((X-mux)/sigmax)**2
hh=((Y-muy)/sigmay)**2
hhh=h+hh
expont = -0.5*(((X-mux)/sigmax)**2 + ((Y-muy)/sigmay)**2)
Z=np.exp(expont)/(2*np.pi*sigmax*sigmay)
Z=Z/np.sum(Z) #归一化
print(Z)
sns.heatmap(Z) ##热力图
plt.show()

 

 自己手写的高斯滤波和opencv的对比

import numpy as np
import cv2
def pre_convolve(img,fill):
    fill_height = fill.shape[0]#卷积高
    fill_width = fill.shape[1]#卷积宽
    P1,P2=fill_height//2,fill_width//2
    img=np.pad(img,((P1,P1),(P2,P2)),'constant',constant_values=(0,0))

    img_height = img.shape[0]#图像高
    img_width = img.shape[1]#图像宽

    #图像进行卷积后的图像大小
    img_pro_height = img_height - fill_height + 1
    img_pro_width = img_width - fill_width + 1

    pro_rgb = np.zeros((img_pro_height,img_pro_width),dtype='uint8')

    for i in range(img_pro_height):
        for j in range(img_pro_width):
            pro_rgb[i][j] = pro_statistic(img[i:i + fill_height,j:j + fill_width],fill)

    return pro_rgb

def pro_statistic(img,fill):
    res = (img * fill).sum()
    if(res > 255):
        res = 255
    elif(res < 0):
        res = abs(res)  # 让负边缘也显现出来
    return res

def convolve(img,fill):#彩色图像分为RGB
    rgb_r = img[:,:,0]
    rgb_g = img[:,:,1]
    rgb_b = img[:,:,2]

    pro_rgb_r = pre_convolve(rgb_r,fill)#每层进行卷积操作
    pro_rgb_g = pre_convolve(rgb_g,fill)
    pro_rgb_b = pre_convolve(rgb_b,fill)

    img_pro = np.dstack((pro_rgb_r,pro_rgb_g,pro_rgb_b))#合并三层,返回图像
    return img_pro

def gaussian(kernel_size,sigma):
    sigma_3 = 3 * sigma
    X = np.linspace(-sigma_3, sigma_3, kernel_size)
    Y = np.linspace(-sigma_3, sigma_3, kernel_size)
    x, y = np.meshgrid(X, Y)
    gauss_1 = 1 / (2 * np.pi * sigma ** 2) * np.exp(- (x ** 2 + y ** 2) / (2 * sigma ** 2))
    Z = gauss_1.sum()  # 计算归一化系数
    gauss_2 = (1 / Z) * gauss_1
    return gauss_2

kernel=gaussian(5,1)
print(kernel)
img=cv2.imread('img1.png')
blur=convolve(img,kernel)
blur1=cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('image',np.hstack((blur,blur1)))
cv2.imwrite('image.png',np.hstack((blur,blur1)))
cv2.waitKey()

 

这里发现自己手写的和opencv实现的高斯滤波还是存在很大的区别的,可能是高斯卷积核不一样 

高斯核大小的影响

import matplotlib.pyplot as plt
import cv2

plt.rc('font', family='Youyuan', size='9')
img = cv2.imread('img1.png')
img_ret1 = cv2.GaussianBlur(img, (3, 3), 0)
img_ret2 = cv2.GaussianBlur(img, (5, 5), 0)
img_ret3 = cv2.GaussianBlur(img, (11, 11), 0)

# 显示图像
fig, ax = plt.subplots(2, 2)
ax[0, 0].set_title('原图')
ax[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  # matplotlib显示图像为rgb格式
ax[0, 1].set_title('GaussianBlur ksize=3')
ax[0, 1].imshow(cv2.cvtColor(img_ret1, cv2.COLOR_BGR2RGB))
ax[1, 0].set_title('GaussianBlur ksize=5')
ax[1, 0].imshow(cv2.cvtColor(img_ret2, cv2.COLOR_BGR2RGB))
ax[1, 1].set_title('GaussianBlur ksize=11')
ax[1, 1].imshow(cv2.cvtColor(img_ret3, cv2.COLOR_BGR2RGB))
ax[0, 0].axis('off');
ax[0, 1].axis('off');
ax[1, 0].axis('off');
ax[1, 1].axis('off')  # 关闭坐标轴显示
plt.show()

 标准差的影响

import matplotlib.pyplot as plt
import cv2

plt.rc('font', family='Youyuan', size='9')
img = cv2.imread('img1.png')
img_ret1 = cv2.GaussianBlur(img, (5, 5), 0.5)
img_ret2 = cv2.GaussianBlur(img, (5, 5), 10)
img_ret3 = cv2.GaussianBlur(img, (5, 5), 25)

# 显示图像
fig, ax = plt.subplots(2, 2)
ax[0, 0].set_title('原图')
ax[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  # matplotlib显示图像为rgb格式
ax[0, 1].set_title('GaussianBlur ksize=5 sigma=0.5')
ax[0, 1].imshow(cv2.cvtColor(img_ret1, cv2.COLOR_BGR2RGB))
ax[1, 0].set_title('GaussianBlur ksize=5 sigma=10')
ax[1, 0].imshow(cv2.cvtColor(img_ret2, cv2.COLOR_BGR2RGB))
ax[1, 1].set_title('GaussianBlur ksize=5 sigma=25')
ax[1, 1].imshow(cv2.cvtColor(img_ret3, cv2.COLOR_BGR2RGB))
ax[0, 0].axis('off');
ax[0, 1].axis('off');
ax[1, 0].axis('off');
ax[1, 1].axis('off')  # 关闭坐标轴显示
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一壶浊酒..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值