Matlab数字图像处理基础【5】

来源:互联网 发布:ug编程学习 编辑:程序博客网 时间:2024/04/30 17:11

第五章 频域滤波

代码参考自:

[置顶] [数字图像处理]频域滤波(1)--基础与低通滤波器


[数字图像处理]频域滤波(2)--高通滤波器,带阻滤波器与陷波滤波器


另外,本章代码对上述代码做出了一些修改。

目录:

5.1 低通频率滤波器

    5.1.1 理想低通滤波器

    5.1.2 巴特沃斯低通滤波器

    5.1.3 高斯低通滤波器

5.2 高通频率滤波器

    5.2.1 理想高通滤波器

    5.2.2 巴特沃斯高通滤波器

    5.2.3 高斯高通滤波器


5.1 低通频率滤波器

5.1.1 理想低通滤波器

close all;

clear all;

 

%% ---------Ideal Lowpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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);

 

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 <= 60) H_1(x+(P/2)+1,y+(Q/2)+1) = 1; end   

       if(D <= 160) H_2(x+(P/2)+1,y+(Q/2)+1) = 1; end

     end

end

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);        

 

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);

    end

end

 

%% -----show-------

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c).Ideal Lowpass filter(D=60)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Ideal Lowpass filter(D=160)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');

 

5.1.2 巴特沃斯低通滤波器

close all;

clear all;

 

%% ---------Butterworth Lowpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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);

 

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= 100;

       H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);  

       H_2(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^6);

     end

end

 

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);        

 

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);

    end

end

 

%% -----show-------

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c)Butterworth Lowpass (D_{0}=100,n=1)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Butterworth Lowpass (D_{0}=100,n=3)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');


5.1.3 高斯低通滤波器

close all;

clear all;

clc;

%% ---------Gaussian Lowpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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);

 

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= 60;

       H_1(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));  

        D_0= 160;

       H_2(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));

     end

end

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);         

 

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);

    end

end

 

 

%% -----show-------

close all;

 

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c)Gaussian Lowpass (D_{0}=60)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Gaussian Lowpass (D_{0}=160)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');


5.2 高通频率滤波器

5.2.1 理想高通滤波器

close all;

clear all;

 

%% ---------Ideal Highpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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 = ones(P,Q);

H_2 = ones(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 <= 60) H_1(x+(P/2)+1,y+(Q/2)+1) = 0; end   

       if(D <= 160) H_2(x+(P/2)+1,y+(Q/2)+1) = 0; end

     end

end

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);        

 

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);

    end

end

%% -----show-------

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c).Ideal Highpass filter(D=60)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Ideal Highpass filter(D=160)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');


5.2.2 巴特沃斯高通滤波器

close all;

clear all;

 

%% ---------Butterworth Highpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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);

 

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= 100;

       H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);  

       H_2(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^6);

     end

end

 

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);        

 

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);

    end

end

 

%% -----show-------

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c)Butterworth Lowpass (D_{0}=100,n=1)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Butterworth Lowpass (D_{0}=100,n=3)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');


5.2.3 高斯高通滤波器

close all;

clear all;

 

%% ---------Gaussian Highpass Filters (Fre.Domain)------------

f = imread('flower_gray.jpg');

f = imresize(f, [600 600]);

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);

 

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= 60;

       H_1(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));  

        D_0= 160;

       H_2(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));

     end

end

 

G_1 = H_1 .* F;

G_2 = H_2 .* 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);         

 

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);

    end

end

 

 

%% -----show-------

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_1,[0 1]);

xlabel('c)Gaussian Highpass (D_{0}=60)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_1(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('d).Result of filtering using c');

 

subplot(1,2,2);

imshow(g_1,[0 1]);

xlabel('e).Result image');

 

figure();

subplot(1,2,1);

imshow(H_2,[0 1]);

xlabel('f).Gaussian Highpass (D_{0}=160)');

 

subplot(1,2,2);

h = mesh(1:20:P,1:20:Q,H_2(1:20:P,1:20:Q));

set(h,'EdgeColor','k');

axis([0 P 0 Q 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,1);

imshow(log(1 + abs(G_2)),[ ]);

xlabel('g).Result of filtering using e');

 

subplot(1,2,2);

imshow(g_2,[0 1]);

xlabel('h).Result image');

0 0
原创粉丝点击