Documente Academic
Documente Profesional
Documente Cultură
Page 1
Lecture notes
Introduction
The Fourier Transform is of essential importance to all
signal processing, and of course, also to image processing.
It allows us to perform tasks which would be impossible to
perform any other way and its efficiency allows us to
perform other tasks more quickly.
The Fourier Transform provides, among other things, a
powerful alternative to linear spatial filtering; it is more
efficient to use the Fourier transform than a spatial filter for
a large filter.
The Fourier Transform also allows us to isolate and process
particular image frequencies, and so perform low-pass and
high-pass filtering with a great degree of precision.
Jorma Kekalainen
958
Jorma Kekalainen
959
Page 2
Lecture notes
Jorma Kekalainen
960
Jorma Kekalainen
961
Page 3
Lecture notes
Note
When we deal with a discrete function, as we
shall do for images, the situation changes
slightly.
Since we only have to obtain a finite number
of values, we only need a finite number of
functions to do it.
Jorma Kekalainen
962
Example
Consider the discrete
sequence
963
Page 4
Lecture notes
Jorma Kekalainen
964
The formula for the inverse DFT is very similar to the forward
transform:
Jorma Kekalainen
965
Page 5
Lecture notes
Jorma Kekalainen
966
Note
Jorma Kekalainen
967
Page 6
Lecture notes
Note
Perhaps, more familiar forms
2
DFT{x( n)} X( k ) x( n) exp j kn , k=0,1, ,N-1
N
n =0
N 1
x( n) = IDFT{X( k )}
1 N 1
2
X( k ) exp j kn , n=0,1, ,N-1
N k=0
N
x(n) X(k)
Jorma Kekalainen
968
Example
Express DFT of the sequence
1 , n = 0,1,2,6,7
x( n ) =
3 n 5
0 ,
Jorma Kekalainen
969
Page 7
Lecture notes
Example
Lets substitute the given sequence x
1 , n = 0,1,2,6,7
x( n) =
3 n 5
0 ,
N
n=0
, k = 0,1,L, N 1
3
7
X( k ) = 1 + exp j k + exp j k + exp j k + exp j k
4
2
2
4
7
3
= 1 + exp j k + exp j k + exp j k + exp j k
4 2
2
4
Jorma Kekalainen
= 1 + exp j k + exp j k + exp j k + exp j k
2
4
4
2
= 1 + 2 cos k + 2 cos k , k = 0,1,L ,7
4
2Image
Processing
Digital
970
Example
Matlab calculations
k=0:7;
X(k+1)=1+2*cos(pi*k/4)+2*cos(pi*k/2)
X=
Columns 1 through 8
5.0000 2.4142 -1.0000 -0.4142 1.0000 -0.4142 -1.0000 2.4142
or
X(k+1)=1+exp(-i*pi*k/4)+exp(-i*pi*k/2)+exp(-i*3*pi*k/2)+exp(-i*7*pi*k/4)
X=
Columns 1 through 4
5.0000
Columns 5 through 8
Jorma Kekalainen
1.0000
971
Page 8
Lecture notes
Jorma Kekalainen
972
Example
>> a=[1,2,3,4,5,6]
a=
1 2 3 4 5 6
>> fft(a')
ans =
21.0000 + 0.0000i
-3.0000 + 5.1962i
-3.0000 + 1.7321i
-3.0000 + 0.0000i
-3.0000 - 1.7321i
-3.0000 - 5.1962i
Note: To apply a DFT to a single vector in Matlab, we should use a
Jorma Kekalainen
Digital Image Processing
column
vector.
973
Page 9
Lecture notes
Exercise
Give the same input vector x' to function muunnos1 and
Matlab fft-function.
Comparing outputs show that muunnos1 makes DFT.
Hint: Give the vector so that its length is a power of 2,
e.g., x =[1 1 1 0 0 0 1 1] or x=rand(1,128)
Jorma Kekalainen
974
function [X]=muunnos1(x)
function [X]=muunnos1(x)
N=length(x);
P=log10(N)/log10(2);
J=1;
for I=1:N-1;
if I<J;
A=x(J);x(J)=x(I);x(I)=A;end;
K=N/2;
while K<J; J=JK;K=K/2;end;
J=J+K;
end;
Jorma Kekalainen
for L=1:P;
B=2^L;
B1=B/2;
V=1;
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
for I=J:B:N;
C=I+B1;
A=x(C)*V;
x(C)=x(I)-A;
x(I)=x(I)+A;
end;
V=V*W;
end;
end;
975
Page 10
Lecture notes
Solution
>> a=rand(1,256)-0.5+j*(rand(1,256)-0.5);% Inputs
>>X1=muunnos1(a);
>>X2=fft(a);
% Comparing outputs we verify that the function muunnos1 performs DFT.
>>max(abs(X2-X1))
ans=
4.3375e-14
>> figure
plot(real(X2-X1))
title('real(fft-muunnos1)')
grid
figure
plot(imag(X2-X1))
title('imag(fft-muunnos1)')
grid
Jorma Kekalainen
%
976
Re{fft}-Re{muunnos1} and
Im {fft}-Im{muunnos1}
Jorma Kekalainen
977
Page 11
Lecture notes
Exercise
Give the same input vector
xx=[1,1,1,0,0,0,1,1]
to function muunnos2 and Matlab ifftfunction.
Show results are equivalent.
Jorma Kekalainen
978
function [x]=muunnos2(x)
function [x]=muunnos2(x)
% IFFT algorithm
x=x';
% Complex conjugate
N=length(x);
P=log10(N)/log10(2);
J=1;
for I=1:N-1;
if I<J; A=x(J);x(J)=x(I);x(I)=A;end;
K=N/2;
while K<J; J=J-K;K=K/2;end;
J=J+K;
end;
%
Jorma Kekalainen
for L=1:P;
B=2^L;
B1=B/2;
V=1;
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
for I=J:B:N;
C=I+B1;
A=x(C)*V;
x(C)=x(I)-A;
x(I)=x(I)+A;
end;
V=V*W;
end;
end;
x=x'/N; % IFFT
979
Page 12
Lecture notes
Solution
>> xx=[1,1,1,0,0,0,1,1]
xx =
1 1 1 0 0 0 1 1
>> ifft(xx)
ans =
Columns 1 through 7
0.6250 0.3018 -0.1250 -0.0518 0.1250 -0.0518 -0.1250
Column 8
0.3018
>> muunnos2(xx)
ans =
Columns 1 through 3
0.6250 + 0.0000i 0.3018 - 0.0000i -0.1250 + 0.0000i
Columns 4 through 6
-0.0518 + 0.0000i 0.1250 + 0.0000i -0.0518 + 0.0000i
Columns 7 through 8
-0.1250 + 0.0000i 0.3018 - 0.0000i
Jorma Kekalainen
980
Solution
>> ifft(xx')
ans =
0.6250
0.3018
-0.1250
-0.0518
0.1250
-0.0518
-0.1250
0.3018
Jorma Kekalainen
>> muunnos2(xx')
ans =
0.6250 + 0.0000i
0.3018 - 0.0000i
-0.1250 + 0.0000i
-0.0518 + 0.0000i
0.1250 + 0.0000i
-0.0518 + 0.0000i
-0.1250 + 0.0000i
0.3018 - 0.0000
Digital Image Processing
981
Page 13
Lecture notes
Exercise
Combine the functions of the previous
exercises so that one and the same function
performs FFT and IFFT, for example, with
commands like styles muunnos3('fft',x) or
muunnos3('ifft',x)
Jorma Kekalainen
982
function [x]=muunnos3(type,x)
for L=1:P;
function [x]=muunnos3(type,x)
B=2^L;
% FFT and IFFT algorithm
B1=B/2;
% type='fft' or 'ifft'
V=1;
if strcmp(type,'ifft');x=x';end% Conjugate
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
N=length(x);
for I=J:B:N;
P=log10(N)/log10(2);
C=I+B1;
J=1;
A=x(C)*V;
for I=1:N-1;
x(C)=x(I)-A;
x(I)=x(I)+A;
if I<J; A=x(J);x(J)=x(I);x(I)=A;end;
end;
K=N/2;
V=V*W;
while K<J; J=J-K;K=K/2;end;
end;
J=J+K;
end;
end;
if strcmp(type,'ifft');x=x'/N;end% IFFT
Jorma Kekalainen
Digital Image Processing
983
%
Page 14
Lecture notes
>> fft(x')
ans =
5.0000
2.4142
-1.0000
-0.4142
1.0000
-0.4142
-1.0000
2.4142
Jorma Kekalainen
1]
>> muunnos3('fft',x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 + 0.0000i
-0.4142 - 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 + 0.0000i
2.4142 + 0.0000i
Digital Image Processing
984
Jorma Kekalainen
>> ifft(XX)
ans =
1.0000
1.0000
1.0000
0
0.0000
0
1.0000
1.0000
>> muunnos3('ifft',XX)
ans =
1.0000 + 0.0000i
1.0000 - 0.0000i
1.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
1.0000 + 0.0000i
1.0000 - 0.0000i
985
Page 15
Lecture notes
Note
>> a=rand(1,8)-0.5+j*(rand(1,8)-0.5)
a=
Columns 1 through 3
0.3147 + 0.4575i 0.4058 + 0.4649i -0.3730 - 0.3424i
Columns 4 through 6
0.4134 + 0.4706i 0.1324 + 0.4572i -0.4025 - 0.0146i
Columns 7 through 8
-0.2215 + 0.3003i 0.0469 - 0.3581i
>> a'
ans =
0.3147 - 0.4575i
0.4058 - 0.4649i
-0.3730 + 0.3424i
0.4134 - 0.4706i
0.1324 - 0.4572i
-0.4025 + 0.0146i
-0.2215 - 0.3003i
0.0469 + 0.3581i
Jorma Kekalainen
Digital Image Processing
986
987
Page 16
Lecture notes
Jorma Kekalainen
>> muunnos3('fft',a')
ans =
0.3162 - 1.4353i
0.2124 - 0.4326i
0.7038 - 0.4999i
-1.6977 - 1.2294i
-0.6110 - 0.3098i
1.4377 + 0.7349i
1.3794 - 1.4137i
0.7771 + 0.9257i
988
989
Page 17
Lecture notes
>> muunnos3('ifft',a')
ans =
0.0395 - 0.1794i
0.0971 + 0.1157i
0.1724 - 0.1767i
0.1797 + 0.0919i
-0.0764 - 0.0387i
-0.2122 - 0.1537i
0.0880 - 0.0625i
0.0265 - 0.0541i
Jorma Kekalainen
990
Verification of muunnos3
a=rand(1,256)-0.5+j*(rand(1,256)-0.5);
X1=[];X2=[];
X1=[];X2=[];
X1=muunnos3('fft',a);
X2=fft(a);
max(abs(X2-X1))
ans =
3.2610e-14
figure
plot(real(X2-X1))
title('real(fft-muunnos3)')
grid
figure
plot(imag(X2-X1))
title('imag(fft-muunnos3)')
grid
Jorma Kekalainen
x1=[];
x2=[];
x1=muunnos3('ifft',a);
x2=ifft(a);
max(abs(x2-x1))
ans =
1.1857e-16
figure
plot(real(x2-x1))
title('real(ifft-muunnos3)')
grid
figure
plot(imag(x2-x1))
title('imag(ifft-muunnos3)')
grid
Digital Image Processing
991
Page 18
Lecture notes
Verification of muunnos3
a=rand(1,256)-0.5+j*(rand(1,256)-0.5);
Jorma Kekalainen
992
Comparison of results
>> x'
ans =
1
1
1
0
0
0
1
1
x = Jorma
1 Kekalainen
1 1
>> fft(x')
ans =
5.0000
2.4142
-1.0000
-0.4142
1.0000
-0.4142
-1.0000
2.4142
0
>> muunnos1(x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 + 0.0000i
-0.4142 - 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 + 0.0000i
2.4142 + 0.0000i
0
>> dftv(x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 - 0.0000i
-0.4142 + 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 - 0.0000i
2.4142 + 0.0000i
993
Page 19
Lecture notes
Comparison of results
>> a
a=
2
3
4
5
6
7
8
1
>> fft(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
Jorma Kekalainen
>> muunnos1(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> dftv(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
994
Simple DF transformer
Jorma Kekalainen
995
Page 20
Lecture notes
996
Example
Let it be vectors
f=[1,2,3,4,5,6,7,8]
g=[2,4,6,8,10,12,14,16]
and scalars p=3, q=4
First lets calculate h=pf+qg and H=fft(h')
then PF=fft(pf') and QG=fft(qg') and H1=PF+QG
Finally, we compare H to H1
Jorma Kekalainen
997
Page 21
Lecture notes
h=
11 22 33 44 55 66 77 88
H=
1.0e+02 *
3.9600 + 0.0000i
-0.4400 + 1.0623i
-0.4400 + 0.4400i
-0.4400 + 0.1823i
-0.4400 + 0.0000i
-0.4400 - 0.1823i
-0.4400 - 0.4400i
-0.4400 - 1.0623i
Jorma Kekalainen
998
QG =
1.0e+02 *
2.8800 + 0.0000i
-0.3200 + 0.7725i
-0.3200 + 0.3200i
-0.3200 + 0.1325i
-0.3200 + 0.0000i
-0.3200 - 0.1325i
-0.3200 - 0.3200i
-0.3200 - 0.7725i
Digital Image Processing
999
Page 22
Lecture notes
H1=PF+QG vs. H
>> H-H1
H1 =
H=
ans =
1.0e+02 *
1.0e+02 *
3.9600 + 0.0000i
3.9600 + 0.0000i 1.0e-13 *
0.0000 + 0.0000i
-0.4400 + 1.0623i -0.4400 + 1.0623i
0.0000 + 0.1421i
-0.4400 + 0.4400i -0.4400 + 0.4400i
0.0000 + 0.0000i
-0.4400 + 0.1823i -0.4400 + 0.1823i 0.0000 + 0.0000i
-0.4400 + 0.0000i -0.4400 + 0.0000i 0.0000 + 0.0000i
-0.4400 - 0.1823i -0.4400 - 0.1823i 0.0000 + 0.0000i
-0.4400 - 0.4400i -0.4400 - 0.4400i 0.0000 + 0.0000i
-0.4400 - 1.0623i -0.4400 - 1.0623i 0.0000 - 0.1421i
Jorma Kekalainen
1000
>> 3*fft(f')+4*fft(g')
ans =
1.0e+02 *
3.9600 + 0.0000i
-0.4400 + 1.0623i
-0.4400 + 0.4400i
-0.4400 + 0.1823i
-0.4400 + 0.0000i
-0.4400 - 0.1823i
-0.4400 - 0.4400i
-0.4400 - 1.0623i
Digital Image Processing
1001
Page 23
Lecture notes
1002
Example
>> x = [2 3 4 5 6 7 8 1]
x=
2 3 4 5 6 7
>> X=fft(x')
X=
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> x1=(-1).^[0:7].*x
x1 =
2 -3 4 -5 6 -7
>> X1=fft(x1')
X1 =
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
8 -1
Jorma Kekalainen
1003
Note:
The first four elements of X areDigital
theImage
lastProcessing
four elements of X1, and vice versa.
Page 24
Lecture notes
Example
>> x = [2 3 4 5 6 7 8 1];
>> X=fft(x')
36.0000
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> x1=(-1).^[0:7].*x
x1 =
2 -3 4 -5 6 -7 8 -1
>> X1=fft(x1')
X1 =
4.0000
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
36.0000
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
Jorma Kekalainen
1004
Note:
The first four elements of X areDigital
theImage
lastProcessing
four elements of X1, and vice versa.
Conjugate symmetry
If x is real, and of length N , then its DFT X satisfies the
condition that
is the complex conjugate of XN-k , for all k=1,2, , N-1.
So in our example of length 8, we have
In this case we also have X4 = conj(X4), which means X4 must
X0=4.0000
be real.
X1=1.6569 + 4.0000i
In fact if N is even, then XN/2 will be real,
X2=-4.0000 + 4.0000i
X3=-9.6569 - 4.0000i
as we can see above.
X =36.0000
4
Jorma Kekalainen
X5=-9.6569 + 4.0000i
X6=-4.0000 - 4.0000i
1005
X7=1.6569 - 4.0000i
Page 25
Lecture notes
1006
1007
Page 26
Lecture notes
Jorma Kekalainen
1008
Exercise
How length the vector (2n) must be if the
increase in speed with FFT compared to the
direct method is at least 1000 times?
Jorma Kekalainen
1009
Page 27
Lecture notes
1010
Corrugation functions
A one-dimensional function can be written as
a sum of sines and cosines.
Given that an image may be considered as a
two-dimensional function f(x,y), it seems
reasonable to assume that f can be expressed
as sums of corrugated (wave) functions
which have the general form
z=asin(bx +cy).
Jorma Kekalainen
1011
Page 28
Lecture notes
Corrugation functions
A sample such function is shown below.
Actually, this is exactly what the two-dimensional Fourier
transform does: it rewrites the original matrix in terms of
sums of corrugations.
More precisely: the magnitude of the Fourier transform is a
matrix (the same size as the starting matrix) whose elements
are fij .
fij give the height of
the corrugation for
which the horizontal
and vertical
distances between
consecutive wave
bottoms are 1/i and
1/j. Jorma Kekalainen
1012
Jorma Kekalainen
1013
Page 29
Lecture notes
Note: For notational convenience we assume that the x indices are from 0 to (M-1)
Jorma Kekalainen
Digital Image Processing
1014
and the y indices are from 0 to (N-1),
Note
Page 30
Lecture notes
1016
Jorma Kekalainen
1017
Page 31
Lecture notes
Separability
Notice that the Fourier transform filter elements can be
expressed as products:
1018
Separability
This means that we can break down our long formulas above
to simpler formulas that work on single rows or columns e.g.:
1019
Page 32
Lecture notes
DFT of
each row
DFT of
each
column
1020
Linearity
An important property of the DFT is its linearity; the DFT of a
sum is equal to the sum of the individual DFT's, and the same
goes for scalar multiplication:
d=f+n
where f is the original image; n is the noise and d is the
degraded image.
Jorma Kekalainen
1021
Page 33
Lecture notes
Linearity
Since
we may be able to remove or reduce n by modifying
the transform.
Some noise appears on the DFT in a way which
makes it particularly easy to remove.
Jorma Kekalainen
1022
Convolution theorem
This result provides one of the most powerful
advantages of using the DFT.
Suppose we wish to convolve an image M with a spatial
filter S.
Our method has been place S over each pixel of M in
turn, calculate the product of all corresponding gray
values of M and elements of S , and add the results.
The result is called the digital convolution of M and S,
and is denoted MS.
This method of convolution can be very slow, especially
if S is large.
Jorma Kekalainen
1023
Page 34
Lecture notes
Application of convolution
theorem
The convolution theorem states that the result
MS can be obtained by the following sequence
of steps:
1. Pad S with zeroes so that is the same size as M;
denote this padded result by S.
2. Form the DFTs of both M and S, to obtain
F(M) and F(S).
3. Form the element - by - element product of
these two transforms:
F(M) F(S).
Jorma Kekalainen
1024
Application of convolution
theorem
4. Take the inverse transform of the result:
F -1(F(M) F(S)).
The convolution theorem states:
M S = F -1(F ( M) F (S))
or equivalently that
F(M S) = F(M) F(S).
Although this might seem like an unnecessarily
clumsy and roundabout way of computing a
convolution, it can have enormous speed
advantages if S is large.
Jorma Kekalainen
1025
Page 35
Lecture notes
Example
Jorma Kekalainen
1026
DC coefficient
If we put u = v = 0 in the definition given in equation
we get
1027
Page 36
Lecture notes
Shifting a DFT
For purposes of display, it is convenient to have the DC
coefficient in the centre of the matrix.
This will happen if all elements f(x,y) in the matrix are
multiplied by (-1)x+y before the transform.
Figure below demonstrates how the matrix is shifted by this
method.
In each diagram the DC coefficient is the top left hand
element of submatrix A and is shown as a black square.
FFT
Jorma Kekalainen
After shifting
1028
Conjugate symmetry
An analysis of the Fourier transform definition
leads to a symmetry property; if we make the
substitutions u=-u and v=-v in the equation
we find that
For integers
p and q
Jorma Kekalainen
1029
Page 37
Lecture notes
Jorma Kekalainen
1030
Displaying transforms
Having obtained the Fourier transform F(u, v) of an
image f(x, y) we would like to see what it looks like.
As the elements F(u, v) are complex numbers, we cant
view them directly, but we can view their magnitude
|F(u, v)|.
Since these will be numbers of type double, generally
with large range, we have two approaches
1. find the maximum value m of |F(u, v)| (this will be the
DC coefficient), and use imshow to view |F(u, v)|/m,
2. use mat2gray to view |F(u, v)| directly.
Jorma Kekalainen
1031
Page 38
Lecture notes
1032
Jorma Kekalainen
1033
Page 39
Lecture notes
Example
Suppose we take a constant matrix f(x, y) = 1.
Going back to the idea of a sum of corrugations, we
see that in fact no corrugations are required to form
a constant.
Thus we would hope that the DFT consists of a DC
coefficient and zeroes everywhere else.
We will use the ones function, which produces an
(n x n) matrix consisting of 1s, where n is an input to
the function.
Jorma Kekalainen
1034
Example
>> a=ones(8);
>> fft2(a)
The result is indeed as we expected:
ans =
64 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Note that the DC coefficient is indeed the sum of all the matrix values.
Jorma Kekalainen
1035
Page 40
Lecture notes
Example
>> a=ones(8)
a=
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
>> fft2=fft2(a)
fft2 =
64 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Note:
Jorma Kekalainen
1036
Example
Now we'll take a matrix consisting of a single corrugation:
>> a = [100 200; 100 200];
>> a = repmat(a,4,4)
ans =
Jorma Kekalainen
1037
Page 41
Lecture notes
Example
>> af = fft2(a)
ans =
What we have here is really the sum of two matrices: a constant matrix
each element of which is 150, and a corrugation which alternates -50 to 50
from left to right.
The constant matrix alone would produce (as in example 1), a DC
coefficient alone of value 64*150=9600; the corrugation a single value.
By linearity, the DFT will consist of just the two values.
Jorma Kekalainen
1038
Example
Jorma Kekalainen
100
100
100
100
100
100
100
100
200
200
200
200
200
200
200
200
100
100
100
100
100
100
100
100
200
200
200
200
200
200
200
200
1039
Page 42
Lecture notes
Example
af =
9600
Jorma Kekalainen
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-3200
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1040
Example
We will take here a single step edge:
>> a = [zeros(8,4) ones(8,4)]
a=
Jorma Kekalainen
1041
Page 43
Lecture notes
round(abs(af))
%
a=
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
ans =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 9 0 21 32 21 0 9
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Jorma Kekalainen
0 0 0 0 0 0 0 0
1042
Jorma Kekalainen
The mirroring of
values about the
DC coefficient is
a consequence
of the symmetry
of the DFT.1043
Page 44
Lecture notes
Example
Lets create a few simple images, and see what the Fourier
transform produces.
The first example is a single edge
>> a=[zeros(256,128) ones(256,128)];
This is displayed
Then we shall take its DFT,
and shift it:
>> af=fftshift(fft2(a));
Jorma Kekalainen
1044
Example
Now we'll view its spectrum; we have the choice of two
commands:
1. afl=log(1+abs(af));%af is shifted DFT
imshow(afl/afl(129,129))
This normalization works because after shifting, the DC
coefficient is at position x=129 and y=129.
We stretch the transform using log, and divide the result by
the middle value to obtain matrix of type double with values
in the range 0.0 1.0.
This can then be viewed directly with imshow.
2. imshow(mat2gray(log(1+abs(af))))
The mat2gray function automatically scales a matrix for
display as an image.
Jorma Kekalainen
1045
Page 45
Lecture notes
Jorma Kekalainen
1046
function fftshow(f,type)
% Function to display a Fourier transform
%fftshow(f,'log') and fftshow(f,'abs')
% Usage: FFTSHOW(F,TYPE)
%
% Displays the fft matrix f using imshow,
% where TYPE must be one of
% 'abs' or 'log'. If TYPE='abs', then then abs(f)
% is displayed; if
% TYPE='log' then log(1+abs(f)) is displayed.
% If TYPE is omitted, then
% 'log' is chosen as a default.
%
% Example:
% c=imread('image.tif');
% cf=fftshift(fft2(c));
% fftshow(cf,'abs')
%
Jorma Kekalainen
if nargin<2,
type='log';
end
if (type=='log')
fl = log(1+abs(f));
fm = max(fl(:));
imshow(im2uint8(fl/fm))
elseif (type=='abs')
fa=abs(f);
fm=max(fa(:));
imshow(fa/fm)
else
error('TYPE must be abs or log.');
end;
1047
Page 46
Lecture notes
fftshow(f,type) function
function [ ] = fftshow(f,type)
if nargin<2,
%Function to display a Fourier transform
type='log';
%fftshow(f,'log') and fftshow(f,'abs')
end
%
if (type=='log')
% Usage: FFTSHOW(F,TYPE)
fl = log(1+abs(f));
%
fm = max(fl(:));
% Displays the fft matrix f using imshow,
where %TYPE must be one of 'abs' or 'log'.
imshow(im2uint8(fl/fm))
%If TYPE='abs', then then abs(f) is displayed;
elseif (type=='abs')
%if TYPE='log' then log(1+abs(f)) is displayed.
fa=abs(f);
%If TYPE is omitted, then 'log' is chosen as a
fm=max(fa(:));
%default.
imshow(fa/fm)
%
else
% Example:
error('TYPE must be abs or log.');
% c=imread('image.tif');
% cf=fftshift(fft2(c));
end
% fftshow(cf,'abs')
Jorma
Digital Image Processing
1048
% Kekalainen
Using fftshow(f,type)
Then for example
fftshow(af,'log')
will show the logarithm of the absolute
values of the transform log(1+abs(f)),
and
fftshow(af,'abs')
will show the absolute values of the
transform abs(f) without any scaling.
An example command sequence is
shown on the right.
Jorma Kekalainen
% Single edge
a=[zeros(256,128) ones(256,128)];
figure,imshow(a)
title('Single edge')
af=fftshift(fft2(a));
figure,fftshow(af,'abs')
title('DFT of the single edge')
xlabel('fftshow(af,''abs'')')
figure,fftshow(af,'log')
title('DFT of the single edge')
xlabel('fftshow(af,''log'')')
1049
Page 47
Lecture notes
Jorma Kekalainen
1050
Note
DFT of the single edge with fftshow(af,'abs')
Note that the line is seen only in enlarged scale!
Jorma Kekalainen
1051
Page 48
Lecture notes
Example
Now we'll create a box, and
then its Fourier transform:
a=zeros(256,256);
A box and its DFT
a(78:178,78:178)=1;
imshow(a)
title('Box')
af=fftshift(fft2(a));
figure,fftshow(af,'abs')
title('DFT of the box')
xlabel('fftshow(af,''abs'')')
figure,fftshow(af,'log')
title('DFT of the box')
xlabel('fftshow(af,''log'')')
Jorma Kekalainen
1052
Example: Box
Jorma Kekalainen
1053
Page 49
Lecture notes
A
rotated
box and
its DFT
Example
Now we shall look at a box
rotated 45
>> [x,y]=meshgrid(1:256,1:256);
>> b=(x+y<329)&(x+y>182)&(xy>-67)&(x-y<73);
>> imshow(b)
>> bf=fftshift(fft2(b));
>> figure,fftshow(bf)
Note that the transform of
the rotated box is the rotated
transform of the original box.
Jorma Kekalainen
1054
Note: meshgrid
[X,Y] = meshgrid(xgv,ygv)
replicates the grid vectors xgv
and ygv to produce a full grid.
>> [x,y]=meshgrid(1:5,1:5)
x=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
y=
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
Jorma Kekalainen
5 5 5 5 5
>> [x,y]=meshgrid(-4:3,-4:3)
x=
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
y=
-4 -4 -4 -4 -4 -4 -4
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
Digital Image
3 Processing
3 3 3 3 3 3
3
3
3
3
3
3
3
3
-4
-3
-2
-1
0
1
2
3
1055
Page 50
Lecture notes
Note
Jorma Kekalainen
1056
Example
We will create a small circle,
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<15);
and then transform and display it:
cf=fft2shift(fft2(z));
>> fftshow(cf,'log')
Jorma Kekalainen
1057
Page 51
Lecture notes
Note
>> x=1:5
x=
1 2 3
[X,Y] = meshgrid(xgv,ygv)
replicates the grid vectors xgv
and ygv to produce a full grid.
>> [x,y]=meshgrid(1:5,1:5)
x=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
y=
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Jorma Kekalainen
>> z=sqrt(x.^2+y.^2)
z=
1.4142 2.2361 3.1623
2.2361 2.8284 3.6056
3.1623 3.6056 4.2426
4.1231 4.4721 5.0000
5.0990 5.3852 5.8310
4.1231
4.4721
5.0000
5.6569
6.4031
5.0990
5.3852
5.8310
6.4031
7.0711
1058
Note
>> [x,y]=meshgrid(-4:3,-4:3)
x=
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
y=
-4 -4 -4 -4 -4 -4 -4
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
Jorma Kekalainen
3
3
3
3
3 >> z=sqrt(x.^2+y.^2)
3 z=
5.6569 5.0000 4.4721
3
5.0000 4.2426 3.6056
3
4.4721 3.6056 2.8284
-4 4.1231 3.1623 2.2361
-3 4.0000 3.0000 2.0000
-2 4.1231 3.1623 2.2361
-1 4.4721 3.6056 2.8284
5.0000 4.2426 3.6056
0
1
2
3
Digital Image Processing
4.1231
3.1623
2.2361
1.4142
1.0000
1.4142
2.2361
3.1623
1059
Page 52
Lecture notes
Jorma Kekalainen
1060
Comment
Note the ringing in the Fourier transform.
This is an effect associated with the sharp cutoff the
circle.
As we have seen from both the edge and box images in
the previous examples, an edge appears in the
transform as a line of values at right angles to the edge.
We may consider the values on the line as being the
coefficients of the appropriate corrugation functions
which sum to the edge.
With the circle, we have lines of values radiating out
from the circle; these values appear as circles in the
transform.
Jorma Kekalainen
1061
Page 53
Lecture notes
Example
Jorma Kekalainen
Example
Jorma Kekalainen
1063
Page 54