Text Figure (4.2) 1D Discrete Fourier Transform

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Text figure (4.

2) 1D Discrete Fourier Transform


%Matlab Code
A = 1; K = 10; M = 100;
f1 = zeros(1,M); f2 = zeros(1,M);
f1(1:K) = A; f2(1:2*K) = A;
fft1 = abs(fftshift(fft(f1)))/M;
fft2 = abs(fftshift(fft(f2)))/M;
figure;
subplot(2,2,1);
plot(f1); title('Filter for M=100, K=10, A=1');
ylabel('f(x)'); axis([0 M 0 1.1*A]);
subplot(2,2,2);
plot(fft1); title('Fourier Transform for Filter
ylabel('|F(u)|'); axis([0 M 0 1.1*[A*K]/M]);
subplot(2,2,3);
plot(f2); title('Filter for M=100, K=10, A=1');
ylabel('f(x)'); axis([0 M 0 1.1*A]);
subplot(2,2,4);
plot(fft2); title('Fourier Transform for Filter
ylabel('|F(u)|'); axis([0 M 0 1.1*[2*A*K]/M]);

xlabel('x');

in (a)'); xlabel('u');

xlabel('x');

in (c)'); xlabel('u');

Fourier Transform for Filter in (a)

0.1

0.8

0.08

0.6

0.06

|F(u)|

f(x)

Filter for M=100, K=10, A=1

0.4

0.04

0.2

0.02

20

40

60

80

100

20

40

60

80

Filter for M=100, K=10, A=1

Fourier Transform for Filter in (c)

100

0.2

0.8

0.15

0.6

|F(u)|

f(x)

0.1

0.4
0.05

0.2
0

20

40

60
x

80

100

20

40

60
u

Text figure (4.3) 2D Discrete Fourier Transform

80

100

4.4)
%Matlab Code
close all; clear all;
img = imread('Fig4.03(a).jpg');
imgfft = abs(fftshift(fft2(img)));
figure;
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(imscale01(log10(imgfft+1)));

Text figure (4.6) 2D Fourier Notch Filtering

%Matlab Code
close all; clear all;
img = imread('Fig4.04(a).jpg');
imgfft = fftshift(fft2(img));
imgfft(size(img,1)/2+1, size(img,2)/2+1) = 0;
imgfilt = abs((ifft2(imgfft)));
figure;
imshow(imscale01(imgfilt));

Text figure (4.7) High Pass / Low Pass 2D Filtering

%Matlab Code
close all; clear all;
img = imread('Fig4.04(a).jpg');
[x,y] = size(img);
lp = zeros(size(img));
cx = x/2; cy = y/2; std = 30;
for i=1:x; for j=1:y;
lp(i,j) = exp(-[(i-cx)^2 + (j-cy)^2]/std^2);
end end
hp = 1-lp;
lpplot = lp(1:8:x, 1:8:y); hpplot = hp(1:8:x, 1:8:y);
imglp = fftshift(fft2(img)); imglp = imglp .* lp;
imghp = imglp; imghp = imghp .* hp;
imglp = abs((ifft2(imglp))); imghp = abs((ifft2(imghp)));
figure;
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);

mesh(lpplot); axis tight; colormap(gray(1));


imshow(imscale01(imglp));
mesh(hpplot); axis tight; colormap(gray(1));
imshow(imscale01(imghp));

Text figure (4.8) Modified 2D High Pass Filter

%Matlab Code
close all; clear all;
img = imread('Fig4.04(a).jpg');
[x,y] = size(img);
lp = zeros(size(img));
cx = x/2; cy = y/2; std = 30;
for i=1:x;
for j=1:y;
lp(i,j) = exp(-[(i-cx)^2 + (j-cy)^2]/std^2);
end
end
hp = 1-lp; hp = hp + 0.5*max(hp(:));
imghp = fftshift(fft2(img)); imghp = imghp .* hp;
imghp = abs((ifft2(imghp)));
figure; imshow(imscale01(imghp));

Text figure (4.12) 2D Ideal Low-pass Filtering


%Matlab Code

close all; clear all;


img = double(imread('Fig4.11(a).jpg')); [x,y] = size(img);
lp5
= zeros(size(img)); lp15 = zeros(size(img));
lp30 = zeros(size(img)); lp80 = zeros(size(img));
lp230 = zeros(size(img));
cx = x/2; cy = y/2; std = 30;
for i=1:x;
for j=1:y;
if(sqrt([i-cx]^2 + [j-cy]^2) < 5)
lp5(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) < 15)
lp15(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) < 30)
lp30(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) < 80)
lp80(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) < 230)
lp230(i,j) = 1;
end
end
end
imgfft = fftshift(fft2(img));
lp5
= abs(ifft2(imgfft .* lp5)); lp15 = abs(ifft2(imgfft .* lp15));
lp30 = abs(ifft2(imgfft .* lp30)); lp80 = abs(ifft2(imgfft .* lp80));
lp230 = abs(ifft2(imgfft .* lp230));
figure;
subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(lp5));
subplot(3,2,3); imshow(imscale01(lp15)); subplot(3,2,4); imshow(imscale01(lp30));
subplot(3,2,5); imshow(imscale01(lp80)); subplot(3,2,6); imshow(imscale01(lp230));

Text figure (4.13) Ideal Low-Pass Filter Response

%Matlab Code
close all; clear all;
img = zeros(500,500);
x = 500; y = 500;
cx = 250; cy = 250;
for i=1:x;
for j=1:y;
if(sqrt([i-cx]^2 + [j-cy]^2) < 5)
img(i,j) = 1;
end
end
end
imgfft = (fftshift(ifft2(img)));
a = ones(1,500);
a(1:2:500) = -1;
plota = real(imgfft(250,:)).*a;
img2
= zeros(size(img));
img2(128,128) = 1; img2(128, 500-128) = 1; img2(250, 250) = 1; img2(500-128, 128) = 1;
img2(500-128, 500-128) = 1; img3 = conv2(img2, imgfft,'same');
idx = 1:size(img,2)+1:length(img(:));
plotb = real(img3(idx));
figure;
subplot(3,2,1);
subplot(3,2,2);
subplot(3,2,3);
subplot(3,2,4);
subplot(3,2,5);
subplot(3,2,6);

imshow(imscale01(img));
plot((plota)); axis tight;
imshow(imscale01(0.5*log10(abs(imgfft)+1)));
imshow(imscale01(img2));
plot((plotb)); axis tight;
imshow(imscale01(0.5*log10(abs(img3)+1)));
-4

x 10
2
1
0

100

200

-4

x 10

2
1

0
100

200

300

400

500

Text figure (4.15) Butterworth Low-pass Filtering


%Matlab Code

300

400

500

close all; clear all;


img
= im2double(imread('Fig4.11(a).jpg'));
imgfft = fftshift(fft2(img));
H5
= zeros(500,500); H15 = H5; H30 = H5; H80 = H5;
H230 = H5; x = 500; y = 500; cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
H5(i,j)
= 1 / (1 + [D(i,j)/5]^(4));
H15(i,j) = 1 / (1 + [D(i,j)/15]^(4));
H30(i,j) = 1 / (1 + [D(i,j)/30]^(4));
H80(i,j) = 1 / (1 + [D(i,j)/80]^(4));
H230(i,j) = 1 / (1 + [D(i,j)/230]^(4));
end
end
img5
= abs(ifft2(imgfft .* H5)); img15 = abs(ifft2(imgfft .* H15));
img30 = abs(ifft2(imgfft .* H30)); img80 = abs(ifft2(imgfft .* H80));
img230 = abs(ifft2(imgfft .* H230));
figure;
subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(img5));
subplot(3,2,3); imshow(imscale01(img15)); subplot(3,2,4); imshow(imscale01(img30));
subplot(3,2,5); imshow(imscale01(img80)); subplot(3,2,6); imshow(imscale01(img230));

Text figure (4.16) Butterworth Low-Pass Filter Response


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.11(a).jpg'));
imgfft = fftshift(fft2(img));
H1
= zeros(500,500); H2 = H1; H5 = H1; H20 = H1;
H230 = H5; x = 500; y = 500; cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
H1(i,j) = 1 / (1 + [D(i,j)/5]^(2));
H2(i,j) = 1 / (1 + [D(i,j)/5]^(4));
H5(i,j) = 1 / (1 + [D(i,j)/5]^(10));
H20(i,j) = 1 / (1 + [D(i,j)/5]^(40));
end
end
h1 = abs(fftshift(ifft2(H1))); h2 = abs(fftshift(ifft2(H2)));
h5 = abs(fftshift(ifft2(H5))); h20 = abs(fftshift(ifft2(H20)));
figure;
subplot(2,4,1);
subplot(2,4,2);
subplot(2,4,3);
subplot(2,4,4);
subplot(2,4,5);
subplot(2,4,6);
subplot(2,4,7);
subplot(2,4,8);

imshow(imscale01(20*log10(1+h1)));
imshow(imscale01(20*log10(1+h2)));
imshow(imscale01(20*log10(1+h5)));
imshow(imscale01(20*log10(1+h20)));
plot(h1(250,:)); axis tight;
plot(h2(250,:)); axis tight;
plot(h5(250,:)); axis tight;
plot(h20(250,:)); axis tight;

-4

-4

x 10

-4

-4

x 10

x 10

x 10
3

3
15

2.5
2.5

5
1

400

1.5

1.5

200

10

200

400

0.5

0.5
200

400

Text figure (4.18) Gaussian Low-Pass Filtering


%Matlab Code

200

400

close all; clear all;


img
= im2double(imread('Fig4.11(a).jpg'));
imgfft = fftshift(fft2(img));
H5
= zeros(500,500); H15 = H5; H30 = H5; H80 = H5; H230
x = 500; y = 500; cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
end
end
H5
= exp(-.5*[D/5].^2); H15 = exp(-.5*[D/15].^2);
H30 = exp(-.5*[D/30].^2); H80 = exp(-.5*[D/80].^2);
H230 = exp(-.5*[D/230].^2);
h5
= abs(ifft2(H5.*imgfft)); h15
h30 = abs(ifft2(H30.*imgfft)); h80
h230 = abs(ifft2(H230.*imgfft));

= H5;

= abs(ifft2(H15.*imgfft));
= abs(ifft2(H80.*imgfft));

figure;
subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(h5));
subplot(3,2,3); imshow(imscale01(h15)); subplot(3,2,4); imshow(imscale01(h30));
subplot(3,2,5); imshow(imscale01(h80)); subplot(3,2,6); imshow(imscale01(h230));

Text figure (4.20) Gaussian Low-Pass Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.20(a).jpg'));
imgfft = fftshift(fft2(img));
[x,y] = size(img); cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);
end
end
H100 = exp(-.5*[D/100].^2); H80 = exp(-.5*[D/80].^2);
h100 = abs(ifft2(H100.*imgfft)); h80 = abs(ifft2(H80.*imgfft));
figure;
subplot(2,3,1);
subplot(2,3,2);
subplot(2,3,3);
subplot(2,3,4);
subplot(2,3,5);
subplot(2,3,6);

imshow(imscale01(img(380:460,380:460)));
imshow(imscale01(h100(380:460,380:460)));
imshow(imscale01(h80(380:460,380:460)));
imshow(imscale01(img));
imshow(imscale01(h100));
imshow(imscale01(h80));

Text figure (4.21) Gaussian Low-Pass Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.21(a).jpg'));
imgfft = fftshift(fft2(img));
[x,y] = size(img); cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);
end
end
H30 = exp(-.5*[D/30].^2); H10 = exp(-.5*[D/10].^2);
h30 = abs(ifft2(H30.*imgfft)); h10 = abs(ifft2(H10.*imgfft));
figure;
subplot(1,3,1); imshow(imscale01(img));
subplot(1,3,2); imshow(imscale01(h30));
subplot(1,3,3); imshow(imscale01(h10));

Text figure (4.24) Ideal High-pass Filtering


%Matlab Code

close all; clear all;


img = double(imread('Fig4.11(a).jpg')); [x,y] = size(img);
hp15 = zeros(size(img)); hp30 = zeros(size(img));
hp80 = zeros(size(img));
cx = x/2; cy = y/2; std = 30;
for i=1:x;
for j=1:y;
if(sqrt([i-cx]^2 + [j-cy]^2) > 15)
hp15(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) > 30)
hp30(i,j) = 1;
end
if(sqrt([i-cx]^2 + [j-cy]^2) > 80)
hp80(i,j) = 1;
end
end
end
imgfft = fftshift(fft2(img));
hp15 = abs(ifft2(imgfft .* hp15));
hp30 = abs(ifft2(imgfft .* hp30));
hp80 = abs(ifft2(imgfft .* hp80));
figure;
subplot(1,3,1); imshow(imscale01(hp15));
subplot(1,3,2); imshow(imscale01(hp30));
subplot(1,3,3); imshow(imscale01(hp80));

Text figure (4.25) Butterworth High-Pass Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.11(a).jpg'));
imgfft = fftshift(fft2(img));
H15 = zeros(500,500); H30 = H15; H80 = H15;
x = 500; y = 500; cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
H15(i,j) = 1 / (1 + [15/D(i,j)]^(4));
H30(i,j) = 1 / (1 + [30/D(i,j)]^(4));
H80(i,j) = 1 / (1 + [80/D(i,j)]^(4));
end
end
img15 = abs(ifft2(imgfft .* H15));
img30 = abs(ifft2(imgfft .* H30));
img80 = abs(ifft2(imgfft .* H80));
figure;
subplot(1,3,1); imshow(imscale01(img15));
subplot(1,3,2); imshow(imscale01(img30));
subplot(1,3,3); imshow(imscale01(img80));

Text figure (4.26) Gaussian High-Pass Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.11(a).jpg'));
imgfft = fftshift(fft2(img));
H15 = zeros(500,500); H30 = H15; H80 = H15;
x = 500; y = 500; cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
end
end
H15 = 1-exp(-.5*[D/15].^2);
H30 = 1-exp(-.5*[D/30].^2);
H80 = 1-exp(-.5*[D/80].^2);
h15 = abs(ifft2(H15.*imgfft));
h30 = abs(ifft2(H30.*imgfft));
h80 = abs(ifft2(H80.*imgfft));
figure;
subplot(1,3,1); imshow(imscale01(h15));
subplot(1,3,2); imshow(imscale01(h30));
subplot(1,3,3); imshow(imscale01(h80));

Text figure (4.28) Laplacian Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.28(a).jpg'));
imgfft = fftshift(fft2(img));
[x,y] = size(img); cx = x/2; cy = y/2;
for i=1:x;
for j=1:y;
D(i,j)
= -((i-cx)^2 + (j-cy)^2);
end
end
lapimg = ifft2(D.*imgfft);
figure;
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);

imshow(imscale01(img));
imshow(imscale01(abs(lapimg)));
imshow(imscale01((real(lapimg))));
imshow(imscale01(real(img - abs(lapimg))));

Text figure (4.29) High-boost Filtering

%Matlab Code Note: I used a Gaussian HP Filter since I dont trust my Laplacian
close all; clear all;
img
= im2double(imread('Fig4.29(a).jpg'));
imgfft = fftshift(fft2(img));
[x,y] = size(img); cx = x/2; cy = y/2;
k = zeros(x,y);
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
k(i,j) = (-1)^(i+j);
end
end
H
= 1-exp(-.5*[D/50].^2);
himg = real(ifft2(H.*imgfft)).*k;
img1
= img + himg;
img17
= 1.7*img + himg;
figure;
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);

imshow(imscale01(img));
imshow(imscale01(himg));
imshow(imscale01(img1));
imshow(imscale01(img17));

Text figure (4.30) Multiple-operation Filtering

%Matlab Code
close all; clear all;
img
= im2double(imread('Fig4.30(a).jpg'));
imgfft = fftshift(fft2(img));
BHPF
= zeros(size(imgfft));
x = size(BHPF,1); y = size(BHPF,2); cx = x/2; cy = y/2;
Do = floor(0.05*x);
for i=1:x;
for j=1:y;
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
if(D(i,j) == 0) D(i,j)=0.01; end
BHPF(i,j) = 1 / (1 + [Do/D(i,j)]^(4));
end
end
imgBHPF = abs(ifft2(imgfft .* (BHPF)));
imgHFE
= abs(ifft2(imgfft .* (0.5 + 2*BHPF)));
imgHFEeq = floor(255*imscale01(imgHFE));
[hist1, bins1] = hist(double(imgHFEeq(:)),0:255);
hist1 = hist1./length(imgHFEeq(:));
CDF1 = cumsum(hist1);
img1eq = zeros(size(imgHFEeq));
for i=0:255
img1eq(find(imgHFEeq==i)) = CDF1(i+1);
end
figure;
subplot(2,2,1);
subplot(2,2,2);
subplot(2,2,3);
subplot(2,2,4);

imshow(imscale01(img));
imshow(imgBHPF+.4);
imshow(imscale01(imgHFE));
imshow(imscale01(img1eq));

Text figure (4.33) Homomorphic Filtering


%Matlab Code

close all; clear all;


img
= im2double(imread('Fig4.29(a).jpg'));
img2 = log(img+1);
imgfft = fftshift(fft2(img2));
H = zeros(size(img));
[x,y]=size(H); cx = x/2; cy = y/2;
Gl = .5; Gh = 2; Gdiff = Gh - Gl; c=1; Do = 10;
k = zeros(x,y);
for i=1:x
for j=1:y
D(i,j)
= sqrt([i-cx]^2 + [j-cy]^2);
H(i,j) = Gdiff*[1 - exp(-c*(D(i,j)/Do)^2)] + Gl;
k(i,j) = (-1)^(i+j);
end
end
imgen = real(exp(ifft2(H.*imgfft))-1).*k;
figure;
subplot(1,2,1); imshow(imscale01(img));
title('Original Image');
subplot(1,2,2); imshow(imscale01(imgen));
title('Homomorphic Image: c=1, D_o=10, G_l=.5, G_h=2');

Original Image

Homomorphic Image: c=1, Do=10, Gl=.5, Gh=2

Acknowledge: I would like to thank my former student Brad Ratliff for providing these
images.

You might also like