【问题标题】:Manual high/low-pass filter in MATLABMATLAB中的手动高通/低通滤波器
【发布时间】:2017-09-17 11:18:25
【问题描述】:

我正在努力寻找我的代码或推理中的错误;我必须手动创建一个高通/低通滤波器来处理频域中的图像。我不允许使用 butter()、filter() 和其他相关函数。

我所追求的最终结果与此处显示的类似:link 1link2

为了实现这个结果,我创建了以下函数:

function out = hlow2(x,n,mask,u)
% x - input image
% n - mask size
% mask - user-defined mask
% u - user choice: 1 -> high-pass, anything else -> low-pass
a=size(x);
mask=padarray(mask,[floor((a(1)-n)/2) floor((a(2)-n)/2)],'replicate');
mask=padarray(mask,[1 1],'replicate','pre');
% i am padding the mask array in order to make it fit the image and have my
% circle filter attached to the "middle" of the frequency graph.

maskl=logical(mask);
maskh=~mask;
maskl=double(maskl);
maskh=double(maskh);
% here, i created the high and low pass masks from the user-defined mask.

x=fft2(x);

if u==1
    HP=x.*maskh;
    out=ifft(HP);
else
    LP=x.*maskl;
    out=ifft(LP);
end
end

我使用的面具是黑色背景上的白色圆圈。我在以下代码中使用它:

mask=imread('circle.png');
mask=double(mask)/255;
mask=mask(:,:,1);

boat_a=imread('boat.png');
boat_a2=double(boat_a)/255;

c1=hlow2(boat_a2,255,mask,1);
c2=hlow2(boat_a2,255,mask,2);


figure()
imshow(c1)
figure()
imshow(c2)

然而,最终结果完全不是我所期望的!我检查了图像的频率图,它们看起来很好,圆形滤波器放置在它应该在的位置,但输出图像完全错误。对于高通滤波器,输出图像不变。对于低通,它是完全黑色的。我尝试重写函数几次并使用矩阵相乘的方式,但我的结果不会改变。

我确定我遗漏了一些东西,但我似乎找不到什么。请,请帮助我。

【问题讨论】:

    标签: matlab image-processing filter fft lowpass-filter


    【解决方案1】:

    使用 ifft2 和 fftshift

    x=fft2(x);
    x=fftshift(x);
    if u==1
        P=x.*maskh;
    else
        P=x.*maskl;
    end
    P=fftshift(P);
    out=ifft2(P);
    

    另外我认为这是制作中间有圆圈的面具的一种更简单的方法:

    [i,j]=size(im');
    [x, y] = meshgrid(1:i, 1:j);
    r=20;
    mask=((x-i/2).^2+(y-j/2).^2)<r^2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多