【课程作业】实现高斯低通滤波器并与理想低通滤波器进行效果比较

要求

尝试设计巴特沃斯或高斯低通滤波器对下面的图像进行处理,比较不同参数下的结果,并与理想滤波器的结果进行比较。

在这里插入图片描述

高斯低通滤波器实现原理

高斯低通滤波器公式如下:
在这里插入图片描述
根据不同的sigma值,生成模糊后图像

高斯低通滤波器实现代码

import cv2
from matplotlib import pyplot as plt
import numpy as np

imgGray = cv2.imread(r"D:\Project\PythonProject\GraphicAnalysis\class4\zuoye4.png", flags=0)  # flags=0 读取为灰度图像

imgFloat32 = np.float32(imgGray)  
dft = cv2.dft(imgFloat32, flags=cv2.DFT_COMPLEX_OUTPUT) 
dftShift = np.fft.fftshift(dft)  

plt.figure(figsize=(10, 10))
rows, cols = imgGray.shape[:2]  
sigma2 = np.arange(0.05, 0.25, 0.05)  
for i in range(4):

    x, y = np.mgrid[-1:1:2 / rows, -1:1:2 / cols]
    z =  np.exp(-(x ** 2 + y ** 2) / (2 * sigma2[i]))
    # 归一化为 [0,255]
    z = np.uint8(cv2.normalize(z, None, 0, 255, cv2.NORM_MINMAX))  
    maskGauss = np.zeros((rows, cols, 2), np.uint8)
    maskGauss[:,:,0] = z
    maskGauss[:,:,1] = z

    dftTrans = dftShift * maskGauss  

    ishift = np.fft.ifftshift(dftTrans)  
    idft = cv2.idft(ishift)  
    imgRebuild = cv2.magnitude(idft[:,:,0], idft[:,:,1])  

    plt.subplot(2,4,i+1), plt.title("sigma^2={:.2f}".format(sigma2[i])), plt.axis('off')
    plt.imshow(maskGauss[:,:,0], cmap='gray')
    plt.subplot(2,4,i+5), plt.title(" "), plt.axis('off')
    plt.imshow(imgRebuild, cmap='gray')

plt.tight_layout()
plt.show()

高斯低通滤波器实现效果

在这里插入图片描述
在这里插入图片描述

与理想低通滤波对比

理想低通滤波实现代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class4/zuoye4.png")[:,:,0]
#傅里叶变换
dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
fshift = np.fft.fftshift(dft)
#设置低通滤波器
rows, cols = img.shape
crow,ccol = int(rows/2), int(cols/2) #中心位置
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-60:crow+60, ccol-60:ccol+60] = 1
#掩膜图像和频谱图像乘积
f = fshift * mask
print (f.shape, fshift.shape, mask.shape)
#傅里叶逆变换
ishift = np.fft.ifftshift(f)
iimg = cv2.idft(ishift)
res = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])
#显示原始图像和低通滤波处理图像
plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Image')
plt.axis('off')
plt.subplot(132), plt.imshow(res, 'gray'), plt.title('Result Image')
plt.axis('off')
plt.subplot(133), plt.imshow(mask[:,:,1], 'gray'), plt.title('Mask')
plt.axis('off')
plt.show()

理想低通滤波实现效果

在这里插入图片描述

分析

分析可知,理想低通滤波器有明显的振铃现象,而高斯低通滤波器没有振铃现象,高斯低通滤波器实现效果较好。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

a9c93f2300

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

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

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

打赏作者

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

抵扣说明:

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

余额充值