Sie sind auf Seite 1von 7

Lecture 7: Discrete Fourier Transform in 2D

In the previous lecture we introduced the discrete Fourier transform as given


either by summations or as a matrix vector product. The discrete Fourier transform of of
a vector with n components f = [ f (0) f (1) " f (n − 1)] is another vector whose kth
component is

n −1 n −1
F (k ) = ∑ e − i (2π k ) j / n f ( j ) = ∑ e − i (2π / n ) kj f ( j ).
j =0 j =0

Three very important properties include trig functions, inverse discrete Fourier
transforms and the convolution identity. These allowed us to formulate a “filter” to purge
images of unwanted periodic noise, see the Matlab code fftsine.m
In this lecture we will extend this to 2D images where f is now a nx x ny matrix
with components f(jx, jy), 0 ≤ jx ≤ nx – 1 and 0 ≤ jy ≤ ny – 1. The 2D discrete
Fourier transform of a matrix f is another matrix of the same dimension whose (kx, ky)
component is

ny −1 nx −1
F (kx, ky ) = ∑ ∑e
jy = 0 jx = 0
− i [(2π / nx ) kx jx + (2π / ny ) ky jy ]
f ( jx, jy ).

The inverse 2D discrete Fourier transform has the form

1 ny −1 nx −1
f ( jx, jy ) = ∑
nx ny ky =0
∑e
kx = 0
i [(2π / nx ) kx jx + (2π / ny ) ky jy ]
F (kx, ky ).

Both the 2D discrete Fourier transform and its inverse can easily be computed by
using the Matlab commands fft2() and ifft2(). For example for nx = 2 and ny = 3:

>> a = [1 2 5;6 7 11]

a= 1 2 5
6 7 11
>> ffta = fft2(a)

ffta = 32.0000 -5.5000 + 6.0622i -5.5000 - 6.0622i


-16.0000 0.5000 - 0.8660i 0.5000 + 0.8660i

>> ifft2(ffta)

ans = 1 2 5
6 7 11.

As in the 1D Fourier transform of the sine and cosine functions, the Fourier
transforms will identify the frequencies as “spikes” in the matrix of the Fourier
transforms. Let x = 0: 1/nx:1-1/nx and y = 0: 1/ny:1-1/ny.
The discrete Fourier transform of cos(2π(fx x + fy y)) is

nx ny
, when (kx, ky ) = ± ( f x , f y )
2
0, when (kx, ky ) ≠ ±( f x , f y ).

The discrete Fourier transform of sin(2π(fx x + fy y)) is

nx ny
i, when (kx, ky ) = −( f x , f y )
2
nx ny
− i, when (kx, ky ) = ( f x , f y )
2
0, when (kx, ky ) ≠ ±( f x , f y ).

The following Matlab code illustrates the above properties. Here the padded fast
Fourier transform has been used, and the frequency domains have been shifted so that the
smallest frequencies are in the center. The sine and cosine functions have different
amplitudes, which may be identified in the mesh() plot of log(1+abs(fftu)) where fftu is
the Fourier transform.
Matlab Code ffttrig2d.m

nx = 500;
ny = 500;
for i = 1:nx
for j=1:ny
u(i,j) = 1 + ...
2^0*(1+sin(2*pi*((i-1)/nx)*200))+...
2^2.*(1+sin(2*pi*((j-1)/ny)*200))+...
2^4.*(1+cos(2*pi*((i-1)/nx+(j-1)/ny)*141))+...
2^6.*(1+sin(2*pi*((i-1)/nx-(j-1)/ny)*141));
end
end
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
mesh(log(1+abs(fftu)));
The convolution identity also generalizes to the 2D Fourier transform. Let a be a
nx x ny matrix with components a(i,j). The polynomial of two variables x and y
associated with this matrix is

ny −1 nx −1
pa ( x, y ) = ∑ ∑ a (i, j ) xi y j .
j =0 i =0

The convolution of two nx x ny matrices, conv2(a,b), is a (2 nx –1) x (2 ny –1) matrix


such that

pconv 2( a ,b ) ( x, y ) = pa ( x, y ) pb ( x, y ).

For example, consider nx = 2 and ny = 3 where we have again used Matlab:

>> a = [1 2 5; 6 7 11]
1 2 5
6 7 11

>> b = [2 5 9; 1 2 8]
2 5 9
1 2 8

>> conv2(a,b)
2 9 29 43 45
13 48 128 144 139
6 19 73 78 88.

The coefficient in pconv2(a,b)(x,y) of x2y4 is

88 = 11*8 = a(1,2)*b(1,2).

The coefficient in pconv2(a,b)(x,y) of x2y3 is

78 = 7*8 + 11*2 = a(1,1)*b(1,2) + a(1,2)*b(1,1).


The convolution identity in 2D also requires the matrices to be “padded” by
zeros in both the rows and columns. The justification of this identity is similar to that
given for the 1D Fourier transformation. Here we have used the Matlab notation for fast
Fourier transforms in 2D.

fft 2(conv 2( a, b)) = fft 2(a, 2* nx − 1, 2* ny − 1).* fft 2(b, 2* nx − 1, 2* ny − 1).

The following Matlab calculation verifies this for the above two matrices:

>> fft2(conv2(a,b))

1.0e+002 *

Columns 1 through 4

8.6400 -2.7193 + 2.0698i -1.0757 + 0.8192i -1.0757 - 0.8192i


-2.4000 - 1.8013i 1.0034 + 0.1752i 0.2677 - 0.1158i 0.2249 + 0.4680i
-2.4000 + 1.8013i 0.5290 - 0.9709i 0.2249 - 0.4680i 0.2677 + 0.1158i

Column 5

-2.7193 - 2.0698i
0.5290 + 0.9709i
1.0034 - 0.1752i

>> norm(fft2(conv2(a,b))-fft2(a,3,5).*fft2(b,3,5))

4.8969e-014.

The convolution identity can be restated by taking the inverse Fourier transform
of both sides

conv 2( a, b) = ifft 2( fft 2(a, 2* nx − 1, 2* ny − 1) .* fft 2(b, 2* nx − 1, 2* ny − 1) ).

If b is an image, then its Fourier transform will reflect the frequencies of the periodic
parts of the image. By masking or filtering out the unwanted frequencies one can
obtained a new image by applying the inverse Fourier transformation. A filter is a matrix
with the same dimension as the Fourier transform of the padded image. The components
of the filter usually vary from 0 to 1. If the component is 1, then the frequency is allowed
to pass; if the component is 0, then the frequency is tossed out. Let Filter represent such a
matrix. Then the filtered image is Newb

Newb = ifft 2( Filter .* fft 2(b, 2* nx − 1, 2* ny − 1) ).

The following Matlab code illustrates this for a low frequency sine wave with higher
frequency sine “noise.”

Matlab Code fftsine2d.m

clear;
nx = 200;
ny = 600;
for i = 1:nx % Define the big wave with periodic noise.
for j = 1:ny
u(i,j) = 0+100*(1+sin(2*pi*((j-1)/ny)*5)) + ...
15.*(1+sin(2*pi*((i-1)/nx)*80))+...
15.*(1+sin(2*pi*((j-1)/ny)*80));
end
end
sine = uint8(u);
imwrite(sine, 'sine.jpg');
fftu = fft2(u,2*nx-1,2*ny-1); % Padded fft.
fftu = fftshift(fftu);
figure(1);
mesh(u');
figure(2)
mesh(log(1+(abs(fftu))));
filter = ones(2*nx-1,2*ny-1);
d0 = 150; % Use ideal low pass filter.
for i = 1:2*nx-1
for j =1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
if dist > d0
filter(i,j) = 0;
end
end
end
figure(3)
mesh(filter);
fil_sine = filter.*fftu;
figure(4)
mesh(log(1+abs(fil_sine)));
fil_sine = ifftshift(fil_sine);
fil_sine = ifft2(fil_sine,2*nx-1,2*ny-1);
fil_sine = real(fil_sine(1:nx,1:ny));
fil_sine = uint8(fil_sine);
imwrite(fil_sine, 'sine_fil.jpg');
Sine Wave with Noise Fourier Transform of Noisy Sine Wave

Low Pass Filter Filtered Fourier Transform

Das könnte Ihnen auch gefallen