dft domain image filteringyao/el5123/lecture6_2... · 2011. 10. 18. · dft domain image filtering...

60
DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from Zhu Liu, Onur Guleryuz, and Gonzalez/Woods, Digital Image Processing, 2ed

Upload: others

Post on 24-Jan-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

DFT Domain Image Filtering

Yao WangPolytechnic Institute of NYU, Brooklyn, NY 11201

With contribution from Zhu Liu, Onur Guleryuz, andGonzalez/Woods, Digital Image Processing, 2ed

Page 2: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Lecture Outline

• 1D discrete Fourier transform (DFT)2D discrete Fo rier transform (DFT)• 2D discrete Fourier transform (DFT)

• Fast Fourier transform (FFT)• DFT domain filtering• 1D unitary transform1D unitary transform• 2D unitary transform

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 2

Page 3: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Discrete Fourier Transform (DFT): DTFT for Finite Duration SignalsDTFT for Finite Duration Signals

:becomesansformFourier tr:1,...,1,0for definedonly is signal theIf Nn

)1,0(,)2exp()('1

0

ffnjnf(f)FN

n

:(DFT) transformForward :yields rescaling and,110at ' Sampling ,...,N-,k/N, kf(f)F

( )f

1,...,1,0,)2exp()(1')(1

0

NknNkjnf

N)

Nk(FkF

N

n

1,...,1,0,)2exp()(1:(IDFT) transformInverse

1

NnnNkjkF

Nf(n)

N

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 3

0 NN k

Page 4: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of DFT (1)• Periodicity

.modulorepresents))((where

.0),))((()(NkNkorkkFkF

N

N p))(( N

))(2exp()(1)(

1

1

0n

NmNkjnf

NmNkF

N

N

n

Proof

AG

AG

AG

F(k)

)()22exp()(1 1

0

kFmnjnNkjnf

N

N

n

BC

DE

FG B

CD

E

FGB

CD

E

FG

0 N-1N

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 4

low lowhighNote: Highest frequency is at k=[N/2]. k=0,1, N-1 represent low frequency.

Page 5: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of DFT (2)

• Translation)}/(2{)()))((( NkjkFf

).))(((}/2exp{)()}/(2exp{)()))(((

00 N

oNo

kkFNnkjnfNknjkFnnf

– Special casep• N is even, k0 = N/2.

)))((()1)((}exp{)( n NkFnfnjnf ).))2

((()1)((}exp{)( NkFnfnjnf

Shifting the frequency up by N/2

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 5

g q y p y

Page 6: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of DFT (3)

• Conjugate symmetry for real sequences**

|)(||)(|)()()( **

orkNFkFkNFkFkF

.|,)2

(||)2

(| evenisNwhenkNFkNF

0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 6

N=8 N=9

Page 7: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

2D Discrete Fourier Transform

• DefinitionAssuming f(m n) m = 0 1 M 1 n = 0 1– Assuming f(m, n), m = 0, 1, …, M-1, n = 0, 1, …, N-1, is a finite length 2D sequence

;110110)(1)(1 1 )(2

NlMkflkFM N

Nln

Mkmj

.1,...,1,0,1,...,1,0,),(1),(

;1,...,1,0,1,...,1,0,),(),(

1 1 )(2

0 0

NnMmelkFMN

nmf

NlMkenmfMN

lkF

M NNln

Mkmj

m n

NM

– Comparing to DTFT0 0 MN k l

lvkuenmfvuF nvmuj

)()( )(2

Nv

MuenmfvuF

m n

j

,,),(),( )(

2/1 2/1 )(2),(),( dudvevuFnmf nvmuj

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 7

2/1 2/1),(),( dudvevuFnmf

Page 8: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of 2D DFT (1)

• Periodicity00)))(())((()( NlorlMkorklkFlkF .0,0),))((,))(((),( NlorlMkorklkFlkF NM

A B A B-M

LowFrequency

A

C

B

D

A

C

B

D-M/2

HighFrequency

A B A B0

(0,0) (0,N-1)

Frequency

C D C DM

M/2

(M-1,0) (M-1,N-1) Displayed area using

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 8

0 N-N -N/2 N/2kl

area using fftshift

Page 9: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of 2D DFT (2)• Translation

)},//(2exp{),()))((,))((( 0000 NM NlnMkmjlkFnnmmf

– Special Case: M,N=even, k0=M/2, l0=N/2).))((,))((()}//(2exp{),( 0000 NM llkkFNnlMmkjnmf

NM ).))2

((,))2

((()1)(,()}(exp{),( )(NM

nm NlMkFnmfnmjnmf

A B A B-M

D C D C-M

A

C

B

D

A

C

B

D0

-M/2D

B

C

A

D

B

C

A0

-M/2f(m,n)

f(m,n)(-1)(m+n)

A

C

B

D

A

C

B

DM/2

D

B

C

A

D

B

C

AM/2F(k,l)

F(k-M/2,l-N/2)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 9

0 N-NM

-N/2 N/2 0 N-NM

-N/2 N/2

Page 10: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of 2D DFT (3)

• Conjugate symmetry for real sequenceslNkMFlkFlkF )()()( **

evenNMforlNkMFlNkMF

lNkMFlkFlNkMFlkFlkF

,|,)2

,2

(||)2

,2

(|

|,),(|),(|),,(),(),(

22220

5

10

1

6

11

2

7

12

3

8

13

4

9

14

3

39

35

2

38

34

1

37

33

4

40

36

0

5

10

1

6

11

2

7

12

3

8

13

4

9

14

3

33

30

2

32

29

1

31

2815

20

20

16

21

25

17

22

26

18

23

27

19

24

28

31

27

23

30

26

22

29

25

21

32

28

24

10

15

20

15

11

16

21

25

12

17

22

26

13

18

23

27

14

19

24

19

30

27

23

18

29

26

22

17

28

25

21

16

M N 8 real C j t i

15

10

29

33

30

34

31

35

32

36

18

13

17

12

16

11

5 37 38 39 40 8 7 6

19

14

9

10

5

28

31

29

32

30

33

14

9

13

8

12

7

11

6

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 10

M=N=8=even, x xreal Conjugate pairs M=N=9=odd

Page 11: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of 2D DFT (5)• Separabability

– 2D DFT can be accomplished by N-point 1D DFT of2D DFT can be accomplished by N point 1D DFT of each row, followed by M-point 1D DFT of each column

• How many 1D DFT’s?– M rows: M N-pt DFT’s

N l N M t DFT’– N columns: N M-pt DFT’s– M=N: 2N N-pt DFT’s– Each N-pt DFT requires N2 operations total requiring– Each N-pt DFT requires N operations, total requiring

2N3 operations• Direct calculation

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 11

– Requiring M2N2 (N4 if M=N) operations

Page 12: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of 2D DFT (6)

• 2D DFT of Separable Images

TT

:formmatrix In)()(),()()(),(

FFFfff

yxyx lFkFlkFnfmfnmf

Tyx

Tyx

)11()11()01()1,0()1,0()0,0(

FFFfff

NfffNfff

)1,1()1,1()0,1(

)1,1()1,1()0,1(f

NMfMfMf

Nfff

y

DFT1Dpt Ny

x DFT1Dpt M

x

)1()1()0(

)1()1()0(

Ff

Ff

Tyyy

Txxx

Nfff

Mfff

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 12

yy yyy

Real images are seldom separable. But many 2D filters are separable.

Page 13: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Computer Implementation of 2D DFT

• Complex array structureImplement comple m ltiplication and• Implement complex multiplication and addition

j2 k /M• Pre-compute the constants: e-j2πkm/M for all k,m=0, 1, …, M-1, and e-j2πln/N for all l,n=0, 1, …, N-1.

• Use separable processing to speed up• For real square image, only need to

calculate half of the points

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 13

calculate half of the points

Page 14: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Display of the Magnitude of 2D DFT

• Shifting the low frequency components into the centerinto the center

).))2

((,))2

(((),()1)(,(),( )(NM

nm NlMkFlkGnmfnmg

A B D CA B

C D B ACD

F(k,l) G(k,l)

LowFrequency

HighFrequency

D

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 14

Frequency Frequency

Matlab command: fftshift

Page 15: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Display of the Magnitude of 2D DFT

• Amplitude rescaling )),(1log(),( lkFlkG

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 15

Page 16: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Sample Fourier pairsWhich Fourier transform in the second row is for each image in the first row?

(a) (b) (c) (d)

(e) (f) (g) (h)(e) (f) (g) (h)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 16

Page 17: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Fast Fourier Transform (FFT)• Direct computation of N-point DFT takes N2

operationsoperations• FFT is a fast algorithm for computing DFT,

reducing the computation from N2 to N log2(N)g p g2( )– Complex conjugate symmetry of Nknje /2

*/2/)(2/2/)(2 NknjNnkjNknjNnNkj eeee

– Periodicity in n and k of Nknje /2

eeee

NNkNNkNk /)(2/)(2/2

• Matlab function for N-point DFT: fft(A,N), fft2(A M N)

NnNkjNNnkjNknj eee /)(2/)(2/2

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 17

fft2(A,M,N)

Page 18: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Computation of 2D DFT

• 2D (MxN) point DFT can be computed in a separable manner:separable manner: – First compute N-point FFT for each row (M N

log (N) )log2 (N) )– Then compute M-point FFT for each column (N M

log2 (M) )log2 (M) )– Total computation if M=N: 2N2log2 (N)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 18

Page 19: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Convolution Theorem (1D)

• Circular convolution

1N

• Circular convolution• Circular convolution (N-pt)

0

)()))((()()(k

N khknfnhnff(((n-k))N)

f(n) h(n)

Convolution theorem0 1 2 3 4 n

Convolution theoremConvolution theorem0 1 2 3 4 n n

n+1n+2

n+3n+4 k

n-10

• Convolution theorem)()()()( kHkFnhnf

• Convolution theorem• Convolution theorem

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 19

Page 20: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Linear Convolution vs. Circular ConvolutionConvolution

• f(n): N1 pt, h(n): N2 pt, assume N1>=N2• f(n)*h(n): N=N1+N2-1 pt• Equivalent to circular convolution of M-pt, if

M>=N

• If we do N1 pt circular convolution, which parts of the resulting output is equal to that of linear convolution (assume N2 is much smaller than N1)?– Illustrate on boardIllustrate on board– If f(n) is from 0 to N1-1, h(n) is from 0 to N2-1– Circular conv. = linear conv. Over N2-1 to N1-1

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 20

Page 21: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Calculate Linear Convolution Using DFT

• 1D casef(n) is length N h(n) is length N– f(n) is length N1, h(n) is length N2

– g(n) = f(n)*h(h) is length N = N1+N2-1.T DFT d t t d f( ) d h( ) t– To use DFT, need to extend f(n) and h(n) to length N by zero padding.

f(n) h(n) g(n)*Convolution

F(k) H(k) G(k)x

N-point DFT DFT DFT

Multiplication

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 21

Page 22: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Calculate Linear Convolution Using DFT

f(n), N1=5 fe(n), N=8 Fe(k)

n n k

h(n), N2=4 he(n), N=8 He(k)

n n k

F (k) H (k))()()(*)( nhnfnhnf ee Fe(k) He(k))()()()( nhnfnhnf ee

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 22

kn

Page 23: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Comparison of Complexity

• Direct calculationEach point N multiplications– Each point, N2 multiplications

– Overall, N2 * N ≈ O(N2)Vi FFT• Via FFT– N-point FFT for f(n) and h(n)– N multiplications in the DFT domain– N-point inverse FFT for F(k)*H(k)– Overall, 3NlogN + N ≈ O(NlogN)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 23

Page 24: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Circular Convolution (2D)

• Circular Convolution

1 1M N

0 0

),()))((,))(((),(),(k l

NM lkhlnkmfnmhnmf

• Convolution Theorem),(),(),(),( lkHlkFnmhnmf ),(),(),(),(f

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 24

Page 25: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Circular vs. Linear Convolution– If f(m,n) is N1xM1 pt, h(m,n) is N2xm2 pt– f(m n)*h(m n) is N’xM’ pt N’=N1+N2-– f(m,n) h(m,n) is N xM pt, N =N1+N2-

1,M’=M1+M2-1– Equivalent to NxM pt circular convolution ifEquivalent to NxM pt circular convolution if

N>=N’,M>-M‘– Can be implemented by using NxM pt 2DCan be implemented by using NxM pt 2D

DFT!

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 25

Page 26: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Separable filter

• If the filter is separable, h(m n)=hx(m) hy(n)– h(m,n)=hx(m) hy(n)

– Row-by-row circular convolution with hy(n), then column by column circulua convolutionthen column-by-column circulua convolution with hx(m)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 26

Page 27: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Filtering in DFT Domain

Relation between spatial and frequency domain operation:

))(()())(()(),(),(),(),(),(),(

yxhDFTvuHvuHIDFTyxhvuFvuHvuGyxfyxhyxg

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 27

)).,((),()),,((),( yxhDFTvuHvuHIDFTyxh Typically DFT size=image size. This corresponds to circular convolution.

Page 28: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Low-Pass Filter Using DFT Window• Filtering in DFT domain:

– G(k,l)=H(k,l) F(k,l)• Ideal Low-Pass Filter

– H(k,l)= 1 in low frequency range (the four corners!) = 0 in high frequency range

– Can think of H(k,l) as a mask in the DFT domain• The (k,l)th coefficient is unchanged if H(k,l)=1, and is set to zero if

H(k,l)=0.

• More generally, H(k,l) can take on higher values in low-frequency range, and lower values in high frequency range

• Note that equivalent spatial filter is truncated sinc. With Circular convolution, it leads to ringing artifacts.

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 28

Page 29: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Low-Pass Filter in DFT Domain

The window size where H(k l)=1 is

• The window can be implemented in matlab via

H(k,l)=1 is (2 W1+1)x(2 W2+1)

Before The window can be implemented in matlab via» R1=zeros(M,1);» R1(1:W1+1,M-W1+1:M)=1;» R2=zeros(N,1);

shifting: note low frequency are at four corners!( , );

» R2(1:W2+1,N-W2+1:N)=1;» H=R1*R2’;

• Given G(k,l)=F(k,l)H(k,l), matlab may make small numerical errors in

corners!

the inverse transform leading to complex valued images. g(m,n) is best reconstructed via:

» g=real(ifft2(F.*H));

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 29

Page 30: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 30

Courtesy of Onur Guleyuz

Page 31: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Spatial Filter Corresponding to Ideal LPFto Ideal LPF

10

2020

30

40

50

10

20

10 20 30 40 50 60

60

20

30

40

50IDFT

10 20 30 40 50 60

60

Ideal LPF withIdeal LPF with W1=W2=10

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 31

To suppress the ringing artifacts, the window function should be smoothed by an appropriately designed smoothing function.

Page 32: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

High Pass Filtering

• The high-pass filtered image can be thought of as the original image minus thethought of as the original image minus the low pass filtered image.

High pass filtering by DFT windows:– High-pass filtering by DFT windows:• If w(k,l) (W1 W2) is a low-pass DFT window,

simply define a high-pass window h(k, l) by h(k, l) p y g p ( , ) y ( , )= 1 – w(k, l).

– High-pass filtering in spatial domain:• If L is a low-pass filter of size W, simply define a

high pass filter H via H(m,n) = δ(m,n) - L(m,n).

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 32

Courtesy of Onur Guleyuz

Page 33: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

High Pass Filtering in DFT Domain

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 33

Page 34: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

DFT as Unitary Transform• We have so far explained DFT as sampled

version of DTFT for finite duration discrete time signals

• Finite duration discrete time signal = vector (1D) or image (2D)or image (2D)

• Transform = decomposition of vector or image into a weighted sum of some basis vectors orinto a weighted sum of some basis vectors or images.

• Unitary transform : basis functions are orthonormal to each other

• DFT uses a special type of basis functions

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 34

Page 35: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

One Dimensional Linear Transform

• Let CN represent the N dimensional complex spacecomplex space.

• Let h0, h1, …, hN-1 represent N linearly independent vectors in CNindependent vectors in CN.

• For any vector f є CN, 1N

)0(

,)(1

0

t

ktN

kk Bthf

AffBt 1

.)1()(

],,...,,[ 110

twhere N

thhhB

AffBt

f and t form a transform pair

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 35

)1(

Nt

Page 36: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Inner Product

• Definition of inner product1

* )()(N

H ffffff

• Orthogonal

0

212121 )()(,n

H nfnfffff

• Norm of a vector0, 21 ff

Norm of a vector

Normalized: unit norm

1

0

22 |)(|,N

n

H nffffff

12f• Normalized: unit norm• Orthonomal = orthogonal + normalized

1f

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 36

Page 37: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Orthonormal Basis Vectors (OBV)

• {hk, k=0,…N-1} are OBV if lk1

lklk

lklk 01

, ,hh

fhhhhhfh HNN

lkk

)()()(11

fhhhhhfh Hlkl

kk

kll ltktkt

)(,)()(,,00

h

H

0

AffBfhh

t

HH

1

0

h

HN 1

.,1 IBBBBBB HHH or B is unitary

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 37

., IBBBBBB or y

Page 38: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Definition of Unitary Transform

• Basis vectors are orthonormalFor ard transformfh

N

fhk1

* )()()(• Forward transformh

fh

H

nkk nfnhkt

0

0

* ,)()(,)(

AffBf

h

ht

H

H

H1

Inverse transform

h N 1

N

hkf

1

)()()(• Inverse transform

tABtthhhhf HN

N

k

kk

kt

nhktnf

110

10

)(

,)()()(

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 38

tABtthhhhf Nk

kkt 110

0)(

Page 39: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example of 1D Unitary Transform

2/12/1

2/12/1

2/12/1

2/12/1

,2/12/1

,2/12/1

,2/1

2/1,

2/12/1

3210 hhhh

51

2/12/12/12/1

0t

02

32 1

0

tt

f

10

43

3

2

tt

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 39

Page 40: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of Unitary Transform

• Energy preservation: ||f||=||t||.Proof: ttttAAtfff HHHHProof:

• Mean vector relation:ttttAAtfff

AA hH

}{},{

,,

tμfμ

μAμAμμ

EandE

where

tf

tH

fft

• Covariance relation:,,

HH

tH

fH

ft whereACACAACC

Proof:}))({(},))({( H

tttH

fff EE μtμtCμfμfC

HHH

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 40

.}))(({)( Hf

HHfftft E AACAμfμfACμfAμt

Page 41: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

1D DFT as a Unitary Transform

;1...,,1,0,)(1)(1

0

2

Nkenf

NkF

N

n

Nknj

.1...,,1,0,)(1)(1 2

0

NnekFN

nf

NN

Nknj

n

,1)(2

oreN

nh Nknj

k

0N k

1

12

N

Nkj

.1,...,1,0,1

)1(2

Nke

NN

kNj

N

k

h

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 41

e N

Page 42: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example: 1D DFT

)02exp(

:vectorsbasisonly two are there:case 2kj

N

hhh

1

12

1,11

21:

)12

2exp(

)02

2exp(

21

10kj

jk

fh

f

obtain we,, Using

, determine ,21

if 10

t

tt

kk

111

212*11*1

21

21

,1

12

1,2

32*11*12

121

,11

21

10 tt

kk

fhh

21

11

21

11

23 :Verify 1100 tt

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 42

Page 43: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Another Example: 1D DFT

111

11

111

11)1

42exp(

)04

2exp(

1 jjkj

kj

hhhhh

12

1;

111

21;

121;

111

21:yields

)34

2exp(

)24

2exp(4

21 using :case 4 3210

j

j

j

j

kj

kjN k

f

);3(213542

21;73542

21

42

4

10 jjjtt

f

8)3()3(14

.3213542

21;03542

21

222

35

32

jj

jjjtt

fhhhh

122016

41

)3()3(14)3()3(14)3()3(14

41 :Verify 33221100

jjjjjj

jjjjtttt

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 43

Page 44: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Two Dimensional Decomposition

• Decompose an MxN 2D matrix F=[F(m,n)] into a linear combination of some basicinto a linear combination of some basic images, Hk,l=[Hk,l(m,n)], so that:

1

0

1

0, ,),(

M

k

N

llklkT HF

1 1

,

0 0

),(),(),(M N

lk

k l

nmHlkTnmF 0 0k l

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 44

Page 45: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Graphical Interpretation

+t1 t2 t3 t4t1 t2 t3 t4

Inverse transform: Represent a vector (e.g. a block of image samples) as the superposition of some basis vectors (block patterns)Forward transform: Determine the coefficients associated with each basis vector

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 45

Page 46: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Two Dimensional Inner Product

• Inner Product

1 1

*M N

• Norm of a Matrix

0 0

2*

121 ),(),(,m n

nmFnmFFF

• Norm of a Matrix

1

0

1

0

2),(,M N

nmFFFF

• A set of basis images {Hk,l, k=0,1,…,M-1, l=0 1 N-1} is orthonormal if

0 0m n

l=0,1,…,N-1} is orthonormal if

otherwise

jlikifjlikjilk 0

,,1, ,,,, HH

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 46

otherwise,0

Page 47: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Two Dimensional Unitary Transform

• {Hk,l} is an orthonormal set of basis imagesFor ard transform• Forward transform

1 1

* )()()(M N

nmFnmHlkT FH

• Inverse transform

0 0

,, ),(),(,),(m n

lklk nmFnmHlkT FH

Inverse transform

1

0

1

0, ,),(),(),(

M

k

N

llk ornmHlkTnmF

1

0

1

0,),(

M

k

N

llklkT HF

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 47

Page 48: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example of 2D Unitary Transform

5)0,0(

,2/12/12/12/1

,2/12/12/12/1

,2/12/1

2/12/1,

2/12/12/12/1

11100100

T

HHHH

0)1,1(1)0,1(2)1,0(

),(

4321

TTT

F

0)1,1(T

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 48

Page 49: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

2D DFT as a Unitary Transform.1,...,1,0,1,...,1,0,),(1),(

1

0

1

0

)(2

NnMmelkF

MNnmf

M

k

N

l

Nln

Mkmj

Nln

Mkmj 21

NM

j

lk eMN

nmH ,1),(

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 49

Basis Images for 8x8 DFT (Real Part) Basis Images for 8x8 DFT (Imaginary Part)Notice how the frequency content of the basis images change !

Page 50: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Separable Unitary Transform

• Let hk, k=0, 1, …, M-1 represent orthonormal basis vectors in CMorthonormal basis vectors in C ,

• Let gl, l=0, 1, …, N-1 represent orthonormal basis vectors in CNorthonormal basis vectors in CN,

• Hk,l=hkglT, or Hk,l(m,n)=hk(m)gl(n).

• Then Hk,l will form an orthonormal basis set in CMxN.

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 50

Page 51: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example of Separable Unitary Transform

• Example 1 2/12/1

2/12/12/12/1

.2/1

2/1,2/12/1

10

TT hhHhhH

hh

2/12/12/12/1

2/12/12/12/1

2/12/12/12/1

11110110

10010000

TT hhHhhH

hhHhhH

• 2D DFT 2/12/12/12/1

Nln

Mkmj

enmH21)(

Nlnj

lMkmj

k

lk

eN

ngeM

mh

eMN

nmH

22

,

1)(,1)(

,),(

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 51

NM

Page 52: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example: 4x4 DFT

1

1

21;

11

1

21;

1

1

21;

111

21: are basis DFT 1D theRecall 3210

jjhhhh

1111

111111111

11111

111111111

1

:yields using

11

jjjj

jjjj

jjT

lkk,l hhH

1111

11111

11111

11111

1

11114

1,

111111114

1,

11114

1,

111111114

13,02,01,00,0

jjjj

jjjjjjjj

jjjj

jjjjjj

jjjjjj

HHHH

HHHH

1111

111111111

11111

111111111

1

11114

,11114

,

11114

,11114 3,12,11,10,1

jjjj

jjjj

jjjj

jjjjjjjj

jjjj

HHH

HHHH

1111

1,

11111,

1111

1,

11111

11114

,

111111114

,

11114

,

111111114

33231303

3,22,21,20,2

jjjj

jjjjjjjj

jjjj

jjjj

jjjj

HHHH

HHH

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 52

11

114,

11114,

11114

,11114 3,32,31,30,3

jjjj

jjjjjjjj

jjjj

Page 53: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Example: 4x4 DFT

comp te13100221

For T

F

02211111

e.g, yields, , Using

compute ,

12211210

For

,,

,

T

T

lklk

lk

FH

F

022111

4181221121013100221

41

122112101310

,

111111111111

41,

*

0,00,0

jj

T

FH

)1(4122123221

41

122112101310

,

1111

1141,3,23,2 jjjjjjjj

jjjjjj

T

FH

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 53

Page 54: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Property of Separable Transform

• When the transform is separable, we can perform the 2D transform separatelyperform the 2D transform separately.– First, do 1D transform for each row using

basis vectors gbasis vectors gl,– Second, do 1D transform for each column of

the intermediate image using basis vectors hkthe intermediate image using basis vectors hk.– Proof:

11 11 1 MM NM N

0

*

0 0

**

0 0

*, ),()(),()()(),(),(),(

mm nl

m nlk lmUmhnmFngmhnmFnmHlkT

kk

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 54

Page 55: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Computational Savings of Separable TransformSeparable Transform

• A MxN transform takes about MxN calculations for each transform value,calculations for each transform value, and the total number of calculation is M2N2 (N4 if M=N)( )

• If the transform is separable1. Calculate M N-point 1D transform, each p ,

requiring N2 calculations2. Calculate N M-point 1D transform, each

i i M2 l l tirequiring M2 calculations3. Overall: MN2+NM2 (2N3, if M=N)

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 55

Page 56: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Other Transforms• Discrete cosine transform (DCT)

– Basis functions are real– Can be thought of as “real version” of DFT– Can be thought of as real version of DFT– We will discuss DCT and its application in image compression

(JPEG) later• Discrete sine transform (DST)sc ete s e t a s o ( S )• Hadamard transform• Walsh transform• Haar transform• Haar transform• Slant transform• Wavelet transform

– Multi-resolution transform– We will discuss wavelet transform and its application in image

compression (JPEG2000) later

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 56

Page 57: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Homework1. Find the NxN point DFT of the following 2D image f(m, n), 0 ≤ m, n ≤ N:

a) f(m, n) = 1, n = 0; f(m, n) = 0; n ≠ 0.

b) f( ) 1 0 f( ) 0 ≠ 0b) f(m, n) = 1, m = 0; f(m, n) = 0, m ≠ 0.

c) f(m, n) = 1, m = n; f(m, n) = 0, otherwise.

d) f(m, n) = 1, m = N – 1- n; f(m, n) = 0, otherwise.

From the result, what can you say about the relation between the directionality of an image with that of its DFT?

2. One can use the DFT algorithms to compute the linear convolution of an image F(m, n) with a filter H(m n) Let the convolved image be denoted by Y(m n)n) with a filter H(m, n). Let the convolved image be denoted by Y(m, n).

i) Suppose the image size is 256x256 and the filter size is 11x11: What is the required size of the DFT to obtain the convolution of these two? Explain the exact steps to obtain the convolution result.

ii) Suppose we use a 256x256 point DFT algorithm for F(m, n) and H(m, n), and obtain Z(m, n) as follows: Z = IDFT (DFT(X).*DFT(H)). The DFT and IDFT in the above equation are both 256x256 points. For what values of (m, n) does Z(m, n) equal Y(m, n) ?

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 57

Page 58: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Homework (cnt’d)3. Show that the following vectors form an orthonormal basis set for any value of

cossin

,sincos

10 hh

Determine the corresponding matrix A and B for the forward and inverse transform described by

4. Consider a zero mean random vector u with covariance matrix 1

. : transformInverse ; : transformForward BtfAft

From the class of unitary transforms given in Prob. 3, determine the value of θ for which the components of transformed vector v will be uncorrelated (i.e., the covariance matrix of the transformed vector is a diagonal matrix).

11

uC

5. The basis images of the 2D-DCT are:

;1111

21;

1111

21;

1111

21;

1111

21

11100100

HHHH

Calculate the transform of the image:

Find the reconstructed image obtained with the 2 largest coefficients (in magnitude). Calculate the error between the original and reconstructed images defined by You

F̂ nmFnmFE 2)],(ˆ),([

3221

F

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 58

the error between the original and reconstructed images defined by You should see that where t(k, l) represents the transform coefficient associated with the basis image Hkl.

nm,)],(),([

omittedarelktlklktE

),(:,2|),(|

Page 59: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Computer Assignment1. Perform 2D DFT (using the fft2 function in Matlab) on several test images of your choice

(sample images are available at the Matlab image toolbox directory). Display the magnitude of the DFT image with and without shifting (using fftshift) and with and without logarithmic mapping, to see the effect of shifting and logarithmic mapping. Include the printouts in your submission Also examine in which frequency range the DFT coefficients have largesubmission. Also, examine in which frequency range the DFT coefficients have large magnitudes and explain why. Note that you may want to work on a small portion of your image (say 256x256 or less), to save computation time. Note that if your image is an RGB image, you should convert it to a grayscale image using "rgb2gray" function and apply the above operations on the grayscale image only. If the original image is an index image, you should not apply DFT to the index image directly, rather you should derive the grayscale image from it, using the "ind2gray" function.

2. Write a program (in Matlab or C) which filters an image by zeroing out certain DFT coefficients. The program consists of 3 steps: 1) performing 2D DFT (you can use the “fft2" function in Matlab); 2) zeroing out the coefficients at certain frequencies (see below); ) g ( )3) performing inverse DFT to get back a filtered image. Truncate or scale the image properly such that its range is between 0 and 255. For part 2, try the following two types of filters:

(a) let F(k, l) = 0 for TN < {k, l} < (1 - T )N, T = 1/4, 1/8 (i.e., low-pass filtering); (b) let F(k, l) = 0 for the following regions i) 0 ≤ {k and l}≤ TN;ii) 0<= k <=TN, and (1 - T)N ≤l(b) let F(k, l) 0 for the following regions i) 0 {k and l} TN;ii) 0 k TN, and (1 T)N l

≤ N -1;iii) (1 - T)N ≤k≤ N -1 and 0 ≤ {l}≤ TN; iv)(1 - T)N ≤k and l ≤ N -1;T=1/4, 1/8Compare the original and processed images. Comment on the function of the two types of filters.

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 59

Page 60: DFT Domain Image Filteringyao/EL5123/lecture6_2... · 2011. 10. 18. · DFT Domain Image Filtering Yao Wang Polytechnic Institute of NYU, Brooklyn, NY 11201 With contribution from

Reading

• R. Gonzalez, “Digital Image Processing,” Chap 4 except Sec 4 5Chap.4, except Sec. 4.5.

• A. Jain, “Fundamentals of Digital Image Processing ” Chapter 5Processing,” Chapter 5.

Yao Wang, NYU-Poly EL5123: DFT and unitary transform 60