1. 程式人生 > >影象濾波處理:頻域濾波器實現

影象濾波處理:頻域濾波器實現

課後作業,實現“理想、巴特沃斯、高斯”高通低通濾波器。

程式碼基於Matlab實現。完整程式碼及處理結果見:GitHub

步驟

  1. 載入影象
  2. 中心化影象
  3. 傅立葉變換
  4. 與濾波器做運算(空域的卷積運算對應頻域的乘法運算)
  5. 傅立葉反變換
  6. 裁剪影象

低通濾波器

理想低通濾波器

實現程式碼

close all;
clear all;
f = imread('cameraman.tif');
f = mat2gray(f,[0 255]);

[M,N] = size(f);
P = 2*M;
Q = 2*N;
fc = zeros(M,N);

for x = 1
:1:M for y = 1:1:N fc(x,y) = f(x,y) * (-1)^(x+y); end end F = fft2(fc,P,Q); H_1 = zeros(P,Q); H_2 = zeros(P,Q); H_3 = zeros(P,Q); for x = (-P/2):1:(P/2)-1 for y = (-Q/2):1:(Q/2)-1 D = (x^2 + y^2)^(0.5); if(D <= 10) H_1(x+(P/2)+1,y+(Q/2)+1) = 1; end if(D <= 50
) H_2(x+(P/2)+1,y+(Q/2)+1) = 1; end if(D <= 150) H_3(x+(P/2)+1,y+(Q/2)+1) = 1; end end end G_1 = H_1 .* F; G_2 = H_2 .* F; G_3 = H_3 .* F; g_1 = real(ifft2(G_1)); g_1 = g_1(1:1:M,1:1:N); g_2 = real(ifft2(G_2)); g_2 = g_2(1:1:M,1:1:N); g_3 = real(ifft2(G_3)); g_3 = g_3(1:1:M,1
:1:N); for x = 1:1:M for y = 1:1:N g_1(x,y) = g_1(x,y) * (-1)^(x+y); g_2(x,y) = g_2(x,y) * (-1)^(x+y); g_3(x,y) = g_3(x,y) * (-1)^(x+y); end end %% -----show------- figure(); subplot(2,2,1); imshow(f,[0 1]); xlabel('a).Original Image'); subplot(2,2,2); imshow(g_1,[0 1]); xlabel('b).Ideal_Lowpass/D0=10'); subplot(2,2,3); imshow(g_2,[0 1]); xlabel('c).Ideal_Lowpass/D0=50'); subplot(2,2,4); imshow(g_3,[0 1]); xlabel('d).Ideal_Lowpass/D0=150');

結果

可以看到D0的值越少,即允許用過的頻率越低。高頻資訊損失越多,表現在影象上是邊緣資訊的丟失,看起來影象模糊。如圖b。

巴特沃斯低通濾波器

這裡只展示核心的濾波器實現程式碼。完整程式碼見GitHub

for x = (-P/2):1:(P/2)-1
     for y = (-Q/2):1:(Q/2)-1
        D = (x^2 + y^2)^(0.5);
        D_0 = 10;
        H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);
        D_0 = 50;
        H_2(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);
        D_0 = 150;
        H_3(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);    
     end
end

高斯低通濾波器

for x = (-P/2):1:(P/2)-1
     for y = (-Q/2):1:(Q/2)-1
        D = (x^2 + y^2)^(0.5);
        D_0 = 10;
        H_1(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
        D_0 = 50;
        H_2(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
        D_0 = 150;
        H_3(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
     end
end

高通濾波器

高通濾波器和低通濾波器在形式上相反,即之前允許通過的頻率截止,之間截止的頻率允許通過。公式上表示為高通濾波器=1-低通濾波器的關係。

理想高通濾波器

for x = (-P/2):1:(P/2)-1
     for y = (-Q/2):1:(Q/2)-1
        D = (x^2 + y^2)^(0.5);
        if(D >= 10)  H_1(x+(P/2)+1,y+(Q/2)+1) = 1; end    
        if(D >= 50)  H_2(x+(P/2)+1,y+(Q/2)+1) = 1; end
        if(D >= 150)  H_3(x+(P/2)+1,y+(Q/2)+1) = 1; end
     end
end

巴特沃斯高通濾波器

for x = (-P/2):1:(P/2)-1
     for y = (-Q/2):1:(Q/2)-1
        D = (x^2 + y^2)^(0.5);
        D_0 = 10;
        H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);
        D_0 = 50;
        H_2(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);
        D_0 = 150;
        H_3(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);       
     end
end

高斯高通濾波器

for x = (-P/2):1:(P/2)-1
     for y = (-Q/2):1:(Q/2)-1
        D = (x^2 + y^2)^(0.5);
        D_0 = 10;
        H_1(x+(P/2)+1,y+(Q/2)+1) = 1-exp(-(D*D)/(2*D_0*D_0));
        D_0 = 50;
        H_2(x+(P/2)+1,y+(Q/2)+1) = 1-exp(-(D*D)/(2*D_0*D_0));
        D_0 = 150;
        H_3(x+(P/2)+1,y+(Q/2)+1) = 1-exp(-(D*D)/(2*D_0*D_0));
     end
end