python求巴特沃斯和高斯的低通滤波和高通滤波

低通滤波 

import numpy as np
import cv2 as cv

# 加载图像
image = cv.imread('www.jpg', 0)

def BLPF(image, D0):
    L = np.empty_like(image, float)
    h, w = image.shape
    for i in range(0, h):
        for j in range(0, w):
            d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
            L[j, i] = 1 / (1 + (d / D0) ** 2)
    return L

def fft(image, L):
    dft = np.fft.fft2(image)
    dftshift = np.fft.fftshift(dft)
    rs = dftshift * L

    idft_shift = np.fft.ifftshift(rs)
    idft = np.fft.ifft2(idft_shift)
    result = np.real(idft)
    return np.uint8(result)

def GLPF(image, D0):
    L = np.empty_like(image, float)
    h, w = image.shape
    for i in range(0, h):
        for j in range(0, w):
            d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
            L[j, i] = np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
    return L

cv.imshow('image', image)
# cv.imshow('BLPF_D0=10', fft(image, BLPF(image, 10)))
# cv.imshow('BLPF_D0=20', fft(image, BLPF(image, 20)))
# cv.imshow('BLPF_D0=40', fft(image, BLPF(image, 40)))
# cv.imshow('BLPF_D0=80', fft(image, BLPF(image, 80)))
cv.imshow('GLPF_D0=10', fft(image, GLPF(image, 10)))
cv.imshow('GLPF_D0=20', fft(image, GLPF(image, 20)))
cv.imshow('GLPF_D0=40', fft(image, GLPF(image, 40)))
cv.imshow('GLPF_D0=80', fft(image, GLPF(image, 80)))
cv.waitKey()

高通滤波

import numpy as np
import cv2 as cv

# 加载图像
image = cv.imread('www.jpg', 0)

def BHPF(image, D0):
    L = np.empty_like(image, float)
    h, w = image.shape
    for i in range(0, h):
        for j in range(0, w):
            d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
            L[j, i] = 1 / (1 + (D0 / d) ** 2)
    return L

def fft(image, L):
    dft = np.fft.fft2(image)
    dftshift = np.fft.fftshift(dft)
    rs = dftshift * L

    idft_shift = np.fft.ifftshift(rs)
    idft = np.fft.ifft2(idft_shift)
    result = np.real(idft)
    return np.uint8(result)

def GHPF(image, D0):
    L = np.empty_like(image, float)
    h, w = image.shape
    for i in range(0, h):
        for j in range(0, w):
            d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
            L[j, i] = 1-np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
    return L

cv.imshow('image', image)
cv.imshow('BHPF_D0=10', fft(image, BHPF(image, 10)))
cv.imshow('BHPF_D0=20', fft(image, BHPF(image, 20)))
cv.imshow('BHPF_D0=40', fft(image, BHPF(image, 40)))
cv.imshow('BHPF_D0=80', fft(image, BHPF(image, 80)))
# cv.imshow('GHPF_D0=10', fft(image, GHPF(image, 10)))
# cv.imshow('GHPF_D0=20', fft(image, GHPF(image, 20)))
# cv.imshow('GHPF_D0=40', fft(image, GHPF(image, 40)))
# cv.imshow('GHPF_D0=80', fft(image, GHPF(image, 80)))
cv.waitKey()

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Matlab中可以使用 `butter` 函数来实现巴特沃斯高通和低通滤波。 下面是一个示例代码,其中使用 `butter` 函数设计一个4阶的巴特沃斯低通滤波器,截止频率为100 Hz,然后将其应用于一个具有高频噪声的信号。同样,我们还使用 `butter` 函数设计一个4阶的巴特沃斯高通滤波器,截止频率为10 Hz,并将其应用于一个具有低频噪声的信号。 ```matlab % 生成一个随机信号 fs = 1000; % 采样率为1000 Hz t = 0:1/fs:1-1/fs; % 时间向量 x = sin(2*pi*50*t) + sin(2*pi*120*t); % 生成一个有高频噪声的信号 x = x + 2.5*randn(size(t)); % 添加高斯白噪声 % 巴特沃斯低通滤波器 fc = 100; % 截止频率为100 Hz Wn = fc/(fs/2); % 归一化截止频率 [b,a] = butter(4,Wn,'low'); % 4阶巴特沃斯低通滤波器 y = filtfilt(b,a,x); % 应用滤波器 % 绘制原始信号和滤波后的信号 figure; subplot(2,1,1); plot(t,x); title('原始信号'); xlabel('时间 (秒)'); ylabel('幅度'); subplot(2,1,2); plot(t,y); title('滤波后的信号'); xlabel('时间 (秒)'); ylabel('幅度'); % 巴特沃斯高通滤波器 fc = 10; % 截止频率为10 Hz Wn = fc/(fs/2); % 归一化截止频率 [b,a] = butter(4,Wn,'high'); % 4阶巴特沃斯高通滤波器 z = filtfilt(b,a,x); % 应用滤波器 % 绘制原始信号和滤波后的信号 figure; subplot(2,1,1); plot(t,x); title('原始信号'); xlabel('时间 (秒)'); ylabel('幅度'); subplot(2,1,2); plot(t,z); title('滤波后的信号'); xlabel('时间 (秒)'); ylabel('幅度'); ``` 注意,这里使用 `filtfilt` 函数来获得零相位滤波器的输出,以避免滤波后的相位变化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值