Está en la página 1de 66

2-D Fourier Transforms

Yao Wang
Polytechnic University
University, Brooklyn
Brooklyn, NY 11201

With contribution from Zhu Liu, Onur Guleryuz, and


Gonzalez/Woods, Digital Image Processing, 2ed
Lecture Outline
• Continuous Fourier Transform (FT)
– 1D FT (review)
– 2D FT
• Fourier Transform for Discrete Time Sequence
(DTFT)
– 1D DTFT (review)
– 2D DTFT
• Linear
Li C
Convolution
l ti
– 1D, Continuous vs. discrete signals (review)
– 2D
• Filter Design
• Computer Implementation

Yao Wang, NYU-Poly EL5123: Fourier Transform 2


What is a transform?
• Transforms are decompositions of a function f(x)
into some basis functions Ø(x, u). u is typically
the freq. index.

Yao Wang, NYU-Poly EL5123: Fourier Transform 3


Illustration of Decomposition
Φ3
f
α3

f = α1Φ1+α2Φ2+α3Φ3

o α2
Φ2
α1

Φ1

Yao Wang, NYU-Poly EL5123: Fourier Transform 4


Decomposition
• Ortho-normal basis function
 1, u1  u2
  ( x, u1 ) * ( x, u2 )dx 0, u1  u2
• Forward

Projection of
)  ( x, u ) 
F (u )  f ( x),  f ( x)
*
( x, u )dx
d f(x) onto
 (x,u)

• Inverse

Representing f(x) as sum of
f ( x)   F (u ) ( x, u )du

(x,u) for all u, with weight
F(u)

Yao Wang, NYU-Poly EL5123: Fourier Transform 5


Fourier Transform
• Basis function
 ( x, u )  e j 2ux
, u   , .

• Forward Transform

F (u )  F{ f ( x)}   f ( x)e  j 2ux dx
d


• Inverse Transform

f ( x)  F {F (u )}   F (u )e j 2ux du
1


Yao Wang, NYU-Poly EL5123: Fourier Transform 6


Important Transform Pairs
f ( x)  1  F (u )   (u )
f ( x)  e j 2f0 x F (u )   (u  f 0 )

f ( x)  cos(2f 0 x)  F (u )   (u  f 0 )   (u  f 0 ) 
1
2
f ( x)  sin( 2f 0 x)  F (u ) 
1
 (u  f 0 )   (u  f 0 ) 
2j
 1, x  x0 sin( 2x0u )
f ( x)    F (u )   2 x0 sinc(2 x0u )
0, otherwise u
sin(t )
where, sinc(t ) 
t

Derive the last transform pair in class

Yao Wang, NYU-Poly EL5123: Fourier Transform 7


FT of the Rectangle Function
sin( 2x0u ) sin(t )
F (u )   2 x0 sinc(2 x0u ) where, sinc(t ) 
u t
f(x) f(x) x0=2
x0=1

-1 1 x -2 2 x

Note first zero occurs at u0=1/(2 x0)=1/pulse-width, other zeros are multiples of this.
Yao Wang, NYU-Poly EL5123: Fourier Transform 8
IFT of Ideal Low Pass Signal
• What is f(x)?
F(u)

-u0 u0 u

Yao Wang, NYU-Poly EL5123: Fourier Transform 9


Representation of FT
• Generally, both f(x) and F(u) are complex
• Two representations
– Real and Imaginary F (u )  R(u )  jI (u )
– Magnitude and Phase
F (u )  A(u )e j (u ) , where I
I(u) F(u)
I (u )
A(u )  R(u ) 2  I (u ) 2 ,  (u )  tan 1
R (u
(u )
Φ(u)
R(u) R
• Relationship
R (u )  A(u ) cos  (u ), i  (u )
) I (u )  A(u ) sin
• Power spectrum
P (u )  A(u )  F (u )  F (u )  F (u )
2 * 2

Yao Wang, NYU-Poly EL5123: Fourier Transform 10


What if f(x) is real?
• Real world signals f(x) are usually real
• F(u) is still complex,
complex but has special properties

F * (u )  F (u )
R (u )  R(u ), A(u )  A(u ), P(u )  P (u ) : even function
)  (u )   (u ) : odd function
I (u )   I (u ),

Yao Wang, NYU-Poly EL5123: Fourier Transform 11


Property of Fourier Transform
• Duality f (t )  F (u )
F (t )  f ( u )
• Linearity
F a1 f1 ( x)  a2 f 2 ( x)  a1 F{ f1 ( x)}  a2 F{ f 2 ( x)}
• Scaling
S li
F af ( x)  aF{ f ( x)}
• Translation
f ( x  x0 )  F (u )e  j 2x0u , f ( x)e j 2u0 x  F (u  u0 )
• Convolution
f ( x)  g ( x)   f ( x   ) g ( )d
f ( x)  g ( x)  F (u )G (u )
We will review convolution later!

Yao Wang, NYU-Poly EL5123: Fourier Transform 12


Two Dimension Fourier Transform
• Basis functions

 ( x, y; u , v)  e j ( 2ux  2vyy )  e j 2ux e j 2vyy , u , v   , .


• Forward – Transform
 
F (u, v)  F { f ( x, y )}   
 
f ( x, y )e  j 2 (ux  vy ) dxdy

• Inverse – Transform
 
f ( x, y )  F {F (u , v)}  
1
 F (u , v)e j 2 ( ux  vy )
dudv
 
• Property
P t
– All the properties of 1D FT apply to 2D FT

Yao Wang, NYU-Poly EL5123: Fourier Transform 13


Example 1
f ( x, y )  sin 4x  cos 6y f(x,y)

F {sin 4x}   sin 4xe  j 2 (ux  vy ) dxdy

  sin 4xe  j 2ux dx  e  j 2vy dy


  sin 4xe  j 2ux dx (v)
1
 ( (u  2)   (u  2)) (v)
2j
1
 ( (u  2, v)   (u  2, v)) u
2j F(u,v)
 , x  y  0
where  ( x, y )   ( x) ( y )  
0, otherwise v
1
Likewise, F{cos 6y}  ( (u, v  3)   (u , v  3))
2

Yao Wang, NYU-Poly EL5123: Fourier Transform 14


Example 2
f ( x, y )  sin( 2x  3y ) 
2j
e 
1 j ( 2x 3y )  j ( 2x 3y )
e 
 
F e j ( 2x 3y )   e j ( 2x 3y ) e  j 2 ( xu  yv ) dxdy

  e j 2x e  j 2ux dx  e j 3y e  j 2yv dy


3 3
  (u  1) (v  )   (u  1, v  ) [X,Y]=meshgrid(-2:1/16:2,-2:1/16:2);
[X Y]=meshgrid( 2:1/16:2 2:1/16:2);
2 2 f=sin(2*pi*X+3*pi*Y);

 
imagesc(f); colormap(gray)
3
Likewise, F e  j ( 2x 3y )   (u  1, v  ) Truesize, axis off;
2
Therefore
h f ,
1  3  u
F sin( 2x  3y ) 
3
  (u  1, v  )   (u  1, v  )  F(u,v)
2j 2 2 

Yao Wang, NYU-Poly EL5123: Fourier Transform 15


Important Transform Pairs

sin( 2f x x  2f y y ) 


1
 (u  f x , v  f y )   (u  f x , v  f y )
2j

cos(2f x x  2f y y ) 
1
 (u  f x , v  f y )   (u  f x , v  f y )
2

2D rectangular
g function  2D sinc function

Yao Wang, NYU-Poly EL5123: Fourier Transform 16


Properties of 2D FT (1)
• Linearity
F a1 f1 ( x, y )  a2 f 2 ( x, y )  a1 F{ f1 ( x, y )}  a2 F{ f 2 ( x, y )}

• Translation
 j 2  ( x0 u  y 0 v )
f ( x  x0 , y  y0 )  F (u , v)e ,
f ( x , y ) e j 2 ( u 0 x  v 0 y )  F (u  u0 , v  v0 )
• Conjugation
f * ( x, y )  F * (u ,v)

Yao Wang, NYU-Poly EL5123: Fourier Transform 17


Properties of 2D FT (2)
• Symmetry
f ( x, y ) is real  F (u , v)  F (u ,v)
• Convolution
– Definition of convolution
f ( x, y )  g ( x, y )   f ( x   , y   ) g ( ,  )dd

– Convolution theory
f ( x, y )  g ( x, y )  F (u , v)G (u , v)
We will describe 2D convolution later!

Yao Wang, NYU-Poly EL5123: Fourier Transform 18


Separability of 2D FT and Separable
Signal
• Separability of 2D FT
F2 { f ( x, y )}  Fy {Fx { f ( x, y )}}  Fx {Fy { f ( x, y )}}
– where Fx, Fy are 1D FT along x and y.
– one can do 1DFT for each row of original image, then
1D FT along each column of resulting image
• Separable Signal
– f(x,y) = fx(x)fy(y)
– F(u,v)
( ) = Fx((u)F ) y((v),
)
• where Fx(u) = Fx{fx(x)}, Fy(u) = Fy{fy(y)}
– For separable signal, one can simply compute two 1D
transforms!

Yao Wang, NYU-Poly EL5123: Fourier Transform 19


Example 1
f ( x, y )  sin(3x) cos(5y )

f x ( x)  sin((3x) 
1
Fx (u )   (u  3 / 2)   (u  3 / 2) 
2j

Fy (v)   (v  5 / 2)   (v  5 / 2) 
1
f y ( y )  cos(5y ) 
2
F (u , v)  Fx(u ) Fy (v)
1  3 5 3 5 3 5 3 5 
   (u  , v  )   (u  , v  )   (u  , v  )   (u  , v  ) 
4j 2 2 2 2 2 2 2 2 

Yao Wang, NYU-Poly EL5123: Fourier Transform 20


Example 2
1, | x | x0 , | y | y0
f ( x, y )  
 0, otherwise
F (u , v)  4 x0 y0 sinc(2 x0u ) sinc(2 y0 v)

x
2
u
x0 = 2
y0 = 1

y v
-1
1 1

-2
2

w/ logrithmic mapping
Yao Wang, NYU-Poly EL5123: Fourier Transform 21
Rotation
• Let x  r cos  , y  r sin  , u   cos  , v   sin .
• 2D FT in polar coordinate (r (r, θ) and (ρ(ρ, Ø)
 2
F ( , )    f (r ,  )e  j 2 ( r cos  cos  r sin   sin  ) rdrd
0 0

  f (r ,  )e  j 2r cos(  )
rdrd

• Property
f (r ,   0 )  F ( ,  0 )

Yao Wang, NYU-Poly EL5123: Fourier Transform 22


Example of Rotation

Yao Wang, NYU-Poly EL5123: Fourier Transform 23


Fourier Transform For Discrete Time
Sequence (DTFT)
• One Dimensional DTFT
– f(n) is a 1D discrete time sequence
– Forward Transform
 F(u)) is
F( i periodic
i di iin u, with
ith period
i d
F (u )   f ( n )e
n  
 j 2un
of 1

– Inverse
I Transform
T f
1/ 2
f ( n)   F (u )e j 2un du
1 / 2

Yao Wang, NYU-Poly EL5123: Fourier Transform 24


Properties unique for DTFT
• Periodicity
– F(u) = F(u+1)
– The FT of a discrete time sequence is only
considered for u є (-½
( ½ , ½),
½) and u = +½ is the
highest discrete frequency
• Symmetry for real sequences
f ( n)  f * ( n)  F (u )  F * (u )
 F (u )  F (u )
 F (u ) is symmetric

Yao Wang, NYU-Poly EL5123: Fourier Transform 25


Example
1, n  0,1,..., N  1;
f ( n)  
 0, others
N 1
1  e  j 2uN sin 2u ( N / 2)
F (u )  e  j 2nu

1 e  j 2u
e  j ( N 1)u
sin 2u (1 / 2)
n 0

f(n)

0 N-1 n N=10

There are N/2 zeros in (0, ½], 1/N apart


Yao Wang, NYU-Poly EL5123: Fourier Transform 26
Two Dimensional DTFT
• Let f(m,n) represent a 2D sequence
• Forward Transform
 
F (u , v)  
m  
 n  

f (m, n)e  j 2 ( mu  nv )

• Inverse Transform
1/ 2 1/ 2
f (m, n)    F (u , v)e j 2 ( mu  nv ) dudv
1 / 2 1 / 2

• Properties
– Periodicity, Shifting and Modulation, Energy
Conservation

Yao Wang, NYU-Poly EL5123: Fourier Transform 27


Periodicity
 
F (u , v)  
m   n  
f (m, n)e  j 2 ( mu  nv )

• F(u,v) is periodic in u, v with period 1, i.e.,


for all integers k, l:
– F(u+k, v+l) = F(u, v)
• To see this consider
e  j 2 ( m ( u  k )  n ( v l ))
 e  j 2 ( mu  nv ) e  j 2 ( mk  nl )
 e  j 2 ( mu  nv )
In MATLAB, frequency scaling is such that 1 represents maximum freq u,v=1/2.
Yao Wang, NYU-Poly EL5123: Fourier Transform 28
Illustration of Periodicity
u

Low frequencies
1.0

High frequencies

0.5

-1.0 -0.5 0 0.5 1.0 v

Yao Wang, NYU-Poly EL5123: Fourier Transform 29


Fourier Transform Types

Low Pass High Pass Band Pass

u u u
0.5 0.5 0.5

v v v
-0.5 0.5 -0.5 0.5 -0.5 0.5

-0.5 -0.5 -0.5

Non-zero frequency components

Yao Wang, NYU-Poly EL5123: Fourier Transform 30


Shifting and Modulation
• Shifting
 
F{ f (m  m0 , n  n0 )}  
m   n  
f (m  m0 , n  n0 )e  j 2 ( mu  nv )
 
k  m  m0 , l  n n 0  
k   l  
f (k , l )e  j 2 (( k  m0 )u  (l  n0 ) v )
 
 e  j 2 ( m0u  n0v ) 
k   l  
f (k , l )e  j 2 ( ku lv )

f (m  m0 , n  n0 )  e  j 2 ( m0u  n0v ) F (u , v)

• Modulation
j 2 ( mu0  nv0 )
e f (m, n)  F (u  u0 , v  v0 )
Yao Wang, NYU-Poly EL5123: Fourier Transform 31
Energy Conservation
• Inner Product
 
 f , g  
m   n  
f (m, n) g * (m, n)
 

 f (m, n)   G * (u , v)e  j 2 ( mu  nv ) dudv 


 0.5 0.5

m   n  
 0.5 0.5 
   
      f (m, n)e  j 2 ( mu  nv ) G * (u , v)dudv
0.5 0.5

 0.5  0.5
 m   n   
0.5 0.5
  F (u , v)G * (u , v)dudv  F , G 
 0.5  0.5

• Energy Conservation
 


0 .5 0 .5
 
2 2
f (m, n)  F (u , v) dudv
 0 .5  0 .5
m   n  

Yao Wang, NYU-Poly EL5123: Fourier Transform 32


Delta Function
• Fourier transform of a delta function
 
F (u , v)  
m   n  
 (m, n)e  j 2 ( mu  nv )  1

 (m, n)  1

• Inverse Fourier transform of a delta


function
0.5 0.5
f (m, n)     (u, v)e j 2 ( mu  nv ) dudv  1
 0 .5  0 .5

1   (u, v)

Yao Wang, NYU-Poly EL5123: Fourier Transform 33


Example
m
1 2 1 f(m,n)

f (m, n)   0 0 0 
n
 1  2  1

F (u , v)  1e  j 2 ( 1*u 1*v )  2e  j 2 ( 1*u  0*v )  1e  j 2 ( 1*u 1*v )


 1e  j 2 (1*u 1*v )  2e  j 2 (1*u  0*v )  1e  j 2 (1*u 1*v )
 j 2 sin 2ue j 2v  j 4 sin 2u  j 2 sin 2ue  j 2v
 j 4 sin 2u (cos 2v  1)
Note: This signal
g is low p
pass in the horizontal direction,,
and band pass in the vertical direction.

Yao Wang, NYU-Poly EL5123: Fourier Transform 34


Graph of F(u,v)

du = [-0.5:0.01:0.5];
20 d = [[-0.5:0.01:0.5];
dv 0 5 0 01 0 5]
Fu = abs(sin(2 * pi * du));
40 Fv = cos(2 * pi * dv);
F = 4 * Fu' * (Fv + 1);
60 Using MATLAB freqz2:
mesh(du,
( , dv,, F);
);
80 colorbar; f=[1,2,1;0,0,0;-1,-2,-1];
Imagesc(F); freqz2(f)
00 colormap(gray); truesize;
20 40 60 80 100
Yao Wang, NYU-Poly EL5123: Fourier Transform 35
Linear Convolution
• Convolution of Continuous Signals
– 1D convolution
 
f ( x)  h( x)   f ( x   )h( )d   f ( )h( x  )d
 

• Equalities

f ( x)   ( x)  f ( x),
) f ( x)   ( x  x0 )  f ( x  x0 )
– 2D convolution
 
f ( x, y )  h ( x, y )    f ( x   , y   )h( ,  )dd
 
 
  f ( ,  )h(x   , y   )dd
 

Yao Wang, NYU-Poly EL5123: Fourier Transform 36


Examples of 1D Convolution
f(x) f(α)h(x-α)
h(x-α)
1

0 x x-1 x α 0 x α
1
h(x) (1) 0 ≤ x < 1

f(α)h(x-α)
1 h(x-α)

0 x α α
1 x-1
1 x 0 x-1 1
(2) 1 ≤ x < 2
h(-α)

1 f(x)*h(x)
1
0 1 α x
x
1 2

Yao Wang, NYU-Poly EL5123: Fourier Transform 37


Example of 2D Convolution
y
β h(x-α,y-β)β
f(x,y)
1 h(x-α,y-β) 1 y 1
y
y-1
1
1 x α α
1 x 1
x
y (1) 0<x≤1,
0<x≤1 0<y≤1 (2) 0<x≤1,
0<x≤1 1<y≤2
h(x,y) g(x,y)=x*y g(x,y)=x*(2-y)
1

1 x β β h(x-α,y-β)
h(x-α,y-β) y
1 1
y f(x,y)*h(x,y) y-1
y α α
2 1 1
x-1 x x-1 x

(3) 1<x≤2, 0<y≤1 (4) 1<x≤2, 1<y≤2


g(x,y)=(2-x)*y
( ) (2 )* g(x y)=(2 x)*(2
g(x,y)=(2-x) (2-y)
y)
2 x
Yao Wang, NYU-Poly EL5123: Fourier Transform 38
Convolution of 1D Discrete Signals
• Definition of convolution
 
f ( n) * h( n)  
m  
f ( n  m) h( m)   f ( m) h( n  m)
m  

• The convolution can be considered as the weighted


average:
– sample n-m is multiplied by h(m)
• Filtering: f(n) is the signal, and h(n) is the filter
– Finite impulse
p response
p ((FIR)) filter
– Infinite impulse response (IIR) filter
• If the range of f(n) is (n0, n1), and the range of h(n) is
(m0, m1),) then the range of f(n)*h(n)
f(n) h(n) is (n0+m0, n1+m1)

Yao Wang, NYU-Poly EL5123: Fourier Transform 39


Example of 1D Discrete Convolution

h(n-m)
f(n)
n-5 n 0 m
0 3 n
(a) n < 0, g(n) = 0

h(n)
h(n-m)
0 5 n
h(-m) n-5 0 n m
(b) 0 ≤ n ≤ 8, g(n) > 0
-5 0 m
h(n-m)

f(n)*h(n)
n n-5 0 m
(c) n > 8,
8 g(n) = 0
0 8 n

Yao Wang, NYU-Poly EL5123: Fourier Transform 40


Convolution of 2D Discrete Signals
• Definition of convolution
 
f (m, n) * h(m, n)    f ( m  k , n  l ) h( k , l )
k   l  
 
   f ( k , l ) h( m  k , n  l )
k   l  

• Weighted average:
– Pixel (m-k,n-l) is weighted by h(k,l)
• Range
– If the range of f(m
f(m, n) is m0 ≤ m ≤ m1, n0 ≤ n ≤ n1
– If the range of h(m, n) is k0 ≤ m ≤ k1, l0 ≤ n ≤ l1,
– Then the range of f(m, n)*h(m, n) is
m0 + k0 ≤ m ≤ m1 + k1, n0 + l0 ≤ n ≤ n1 + l1

Yao Wang, NYU-Poly EL5123: Fourier Transform 41


Example of 2D Discrete Convolution
m k
f(m,n) f(k,l)h(-1-k, -2-l)
1 1

n l

k
m f(k l)h(2 k 1 l)
f(k,l)h(2-k,1-l)
h(m,n) 1
1

l
n

f(m,n)*h(m,n) k
k
h(-k,-l) 1
1 2
3
l l

Yao Wang, NYU-Poly EL5123: Fourier Transform 42


• Go through an example in class for an
arbitrary 3x3 (non-symmetric) filter

Yao Wang, NYU-Poly EL5123: Fourier Transform 43


Separable Filtering
• A filter is separable if h(x, y)=hx(x)hy(y) or
h(m n)=hx(m)hy(n).
h(m, (n)
• Matrix representation
H  hx hyT

– Where hx and hy are column vectors


• Example
1 0  1  1 
H  2 0  2  21 0  1  hx hTy
1 0  1  1 

Yao Wang, NYU-Poly EL5123: Fourier Transform 44


DTFT of Separable Filter
• Previous example
1 0  1  1 
H  2 0  2  21 0  1  hx hTy
1 0  1  1 

• Recognizing that the filter is separable

1 0  1    Fy (v)  1e j 2v 0  (1)e  j 2v  2 j sin 2v


1 2 1    Fx(u )  1e j 2u 2 e  j 2u  2  2 cos 2u
F (u, v)  Fx(u ) Fy (v)  4 j (1  cos 2u ) sin 2v

Yao Wang, NYU-Poly EL5123: Fourier Transform 45


Separable Filtering
• If H(m,n) is separable, the 2D convolution can
be accomplished by first applying 1D filtering
along each row using hy(n), and then applying
1D filtering to the intermediate result along each
column using the filter hx(n)
• Proof f (m, n) * h(m, n)   f (m  k , n  l )hx (k )hy (l )
k l

 
    f (m  k , n  l )hy (l ) hx (k )
k  l 
  g y (m  k , n)hx (k )
k

 ( f (m, n) * hy (n)) * hx (m)

Yao Wang, NYU-Poly EL5123: Fourier Transform 46


• Go through an example of filtering using
separable filter in class

Yao Wang, NYU-Poly EL5123: Fourier Transform 47


Computation Cost
• Suppose
– The size of the image is M*N
M N.
– The size of the filter is K*L.
• Non-separable
N bl filt
filtering
i
– Each pixel: K*L mul; K*L – 1 add.
– Total: M*N*K*L mul; M*N*(K*L-1) add.
– When M=N, K=L
• M2K2 mul + M2(K2-1) add.

Yao Wang, NYU-Poly EL5123: Fourier Transform 48


Computation Cost
• Separable:
– Each pixel in a row: L mul; L-1
L 1 add.
– Each row: N*L mul; N*(L-1) add.
– M rows: M*N*L mul; M*N*(L-1) add.
– Each pixel in a column: K mul; K-1 add.
– Each column: M*K mul; M*(K-1) add.
– N columns: N*M*K mul; N*M*(K-1) add.
– Total: M*N*(K+L) mul; M*N*(K+L-2) add.
– When M M=N,
N K K=L:
L
• 2M2K mul; 2M2(K-1) add.
• Significant savings if K (and L) is large!

Yao Wang, NYU-Poly EL5123: Fourier Transform 49


Boundary Treatment
• When assuming the
image values outside a
given image are zero, the
filtered values in the
boundary are affected by
assumed zero values 200 200 205 203 203
adversely
200 200 205 203 203
• For
F better
b tt results,
lt use
symmetric extension 195 195 200 200 200
((mirror image)
g ) to fill the 200 200 205 195 195
outside image values
200 200 205 195 195

Actual image pixels Extended pixels

Yao Wang, NYU-Poly EL5123: Fourier Transform 50


Computer implementation of convolution
• Boundary treatment
– An image of size MxN convolving with a filter of size KxL will yield an
image of size (M+K-1,N+L-1)
– If the filter is symmetric, the convolved image should have extra
boundary of thickness K/2 on each side
– Filtered values in the outer boundary of K/2 pixels depend on the
extended pixel values
– For simplicity, we can ignore the boundary problem, and process only
the inner rows and columns of the image, leaving the outer K/2 rows
and L/2 columns unchanged (if filter is low-pass) or as zero (if filter is
high-pass)
• R
Renormalization:
li ti
– The filtered value may not be in the range of (0,255) and may not be
integers
– Use two-pass
p operation
p
• First pass: save directly filtered value in an intermediate floating-point array
• Second pass: find minimum and maximum values of the intermediate image,
renormalize to (0,255) and rounding to integers
– F= round((F1-fmin)*255/(fmax-fmin))
• To
T display
di l ththe unnormalized
li d iimage di
directly,
tl use “i
“imagesc(( )” function
f ti

Yao Wang, NYU-Poly EL5123: Fourier Transform 51


Sample Matlab Program
% readin bmp file % define 2D filter
x = imread('lena.bmp'); h = ones(5,5)/25;
[xh xw] = size(x); [hh hw] = size(h);
y = double(x); hhh = (hh - 1) / 2;
hhw = (hw- 1) / 2;
% linear convolution, assuming the filter is non-separable (although this example filter is separable)
z = y; %or z=zeros(xh
z=zeros(xh,xw)xw) if not low
low-pass
pass filter
for m = hhh + 1:xh - hhh,
%skip first and last hhh rows to avoid boundary problems
for n = hhw + 1:xw - hhw,
%skip first and last hhw columns to avoid boundary problems
tmpy = 0;
for k = -hhh:hhh,
for l = -hhw:hhw,
tmpv = tmpv + y(m - k,n – l)* h(k + hhh + 1, l + hhw + 1);
%h(0 0) is stored in h(hhh+1
%h(0,0) h(hhh+1,hhw+1)
hhw+1)
end
end
z(m, n) = tmpv;
%for more efficient matlab coding, you can replace the above loop with
z(m,n)=sum(sum(y(m-hhh:m+hhh,n-hhw:n+hhw).*h))
end
end
Yao Wang, NYU-Poly EL5123: Fourier Transform 52
Results

1 1 1 1 1
1 1 1 1 1
1 
h 1 1 1 1 1
25  
1 1 1 1 1
1 1 1 1 1

f(m,n) g(m,n)

Yao Wang, NYU-Poly EL5123: Fourier Transform 53


• What if the filter is symmetric? Can you
write a different MATLAB code that has
reduced computation time?
• MATLAB built
built-in
in functions
– 1D filtering: conv
– 2D filtering: conv2
– Computing DTFT: freqz, freqz2

Yao Wang, NYU-Poly EL5123: Fourier Transform 54


Convolution Theorem
• Convolution Theorem
f *h  F  H, f h  F *H
• Proof g (m, n)  f (m, n) * h(m, n)   f (m  k , n  l )h(k , l )
k l

FT on both sides
G (u , v)   f (m  k , n  l )h(k , l )e  j 2 ( mu  nv )
m , n k ,l

  f (m  k , n  l )e  j 2 (( m  k )u  ( n l ) v ) h(k , l )e  j 2 ( ku lv )
m , n k ,l

  f (m  k , n  l )e  j 2 (( m  k )u  ( n l ) v )  h(k , l )e  j 2 ( ku lv )
m,n k ,l

  f (m' , n' )e  j 2 ( m 'u  n 'v )  h(k , l )e  j 2 ( ku lv )


m ', n ' k ,l

 F (u , v)  H (u , v)

Yao Wang, NYU-Poly EL5123: Fourier Transform 55


Explanation of Convolution
in the Frequency Domain
h(x)
f(x) g(x)=f(x)*h(x)

-∆/2 ∆/2 x x
-∆/4
/ ∆/4 x

F(u)
( ) G(u)=F(u)H(u)
H(u)

u -2/∆ 2/∆ u u
-1/∆ 1/∆ -1/∆ 1/∆

Yao Wang, NYU-Poly EL5123: Fourier Transform 56


Example
• Given a 2D filter, show the frequency response. Apply to a given
image, show original image and filtered image in pixel and freq.
domain

1 1 1 1 1
1 1 1 1 1
1 
h 1 1 1 1 1
25  
1 1 1 1 1
1 1 1 1 1

Yao Wang, NYU-Poly EL5123: Fourier Transform 57


1 1 1 1 1
1 1 1 1 1
1 
h 1 1 1 1 1
25  
1 1 1 1 1
1 1 1 1 1

Yao Wang, NYU-Poly EL5123: Fourier Transform 58


Matlab Program Used

x = imread('lena256.bmp');
figure(1); imshow(x);
f = double(x);
ff=abs(fft2(f));
figure(2); imagesc(fftshift(log(ff+1))); colormap(gray);truesize;axis off;
h = ones(5,5)/9;
hf=abs(freqz2(h));
figure(3);imagesc((log(hf+1)));colormap(gray);truesize;axis off;
y = conv2(f, h);
figure(4);imagesc(y);colormap(gray);truesize;axis off;
yf=abs(fft2(y));
figure(5);imagesc(fftshift(log(yf+1)));colormap(gray);truesize;axis off;

Yao Wang, NYU-Poly EL5123: Fourier Transform 59


1 1 1
H1   0 0 0 
1
3
 1  1  1

Yao Wang, NYU-Poly EL5123: Fourier Transform 60


Filter Design
• Given desired frequency response of the filter
Hd(u,v)
• Perform an inverse transform to obtain the
desired impulse response hd(m,n).
• When Hd(u,v) is band limited, hd(m,n) is infinitely
long
• Truncate hd(m,n)
(m n) to yield a realizable h(m
h(m,n)
n)
• Will distort the original frequency response
• Better approach is to apply a well designed
window function over the specified frequency
response.

Yao Wang, NYU-Poly EL5123: Fourier Transform 61


Use of Window Function in Filter Design

Hd(u) H (u) Hw(u)

u u u

hd(x) window h (x) hw (x)


function
w(x)

… …

x x x
x
Hamming window w( x)  0.54  0.46 cos(2 ), 0  x  X
X

Yao Wang, NYU-Poly EL5123: Fourier Transform 62


Homework
1. Let sin(2f c x) sin(2f c y )
f ( x, y )  sin 2f 0 ( x  y ), h( x, y ) 
 2 xy

Find
Fi d the
h convolved
l d signal
i l g(x,
( y)) = f(x,
f( y)) * h(x,
h( y)) ffor the
h ffollowing
ll i
two cases:
a) f0/2 < fc < f0; and b) f0 < fc < 2f0.
Hint: do the filtering in the frequency domain. Explain what
happened by sketching the original signal, the filter, the convolution
process and the convolved signal in the frequency domain.
2. Repeat the previous problem for

 2 1 1
 fc ,   {x, y} 
h ( x, y )   2 fc 2 fc
 0, otherwise

Yao Wang, NYU-Poly EL5123: Fourier Transform 63


Homework (cntd)
3. For the three filters given below (assuming the origin is at the center):
a) find their Fourier transforms (2D DTFT);
b) sketch the magnitudes of the Fourier transforms
transforms. You should sketch by
hand the DTFT as a function of u, when v=0 and when v=1/2; also as a
function of v, when u=0 or ½. Also please plot the DTFT as a function of
both u and v, using g Matlab pplotting
g function.
c) Compare the functions of the three filters.
In your calculation, you should make use of the separable property of the filter
whenever appropriate. If necessary, split the filter into several additive
terms such that each term can be calculated more efficiently.

1 1 1  1 2 1    1  2  1
H1  1 1 1
1 1   1  
H2   2 12  2 H3   2 12  2
9 24   24  
1 1 1  1  2 1    1  2  1 

Yao Wang, NYU-Poly EL5123: Fourier Transform 64


Programming Assignment
1. Write a Matlab or C-program for implementing filtering of a gray scale
image. Your program should allow you to specify the filter with an
arbitrary size (in the range of k0->k1, l0->l1), read in a gray scale image,
and save the filtered image into another file
file. For simplicity
simplicity, ignore the
boundary effect. That is, do not compute the filtered values for pixels
residing on the image boundary. If the filter has a size of M x N, the
boundary pixels are those in the top and bottom M/2 rows, and left and
right
g most N/2 columns. You should p properly
p y normalize the filtered image g
so that the resulting image values can be saved as 8-bit unsigned
characters. Apply the filters given in the previous problem to a test image.
Observe on the effect of these filters on your image. Note: you cannot
use the MATLAB conv2( ) function. In your report, include your MATLAB
code the original test image and the images obtained with the three
code,
filters. Write down your observation of the effect of the filters.
2. Optional: Now write a convolution program assuming the filter is
separable. Your program should allow you to specify the horizontal and
vertical filters,
filters and call a 1D convolution sub-program
sub program to accomplish the
2D convolution. Note: you cannot use the MATLAB conv( ) function.
3. Learn the various options of conv2( ) function in MATLAB using online
help manual.

Yao Wang, NYU-Poly EL5123: Fourier Transform 65


Reading
• Gonzalez and Woods, Sec. 4.1 and 4.2,
4 3
4., 3.4,
4 33.5,
5 33.5
5

Yao Wang, NYU-Poly EL5123: Fourier Transform 66

También podría gustarte