python opencv巴特沃斯低通滤波处理图片

以下是使用Python OpenCV实现巴特沃斯低通滤波器对图片进行处理的代码示例:

```python import cv2 import numpy as np

读取原始图片

img = cv2.imread('input.jpg')

转换为灰度图像

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

计算频谱和中心点坐标

f = np.fft.fft2(gray) fshift = np.fft.fftshift(f) rows, cols = gray.shape crow, ccol = int(rows/2), int(cols/2)

定义巴特沃斯低通滤波器

D0 = 100 n = 1 def butterworth_lp(s, D0, n): return 1 / (1 + (s/D0)*(2n))

构建滤波器矩阵

filtermatrix = np.zeros((rows, cols)) for i in range(rows): for j in range(cols): distance = np.sqrt((i-crow)2 + (j-ccol)2) filtermatrix[i, j] = butterworth_lp(distance, D0, n)

进行滤波操作

filteredfshift = fshift * filtermatrix filtered_f = np.fft.ifftshift(filteredfshift) filtered = np.fft.ifft2(filteredf).real

显示并保存结果

cv2.imshow(&

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
巴特沃斯低通滤波器是一种常用的信号处理方法,可以用于去除信号中的高频噪声。在Python中,可以使用scipy库中的butter函数来设计和实现巴特沃斯低通滤波器。以下是一个示例代码: ```python import numpy as np from scipy.signal import butter, filtfilt # 设计低通滤波器 def butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False) return b, a # 应用滤波器 def butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = filtfilt(b, a, data) return y # 示例数据 data = np.random.randn(1000) # 设计并应用低通滤波器 cutoff = 100 # 截止频率 fs = 1000 # 采样率 order = 6 # 阶数 filtered_data = butter_lowpass_filter(data, cutoff, fs, order) # 绘制原始数据和滤波后的数据 import matplotlib.pyplot as plt t = np.arange(len(data)) / fs plt.plot(t, data, 'r-', label='raw data') plt.plot(t, filtered_data, 'b-', linewidth=2, label='filtered data') plt.legend() plt.xlabel('Time (seconds)') plt.ylabel('Amplitude') plt.show() ``` 在上面的示例代码中,我们先定义了两个函数:butter_lowpass和butter_lowpass_filter。其中,butter_lowpass用于根据指定的截止频率、采样率和阶数,设计出一个巴特沃斯低通滤波器;而butter_lowpass_filter则是用于将输入数据应用到这个滤波器上,得到滤波后的输出数据。最后,我们使用随机生成的数据并绘制了原始数据和滤波后的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值