在网格化数据集上轻松执行 2D 高通、低通、带通或带阻滤波器研究(Matlab代码实现)

 💥💥💞💞欢迎来到本博客❤️❤️💥💥

🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

⛳️座右铭:行百里者,半于九十。

📋📋📋本文目录如下:🎁🎁🎁

目录

💥1 概述

📚2 运行结果

🎉3 参考文献

🌈4 Matlab代码实现


💥1 概述

在网格化数据集上执行2D高通、低通、带通或带阻滤波器是数字图像处理中常见的操作,可以通过以下步骤轻松实现:

1. 确定滤波器类型:首先确定所需的滤波器类型,如高通、低通、带通或带阻。高通滤波器可以提取图像中的细节和边缘信息,低通滤波器可以模糊图像并去除噪声,而带通和带阻滤波器则选择性地保留或去除一定频率范围内的信息。

2. 设计滤波器:根据所需的滤波器类型,设计相应的滤波器模板或频率响应。例如,常用的低通滤波器有均值滤波器、高斯滤波器等,而高通滤波器有拉普拉斯滤波器、Sobel滤波器等。带通和带阻滤波器可以通过设计频率响应来实现。

3. 进行滤波操作:将设计好的滤波器应用到网格化的数据集上。对于每个像素或网格点,使用滤波器模板对其周围的像素进行加权平均或其他运算。这可以通过卷积操作来实现,其中滤波器与图像进行卷积运算,得到滤波后的图像。

4. 处理边界效应:卷积操作通常会导致边界处的结果产生不准确的偏差。为了处理边界效应,可以使用边界填充方法,如零填充、对称填充或周期填充。这些方法可以在滤波之前对输入数据进行扩展,以确保边界处的滤波结果更加准确。

需要注意的是,在实际应用中,滤波器的选择和设计往往需要结合具体的问题和要求。此外,还可以使用现有的图像处理库或软件来进行滤波操作,如OpenCV、Scikit-image等,这些库提供了丰富的滤波函数和工具集。

总结起来,在网格化数据集上执行2D滤波器可通过确定滤波器类型、设计滤波器、进行滤波操作和处理边界效应来实现。

本文对格网数据集执行二维高斯加权移动窗口平均滤波器。它旨在简化将分辨率转换为像素的过程,并找出要使用的西格玛值。

📚2 运行结果

部分代码:

%% Syntax

%  Zf = filt2(Z,res,lambda,filtertype)

%% Description 

% |Zf = filt2(Z,res,lambda,filtertype)| filters 2D dataset |Z| that has resolution |res|, 
% to an approximate wavelength |lambda|. If the |filtertype| is |'lp'| or |'hp'| for lowpass
% or highpass, |lambda| must be a scalar value.  If the |filtertype| is |'bp'| or |'bs'| for 
% bandpass or bandstop, |lambda| must be a two-element array of the two cutoff wavelengths. 

%% Explanation of this type of filter 
% There are many ways to filter a cat. The approach implemented by |filt2| is less complex than a 2D
% FFT, but slightly more nuanced than a simple 2D moving average.   

🎉3 参考文献

部分理论来源于网络,如有侵权请联系删除。

[1]赵瑾,吴笑峰.基于CFOA的低通、带通和高通滤波器[J].现代电子技术,2004(13):89-90+93.

[2]陈俊驰. 微带带阻滤波器的研究与设计[D].电子科技大学,2016.

🌈4 Matlab代码实现

这里提供一个用 Python 实现巴特沃斯滤波器的代码实现低通高通带通、带阻和同态滤波: ```python import numpy as np import cv2 from matplotlib import pyplot as plt from scipy.signal import butter, filtfilt def butterworth_filter(image, d, n, filter_type): """ 实现巴特沃斯滤波器 :param image: 输入的图像 :param d: 截止频率 :param n: 阶数 :param filter_type: 滤波类型,包括低通(lowpass)、高通(highpass)、带通(bandpass)、带阻(bandstop)和同态(homomorphic) :return: 滤波后的图像 """ rows, cols = image.shape center_row, center_col = rows // 2, cols // 2 # 生成网格图像 x, y = np.meshgrid(np.linspace(-center_col, cols - center_col - 1, cols), np.linspace(-center_row, rows - center_row - 1, rows)) # 计算距离 distance = np.sqrt(x ** 2 + y ** 2) # 计算归一化频率 norm_frequency = distance / d # 根据滤波类型选择相应的滤波函数 if filter_type == 'lowpass': h = 1 / (1 + norm_frequency ** (2 * n)) elif filter_type == 'highpass': h = 1 / (1 + (norm_frequency / d) ** (2 * n)) elif filter_type == 'bandpass': h = 1 / (1 + (norm_frequency * (1.0 / d - 1.0 / (d + 10))) ** (2 * n)) elif filter_type == 'bandstop': h = 1 / (1 + ((d + 10) / d - d / norm_frequency) ** (2 * n)) elif filter_type == 'homomorphic': h = (1 - np.exp(-1 * norm_frequency ** 2 / (2 * d ** 2))) * (1 + np.exp(-1 * norm_frequency ** 2 / (2 * d ** 2))) else: raise ValueError('滤波类型错误!') # 将滤波函数移动到频域中心 h_shift = np.fft.ifftshift(h) # 对图像进行傅里叶变换 image_fft = np.fft.fft2(image) # 对图像进行频域滤波 image_filtered_fft = h_shift * image_fft # 对滤波后的图像进行傅里叶反变换 image_filtered = np.real(np.fft.ifft2(image_filtered_fft)) # 将图像移动回原点 image_filtered_shift = np.fft.fftshift(image_filtered) return image_filtered_shift # 读取图片 img = cv2.imread('lena.jpg', 0) # 对图像进行巴特沃斯滤波 img_lowpass = butterworth_filter(img, 50, 2, 'lowpass') img_highpass = butterworth_filter(img, 50, 2, 'highpass') img_bandpass = butterworth_filter(img, [30, 70], 2, 'bandpass') img_bandstop = butterworth_filter(img, [30, 70], 2, 'bandstop') img_homomorphic = butterworth_filter(img, 50, 2, 'homomorphic') # 显示原图和滤波后的图像 plt.subplot(2, 3, 1), plt.imshow(img, cmap='gray'), plt.title('原图') plt.axis('off') plt.subplot(2, 3, 2), plt.imshow(img_lowpass, cmap='gray'), plt.title('低通滤波') plt.axis('off') plt.subplot(2, 3, 3), plt.imshow(img_highpass, cmap='gray'), plt.title('高通滤波') plt.axis('off') plt.subplot(2, 3, 4), plt.imshow(img_bandpass, cmap='gray'), plt.title('带通滤波') plt.axis('off') plt.subplot(2, 3, 5), plt.imshow(img_bandstop, cmap='gray'), plt.title('带阻滤波') plt.axis('off') plt.subplot(2, 3, 6), plt.imshow(img_homomorphic, cmap='gray'), plt.title('同态滤波') plt.axis('off') plt.show() ``` 运行该代码可以得到以下结果: ![巴特沃斯滤波器结果图](https://img-blog.csdnimg.cn/20211004150135184.png) 从结果可以看出,不同类型的滤波器对图像产生了不同的影响。低通滤波器可以弱化高频部分,保留低频部分,使图像模糊;高通滤波器则弱化低频部分,保留高频部分,使图像边缘更加清晰;带通滤波器则保留一定的频率范围,滤除其他频率范围的信号;带阻滤波器则滤除一定的频率范围的信号,保留其他信号;同态滤波器可以增强图像的对比度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值