数字图像处理第二次编程课后作业
理想低通(d=10,50,150):
close all;
clear all;
%% ---------Ideal Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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 <= 10) H_0(x+(P/2)+1,y+(Q/2)+1) = 1; end
if(D <= 50) H_1(x+(P/2)+1,y+(Q/2)+1) = 1; end
if(D <= 150) H_2(x+(P/2)+1,y+(Q/2)+1) = 1; end
end
end
G_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).理想低通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).理想低通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).理想低通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');
巴特沃斯低通(d=10,50,150):
close all;
clear all;
%% ---------Butterworth Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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);
%n=1
D_0 = 10;
H_0(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);
D_0 = 50;
H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^2);
D_0 = 150;
H_2(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_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).巴特沃斯低通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).巴特沃斯低通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).巴特沃斯低通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');
高斯低通(d=10,50,150):
close all;
clear all;
clc;
%% ---------Gaussian Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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 = 10;
H_0(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
D_0 = 50;
H_1(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
D_0 = 150;
H_2(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
end
end
G_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).高斯低通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).高斯低通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).高斯低通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');
理想高通(d=10,50,150):
close all;
clear all;
%% ---------Ideal Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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 > 10) H_0(x+(P/2)+1,y+(Q/2)+1) = 1; end
if(D > 50) H_1(x+(P/2)+1,y+(Q/2)+1) = 1; end
if(D > 150) H_2(x+(P/2)+1,y+(Q/2)+1) = 1; end
end
end
G_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).理想高通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).理想高通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).理想高通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');
巴特沃斯高通(d=10,50,150):
close all;
clear all;
%% ---------Butterworth Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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);
%n=1
D_0 = 10;
H_0(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);
D_0 = 50;
H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^2);
D_0 = 150;
H_2(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/D_0)^6);
end
end
G_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).巴特沃斯高通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).巴特沃斯高通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).巴特沃斯高通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');
高斯高通(d=10,50,150):
close all;
clear all;
clc;
%% ---------Gaussian Lowpass Filters (Fre. Domain)------------
f = imread('D:/testData/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_0 = zeros(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 = 10;
H_0(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));
D_0 = 50;
H_1(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));
D_0 = 150;
H_2(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));
end
end
G_0 = H_0 .* F;
G_1 = H_1 .* F;
G_2 = H_2 .* F;
g_0 = real(ifft2(G_0));
g_0 = g_0(1:1:M,1:1:N);
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_0(x,y) = g_0(x,y) * (-1)^(x+y);
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('1).原图');
subplot(1,2,2);
imshow(log(1 + abs(F)),[ ]);
xlabel('2).1图的傅里叶光谱');
figure();
subplot(1,2,1);
imshow(H_0,[0 1]);
xlabel('3).高斯高通滤波(D=10)');
subplot(1,2,2);
h = mesh(1:20:P,1:20:Q,H_0(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_0)),[ ]);
xlabel('4).3得到的滤波结果');
subplot(1,2,2);
imshow(g_0,[0 1]);
xlabel('5).效果图(D=10)');
figure();
subplot(1,2,1);
imshow(H_1,[0 1]);
xlabel('6).高斯高通滤波(D=50)');
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('7).6得到的滤波结果');
subplot(1,2,2);
imshow(g_1,[0 1]);
xlabel('8).效果图(D=50)');
figure();
subplot(1,2,1);
imshow(H_2,[0 1]);
xlabel('9).高斯高通滤波(D=150)');
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('10).9得到的滤波结果');
subplot(1,2,2);
imshow(g_2,[0 1]);
xlabel('11).效果图(D=150)');