MATLAB中的理想低通滤波器概念 [英] Ideal Low Pass Filter Concept in MATLAB

查看:162
本文介绍了MATLAB中的理想低通滤波器概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我了解理想低通滤波器的以下MATLAB代码.我无法理解下面的代码中的Part2.请解释一下为什么我们要这样做.

Please help me understand the following MATLAB code for Ideal Low pass filter. I am unable to understand the Part2 in the below code. Please explain me why we are doing like this.

我已经阅读了Rafael C. Gonzalez使用Matlab 2E进行的数字图像处理,它解释了我的问题,但我无法正确理解.如果有人可以清楚地解释我会很有帮助.

I have read the Rafael C. Gonzalez's Digital Image Processing Using Matlab 2E which explains my question but I couldn't understand properly. It will be helpful if someone could explain me clearly.

注意:Dogbert,我的理解是对图像进行变换有助于分离低频分量和高频分量.左上角包含更多的低频率系数,而右下角包含较高的频率系数.低频分量包含所有细节(近似值),而高频分量包含图像中较小的细节.在低通滤波器中,允许截止频率以下的频率通过,而截止频率以上的频率被阻止.

Note: Dogbert, my understanding is that applying transform to an image help to separate low and high frequency components. Top left contains more low freq coefficients where as bottom right contains high freq coefficients. The Low frequency components contains over all detail (approximation) where as the high frequency components contains smaller details in an image. In low pass filter, frequencies below the cut-off freq are allowed to pass and the freqs above the cut-off is blocked.

 %IDEAL LOW-PASS FILTER

%Part 1
        function idealfilter(X,P) % X is the input image and P is the cut-off freq
        f=imread(X);  % reading an image X
        [M,N]=size(f); % Saving the the rows of X in M and columns in N
        F=fft2(double(f)); % Taking Fourier transform to the input image
%Part 2 % I don't understand this part
        u=0:(M-1);
        v=0:(N-1);
        idx=find(u>M/2);
        u(idx)=u(idx)-M;
        idy=find(v>N/2);
        v(idy)=v(idy)-N;
        [V,U]=meshgrid(v,u);
        D=sqrt(U.^2+V.^2);

%Part 3
        H=double(D<=P);       % Comparing with the cut-off frequency 
        G=H.*F;               % Convolution with the Fourier transformed image
        g=real(ifft2(double(G))); % Inverse Fourier transform
        imshow(f),figure,imshow(g,[ ]); % Displaying input and output image
        end

我尝试分别在Part2中为M = 8和N = 8运行每个命令.我知道了

I tried to run each commands in Part2 individually for M= 8 and N=8. I get

u=0:(M-1); ==> u = 0 1 2 3 4 5 6 7

v=0:(N-1); ==> v = 0 1 2 3 4 5 6 7

idx=find(u>M/2); ==> idx = 6 7 8 

u(idx)=u(idx)-M; ==> 0 1 2 3 4 -3 -2 -1

idy=find(v>N/2); ==> idy = 6 7 8 

v(idy)=v(idy)-N; ==> 0 1 2 3 4 -3 -2 -1

[V,U]=meshgrid(v,u); ==> 

V=

     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1
     0     1     2     3     4    -3    -2    -1

U =

     0     0     0     0     0     0     0     0
     1     1     1     1     1     1     1     1
     2     2     2     2     2     2     2     2
     3     3     3     3     3     3     3     3
     4     4     4     4     4     4     4     4
    -3    -3    -3    -3    -3    -3    -3    -3
    -2    -2    -2    -2    -2    -2    -2    -2
    -1    -1    -1    -1    -1    -1    -1    -1

我不确定他们为什么这样做.请帮助我理解此MATLAB代码.并且也帮助我理解了为什么他们必须在下面的MATLAB代码中使用 fftshift .我确实阅读了MATLAB文档,但无法正确理解.如有可能,请举例说明.提前致谢.帮我学习.

I am unsure why they are doing like this. Please help me understand this MATLAB code. And also help me understand why they have to used fftshift in the below MATLAB code. I did read the MATLAB documentation but I couldn't understand it properly. If possible explain with an example. Thanks in advance. Help me learn.

%This code is used to Butterworth lowpass filter
close all;
clear all;
clc;
im=imread('lean.jpg');
fc=20;%Cutoff frequency
n=1;
[co,ro] = size(im);
cx = round(co/2); % find the center of the image
cy = round (ro/2);
imf=fftshift(fft2(im));
H=zeros(co,ro);
for i = 1 : co
    for j =1 : ro
        d = (i-cx).^2 + (j-cy).^ 2;
        H(i,j) = 1/(1+((d/fc/fc).^(2*n)));
    end;
end;
outf = imf .* H;
out = abs(ifft2(outf));
imshow(im),title('Original Image'),figure,imshow(uint8(out)),title('Lowpass Filterd Image')

推荐答案

它们将高于给定频率的频率归零.
他们使用径向遮罩设置输入或输出的频率.
为此,由于DFT转换为0到2pi,他们构建了一个网格并将其移位.

They zeroed out frequencies above the given frequency.
They used radial mask to set which frequencies are in or out.
In order to do so they built a grid and shifted it since the DFT transformation is 0 to 2pi.

这篇关于MATLAB中的理想低通滤波器概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆