1. 程式人生 > 其它 >matlab 遍歷影象_二維Fourier變換及Matlab實現

matlab 遍歷影象_二維Fourier變換及Matlab實現

技術標籤:matlab 遍歷影象

對於一個二維影象,二維Fourier變換本質上可以理解為在一組由Sine、Cosine函式構成的二維基底下,求取每個基底對應的相關值,這些相關值構成了這個二維影象在Fourier變換基下的表達

2D Continuous Fourier變換表示式

equation?tex=F%28u%2Cv%29%3D%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7Df%28x%2Cy%29e%5E%7B-j2%5Cpi%28ux%2Bvy%29%7Ddxdy

equation?tex=f%28x%2Cy%29%3D%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7DF%28u%2Cv%29e%5E%7Bj2%5Cpi%28ux%2Bvy%29%7Ddudv

其中,

equation?tex=f%28x%2Cy%29 表示空域影象, equation?tex=F%28u%2Cv%29 表示對應的頻域影象, equation?tex=%28x%2Cy%29表示空間座標, equation?tex=%28u%2Cv%29 表示頻率取值, equation?tex=e%5E%7B-j2%5Cpi%28ux%2Bvy%29%7D%3D%5Ccos2%5Cpi%28ux%2Bvy%29-j%5Csin+2%5Cpi%28ux%2Bvy%29 可以理解為變換的基底,這些基底是由Sine、Cosine函式構成的

2D Discrete Fourier Transform(2D DFT)的表示式

equation?tex=F%28u%2Cv%29%3D%5Csum_%7Bx%3D0%7D%5E%7BM-1%7D%5Csum_%7By%3D0%7D%5E%7BN-1%7Df%28x%2Cy%29e%5E%7B-j2%5Cpi%28ux%2FM%2Bvy%2FN%29%7D

equation?tex=f%28x%2Cy%29%3D%5Cfrac%7B1%7D%7BMN%7D%5Csum_%7Bu%3D0%7D%5E%7BM-1%7D%5Csum_%7Bv%3D0%7D%5E%7BN-1%7DF%28u%2Cv%29e%5E%7Bj2%5Cpi%28ux%2FM%2Bvy%2FN%29%7D
  • 其中, equation?tex=%28M%2CN%29 表示二維影象 equation?tex=f%28x%2Cy%29
    的長和寬
  • 可以理解為,對於每一個頻域中的點 equation?tex=%28u_0%2Cv_0%29 ,都對應時域中的一個基底: equation?tex=e%5E%7B-j2%5Cpi%28u_0x%2FM%2Bv_0y%2FN%29%7D%3D%5Ccos2%5Cpi%28u_0x%2FM%2Bv_0y%2FN%29-j%5Csin2%5Cpi%28u_0x%2FM%2Bv_0y%2FN%29equation?tex=x%2Cy 為變數。比如,當取 equation?tex=%28u_0%2Cv_0%29%3D%280%2C0%29 時,可得 equation?tex=b%28x%2Cy%29%3D1-j0 ,即此時的基底就是一個取值恆定的影象。我們可以取不同的 equation?tex=%28u_0%2Cv_0%29 ,分別繪製其對應的基底影象:
知乎視訊​www.zhihu.com zhihu-card-default.svg

左圖表示取不同的

equation?tex=u%2Cv 值,右圖表示該取值下對應的基底影象(注意只顯示了實部)

繪製上述過程的Matlab程式碼如下:

Img_M = 20;     % 設定影象大小
Img_N = 20;
[nx,ny] = ndgrid([0:Img_M-1]-(Img_M-1)/2,[0:Img_N-1]-(Img_N-1)/2 ); % 計算x、y可能取值
figure,
set(gcf,'position',[100,100,1500*0.6,520*0.6]);
skip = 1;
for u = [0:skip:Img_M-1]
  for v = [0:skip:Img_N-1]
    subplot(1,2,1)
    b = zeros(Img_M,Img_N);
    b(u+1,v+1)=1;
    imagesc(b);colormap gray;
    xlabel('u');ylabel('v');
    title(['u=',num2str(u),',','v=',num2str(v)])
    subplot(1,2,2)
    imagesc(real(exp(-i*2*3.1416*(u*nx/Img_M+v*ny/Img_N))));colormap gray;
    xlabel('x');ylabel('y');
    pause(0.1)
  end
end
  • 根據2D DFT的表示式,Fourier變換的本質是去求空域影象 equation?tex=f%28x%2Cy%29 與每個基底 equation?tex=%28u_0%2Cv_0%29 之間的相關值,將該相關值放在頻域影象座標 equation?tex=%28u_0%2Cv_0%29 處,遍歷每個可能的 equation?tex=%28u_0%2Cv_0%29 即可得到最終的Fourier變換後的影象
  • 可以編寫Matlab程式碼實現上述過程:
clear
close all
Img=double(imread('lena.jpg'));
Img=imresize(Img,[150,150]);
[Img_M, Img_N] = size(Img);
Output=zeros(Img_M,Img_N);
SumOutner = 0;
%% 2D Discrete Fourier Transform
[nx,ny]=ndgrid([0:Img_M-1],[0:Img_N-1]);
du=1;
for u = [0:Img_M-1]
 dv=1;
for v = [0:Img_N-1]
    SumOutner=sum(sum(Img.*exp(-1i*2*3.1416*(u*nx/Img_M+v*ny/Img_N))));
    Output(du,dv) = SumOutner;
    dv=dv+1;
end
du=du+1;
end
%% Calculate Spectrum and show
figure,set(gcf,'position',[100,100,1800*0.6,520*0.6]);
subplot(1,3,1)
imagesc(Img);colormap gray;title('原始影象')
subplot(1,3,2)
imagesc(log(abs(fftshift(Output))));colormap gray;title('Fourier變換後圖像')
subplot(1,3,3)
imagesc(log(abs(fftshift(fft2(Img)))));colormap gray;title('Matlab自帶函式變換後圖像')

程式碼執行結果為:

492115fda4545e75296c72adc0932347.png
  • 以上程式碼可以參考:
2D DFT​github.com