2013-10-22 41 views
1

請幫我理解理想低通濾波器的以下MATLAB代碼。我無法理解下面代碼中的Part2。請解釋我們爲什麼這樣做。理想的低通濾波器概念在MATLAB中

我已閱讀拉斐爾岡薩雷斯的數字圖像處理使用Matlab 2E解釋我的問題,但我無法正確理解。如果有人能夠清楚地解釋我,這將會有所幫助。

注:Dogbert,我的理解是將變換應用到圖像有助於分離低頻和高頻分量。左上角包含更多的低頻係數,右下角包含高頻係數。低頻分量包含所有細節(近似),其中高頻分量包含圖像中的較小細節。在低通濾波器中,允許截止頻率以下的頻率通過,並且阻斷截止頻率以上的頻率。

%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中的每個命令。我得到

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文檔,但我無法正確理解它。如果可能的話用一個例子來解釋提前致謝。幫助我學習。

%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') 
+0

這是一個代碼審查的邊界。你能告訴我們你對這段代碼有多少了解嗎?也許每行註釋附加到行尾。這可以讓我們知道迄今爲止您已經付出了多少努力來理解它,並讓我們知道您開始偏離哪個方向。 – DevNull

+0

@Dogbert,我已經添加了關於我的理解的詳細信息,並在每行添加了行註釋。請檢查 –

+0

@Dogbert我已閱讀Rafael C. Gonzalez的使用Matlab 2E的數字圖像處理,它解釋了我的問題,但我無法正確理解。如果有人能夠清楚地解釋我,這將會有所幫助 –

回答

1

他們清零了給定頻率以上的頻率。
他們使用徑向掩模來設置哪些頻率進出。
爲了做到這一點,他們建立了一個網格,並將其轉換爲DFT轉換爲0到2pi。