如何用python实现图像的一维高斯滤波器

如何用python实现图像的一维高斯滤波器,第1张

如何用python实现图像的一维高斯滤波器

现在把卷积模板中的值换一下,不是全1了,换成一组符合高斯分布的数值放在模板里面,比如这时中间的数值最大,往两边走越来越小,构造一个小的高斯包。实现的函数为cv2.GaussianBlur()。对于高斯模板,我们需要制定的是高斯核的高和宽(奇数),沿x与y方向的标准差(如果只给x,y=x,如果都给0,那么函数会自己计算)。高斯核可以有效的出去图像的高斯噪声。当然也可以自己构造高斯核,相关函数:cv2.GaussianKernel().

import cv2

import numpy as np

import matplotlib.pyplot as plt

img = cv2.imread(‘flower.jpg‘,0) #直接读为灰度图像

for i in range(2000): #添加点噪声

temp_x = np.random.randint(0,img.shape[0])

temp_y = np.random.randint(0,img.shape[1])

img[temp_x][temp_y] = 255

blur = cv2.GaussianBlur(img,(5,5),0)

plt.subplot(1,2,1),plt.imshow(img,‘gray‘)#默认彩色,另一种彩色bgr

plt.subplot(1,2,2),plt.imshow(blur,‘gray‘)

M=imread('dl011.jpg') %读取MATLAB中的名为cameraman的图像

subplot(3,3,1)

imshow(M) %显示原始图像

title('original')

P1=imnoise(M,'gaussian',0.02) %加入高斯躁声

subplot(3,3,2)

imshow(P1)%加入高斯躁声后显示图像

title('gaussian noise')

P2=imnoise(M,'salt &pepper',0.02) %加入椒盐躁声

subplot(3,3,3)

imshow(P2)%%加入椒盐躁声后显示图像

title('salt &pepper noise')

g=medfilt2(P1) %对高斯躁声中值滤波


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11933267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存