【问题标题】:How can I implement a low-pass Butterworth filter in Matlab?如何在 Matlab 中实现低通巴特沃斯滤波器?
【发布时间】:2017-12-21 09:42:08
【问题描述】:

来自this answer,我知道如何创建高通巴特沃斯滤波器。

this video,我知道,lowpasskernel = 1 - highpasskernel

所以,我创建了以下低通巴特沃斯滤波器,

function [out, kernel] = butterworth_lp(I, Dl, n)
    height = size(I, 1);
    width  = size(I, 2);

    [u, v] = meshgrid(-floor(width/2):floor(width/2)-1,-floor(height/2):floor(height/2)-1);

    % lp_kernel = 1 - hp_kernel
    kernel = 1 - butter_hp_kernel(u, v, Dl, n);

    % fft the image
    I_fft_shifted = fftshift(fft2(double(I)));

    % apply lowpass filter
    I_fft_shift_filtered = I_fft_shifted .* kernel;

    % inverse FFT, get real components
    out = real(ifft2(ifftshift(I_fft_shift_filtered)));

    % normalize and cast
    out = (out - min(out(:))) / (max(out(:)) - min(out(:)));
    out = uint8(255*out);

function k = butter_hp_kernel(u, v, Dh, n)
    uv =  u.^2+v.^2;
    D = sqrt(uv);
    frac = Dh./D;
    p = 2*n;
    denom =  frac.^p;
    k = 1./denom;

输出

这不是低通滤波器输出。

那么,我的代码有什么问题?

【问题讨论】:

  • 你在哪里做 1 - 什么?
  • 好的。但是您运行了哪些代码来获取该图像?你刚才必须编辑它的事实并没有让我充满信心......
  • @OliverCharlesworth,你的两个疑问都解决了。

标签: matlab lowpass-filter butterworth


【解决方案1】:

好的。我已经按照following formula (Page #8/48)解决了这个问题,

输出

源代码

butter_lp_kernel.m

function f = butter_lp_f(u, v, Dl, n)
    uv = u.^2+v.^2;
    Duv = sqrt(uv);
    frac = Duv./Dl;
    denom = frac.^(2*n);
    f = 1./(1.+denom);

function k = butter_lp_kernel(I, Dl, n) 
    Height = size(I,1); 
    Width = size(I,2); 

    [u, v] = meshgrid( ...
                    -floor(Width/2) :floor(Width-1)/2, ...
                    -floor(Height/2): floor(Height-1)/2 ...
                 ); 

    k = butter_lp_f(u, v, Dl, n);

ifftshow.m

function out = ifftshow(f)
    f1 = abs(f);
    fm = max(f1(:));
    out = f1/fm;
end

butterworth_lpf.m

function [out1, out2] = butterworth_lpf(I, Dl, n)

    Kernel = butter_lp_kernel(I, Dl, n);

    I_ffted_shifted = fftshift(fft2(I));

    I_ffted_shifted_filtered = I_ffted_shifted.*Kernel;

    out1 = ifftshow(ifft2(I_ffted_shifted_filtered));

    out2 = ifft2(ifftshift(I_ffted_shifted_filtered));
end

【讨论】:

    【解决方案2】:

    您没有正确复制高通滤波器中的公式:

     uv =  u.^2+v.^2;
     D = uv.^0.5;
     frac = Dh./D;
     p = 2*n;
     denom =  frac.^p;
     k = 1./(1+denom); --> you forgot the "1+"
    

    新输出:

    【讨论】:

    • 不,meshgrid 调用是正确的。该公式也可能不正确,这是罪魁祸首。为了给出更好的答案,请提供过滤器的正确实现。此答案目前在此状态下没有帮助。
    • 没错,在电影中它是不同的,但没关系。是公式不对。我会编辑它。
    • 另外,为了充分利用这一点和我的投票,实际上显示了一个示例过滤图像。它会真正表明这是错误的公式。
    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2012-05-03
    相关资源
    最近更新 更多