Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 &...

19
Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado State University Spring 2015 M.R. Azimi Digital Image Processing

Transcript of Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 &...

Page 1: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Digital Image ProcessingLectures 9 & 10

M.R. Azimi, Professor

Department of Electrical and Computer EngineeringColorado State University

Spring 2015

M.R. Azimi Digital Image Processing

Page 2: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Frequency Sampling of Spatial Limited Images - RelationBetween DSFT and DFT

Recall that the DSFT of image x(m,n) is a continuous spectrumobtained by periodic extension of X(ω1, ω2) i.e.

X(Ω1,Ω2) =∑ ∞∑

m,n=−∞x(m,n)e−j(Ω1m+Ω2n)

Now, let assume that x(m,n) is of finite-extent with ROS RMN thensampling X(Ω1,Ω2) in frequency domain gives

X(k, l) = X(Ω1,Ω2) | Ω1 = k∆Ω1

Ω2 = l∆Ω2

=∑ ∞∑

m,n=−∞x(m,n)e−j(k∆Ω1m+l∆Ω2n)

M.R. Azimi Digital Image Processing

Page 3: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Now choosing ∆Ω1 = 2πM and ∆Ω2 = 2π

N , and considering thatX(Ω1,Ω2) is doubly periodic with periods 2π, the sampledspectrum (sampled at ∆Ω1 = 2π

M ,∆Ω2 = 2πN ) will be periodic with

periods M and N, thus X(k, l) becomes

X(k, l) =

M−1∑m=0

N−1∑n=0

x(m,n)e−j2π(mkM

+nlN)

= 2−D DFTx(m,n), k ∈ [0,M − 1], l ∈ [0, N − 1]

which is 2-D DFT. The 2-D IDFT is

x(p, q) =1

MN

M−1∑k=0

N−1∑l=0

X(k, l)ej2π(pkM

+ qlN)

= 2−D IDFTX(k, l), p ∈ [0,M − 1], q ∈ [0, N − 1]

M.R. Azimi Digital Image Processing

Page 4: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Replacing X(k, l) from the above gives

x(p, q) =1

MN

M−1∑k=0

N−1∑l=0

M−1∑m=0

N−1∑n=0

x(m,n)ej2π(p−m)k

M ej2π(q−n)l

N

=∑∑

m,n

[1

MN

M−1∑k=0

N−1∑l=0

ej2π(p−m)k

M ej2π(q−n)l

N ]x(m,n)

But

1

MN

M−1∑k=0

N−1∑l=0

ej2π(p−m)k

M ej2π(q−n)l

N =

1 for m = p+ rM,

n = q + sN0 otherwise

Thus

x(p, q) =

∞∑r=−∞

∞∑s=−∞

x(p+ rM, q + sN)

i.e. sampling in frequency domain results in periodic extension ofthe finite-extent signal in the spatial domain.

M.R. Azimi Digital Image Processing

Page 5: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Example 1:Find the 2-D DFT of x(m,n) = am−n, m, n ∈ [0, N − 1].

Using 2-D DFT equation, we have

X(k, l) =

N−1∑m=0

N−1∑n=0

x(m,n)WmkN Wnl

N , k, l ∈ [0, N − 1]

where WN = e−j2πN . Then

X(k, l) = N−1∑m=0

amWmkN

N−1∑n=0

a−nWnlN , k, l ∈ [0, N − 1]

X(k, l) =1− aN

1− aW kN

× 1− a−N

1− a−1W lN

, k, l ∈ [0, N − 1]

M.R. Azimi Digital Image Processing

Page 6: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Example 2:Find the 2-D DFT of x(m,n) =

∑N−1p=0 y(m, p), m, n ∈ [0, N − 1].

Assume Y (k, l) is known. Using 2-D DFT expression, we get

X(k, l) =

N−1∑m=0

N−1∑n=0

N−1∑p=0

y(m, p)WmkN Wnl

N , k, l ∈ [0, N − 1]

=

N−1∑n=0

WnlN [

N−1∑m=0

N−1∑p=0

y(m, p)WmkN W p0

N ]

= Y (k, 0)

N−1∑n=0

WnlN = Y (k, 0)Nδ(l)

M.R. Azimi Digital Image Processing

Page 7: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Remarks & Properties

1 DFT is really two things:(a) an exact Fourier representation for finite extent sequences. Inthis case remove all ”∼” notation.(b) Discrete Fourier Series (DFS) expansion for periodic sequences.

2 Basis functions of DFT have several interesting properties

exp(−2πj(mk

M+nl

N)) = exp(−2πj

mk

M) exp(−2πj

nl

N)

each term may be considered as solution to WMM = 1 and WN

N = 1

which leads to W kmM = e−j

2πM km,W ln

N = e−j2πnlN i.e. the M and N

roots of unity. Since these basis functions are both periodic in k andm or (n and l), there are only M distinct roots of unity i.e. W km

M

can take only M distinct value. Similarly WnlN has only N distinct

values.Note that ejΩm represents a unit circle, hence ej

2πkmM (Ω = 2πk

M ) isthe sampled unit circle with M samples equally spaced around theunit circle (see Fig.).

M.R. Azimi Digital Image Processing

Page 8: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

3 The basis functions form orthonormal basis over the set ofM -dimensional and N -dimensional complex vectors i.e.

< W kM (

1

MW lM )∗ >=

M−1∑m=0

e−j2πkmM ej

2πlmM = δ(k − l)

4 Both X(k, l) and x(p, q) are periodic with periods M and N .5 Similar to the bandlimited signals which can be recovered without

aliasing, if sampled above the Nyquist rate, spatially limited images(i.e. finite-extent) can be represented exactly by samples of theirDFT. However, if they are spatially unlimited the result will bespatially aliased.

6 The quadrant of 2-D DFT can be reordered to have its origin at itscenter by multiplying the image by the factor (−1)m+n prior

M.R. Azimi Digital Image Processing

Page 9: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

to taking 2-D DFT. The reason is

X(k + pM, l + qN) =

M−1∑m=0

N−1∑n=0

x(m,n) exp(−2πj(mk

M+nl

N))

exp(−2πj(pm+ qn))

For p = q = 1/2, then

X(k +M

2, l +

N

2) =

M−1∑m=0

N−1∑n=0

x(m,n)(−1)m+n

exp(−2πj(mk

M+nl

N))

which is centered at M2 ,N2 .

7 Since the dynamic range of the transform coefficients is much largerthan the exposure range of photographic film or display systems, itis necessary to compress the coefficients prior to display by using,e.g., log, operation,

D(k, l) = a log(1 + b|X(k, l)|)where a,b are appropriate constants.M.R. Azimi Digital Image Processing

Page 10: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

If |X(k, l)| = 0 then D(k, l) = 0 i.e. zero-preserving mapping.

7 X(k, l) is complex leading to this belief that DFT increasesthe dimension. However, this is not the case due to conjugatesymmetry property (i.e. X(M − k,N − l) = X∗(k, l)).

Figures show Peppers image and its magnitude of 2-D DFT afterusing log and centering operations. As can be seen, a large numberof components are insignificant. Notice any other property?

M.R. Azimi Digital Image Processing

Page 11: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Some properties of DFT that differ from those of DSFT and FTare:

1 Circular Shift (in spatial domain)We know that if a signal is linearly shifted, its DSFT is multiplied bya complex exponential. In case of “finite-extent” sequences if thesequence is shifted circularly then the DFT is multiplied by acomplex exponential.Consider a periodic 2-D image x(m,n) with periods M and N andDFS coefficients of X(k, l). Let y be the shifted (linearly) version ofx i.e. y(m,n) = x(m−m0, n− n0) thenY (k, l) = Wm0k

M Wn0lN X(k, l). The finite-extent sequence y(m,n)

defined as

y(m,n)∆=

y(m,n) m,n ∈ RMN

0 otherwise

is a circularly shifted version of x(m,n) i.e.

y(m,n) = x(((m−m0))M , ((n− n0))N )

where ((n))N∆= mod N i.e. if p = ((n))N , then 0 ≤ p ≤ N − 1 and

there exists an I such that n = p+ IN .

M.R. Azimi Digital Image Processing

Page 12: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Then the DFT y(m,n) is

Y (k, l) = Y (k, l) k, l ∈ RMN

= Wm0kM Wn0l

N X(k, l)

= Wm0kM Wn0l

N X(k, l) k, l ∈ RMN

Thus

x(((m−m0))M , ((n− n0))N )DFT↔ Wm0k

M Wn0lN X(k, l)

2 Circular Shift (in frequency domain)In a similar manner we have

W−mk0M W−nl0N x(m,n)DFT↔ X(((k − k0))M , ((l − l0))N )

i.e. multiplication of the sequences by complex exponential resultsin a circular shift in its DFT.

3 Circular ConvolutionWhat is the IDFT of the product of two DFT’s?

M.R. Azimi Digital Image Processing

Page 13: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Given two finite-extent sequence with ROS RMN , let the DFT’s ofthese sequences be

X(k, l) = DFTx(m,n)H(k, l) = DFTh(m,n)

then the IDFT of the product of these transforms, that is

y(m,n) = IDFTH(k, l)X(k, l)

=1

MN

M−1∑k=0

N−1∑l=0

H(k, l)X(k, l)WmkM Wnl

N

produces the circular convolution of h and x i.e.

y(m,n) =

M−1∑p=0

N−1∑q=0

x(p, q)h(((m− p))M , ((n− q))N )

=

M−1∑p=0

N−1∑q=0

h(p, q)x(((m− p))M , ((n− q))N )

M.R. Azimi Digital Image Processing

Page 14: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

or simply

y(m,n) = h(m,n) ~~x(m,n) = x(m,n) ~~h(m,n)

which differs from linear convolution.Remarks

1 In contrast to the linear convolution, in circular convolution h isshifted circularly and then convoluted with x.

2 Similar to linear convolution, circular convolution is Commutative,Associative and Distributive over addition.

3 Note that it is the linear convolution which is of interest to us (e.g.,in filtering) and unfortunately due to wrap-around error circularconvolution using DFT won’t produce acceptable results. However,there is a simple remedy which allows us to use DFT and performlinear convolution.

M.R. Azimi Digital Image Processing

Page 15: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Algorithm For Computing Linear Convolution using 2DDFT

Recall that the linear convolution of two finite-extent sequencesx(m,n) and h(m,n) with ROS of RM1N1 and RM2N2 , respectively,is an image of size (M1 +M2 − 1)× (N1 +N2 − 1). Now,

1 Choose P and Q such that P ≥M1 +M2 − 1,Q ≥ N1 +N2 − 1. Note: we typically choose P = 2p andQ = 2q i.e. powers of 2 for radix-2 or 4 FFT.

2 Zero-pad both h(m,n) and x(m,n) to have ROS of RPQ.

3 Compute (P ×Q)-points DFT’s of x(m,n) and h(m,n) andtake IDFT of the product X(k, l)H(k, l) of size (P ×Q). Thefirst (M1 +M2 − 1)× (N1 +N2 − 1) portion of the resultcontains the desired linear convolution result.

M.R. Azimi Digital Image Processing

Page 16: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

Algorithm For Computing Linear Convolution using 2DDFT-Cont

Example (Circular & Linear Convolution):

Consider 2-D arrays x(m,n) =1 02 1

,h(m,n) =1 01 1

. Find y(m,n)

and y(m,n).The 2× 2 DFT of these sequences give

X(k, l) =

1∑m=0

1∑n=0

x(m,n)e−j2πmk

2 e−j2πnl

2

X(0, 0) = x(0, 0) + x(0, 1) + x(1, 0) + x(1, 1) = 4

X(0, 1) = x(0, 0) + x(0, 1)e−jπ + x(1, 0) + x(1, 1)e−jπ

= x(0, 0)− x(0, 1) + x(1, 0)− x(1, 1)

= 1− 0 + 2− 1 = 2

X(1, 0) = x(0, 0) + x(0, 1) + x(1, 0)e−jπ + x(1, 1)e−jπ

= x(0, 0) + x(0, 1)− x(1, 0)− x(1, 1) = −2

M.R. Azimi Digital Image Processing

Page 17: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

X(1, 1) = x(0, 0) + x(0, 1)e−jπ + x(1, 0)e−jπ + x(1, 1)e−j2π

= 0

Thus X(k, l) =4 2−2 0

, H(k, l) =3 1−1 1

. and

Y (k, l) = X(k, l)H(k, l) =12 22 0

. Taking IDFT we get

y(m,n) =4 33 2

. This result can alternatively be obtained using

the circular convolution in the spatial domain i.e.

y(m,n) =1∑p=0

1∑q=0

x(p, q)h(((m− p))2, ((n− q))2)

y(0, 0) = x(0, 0)h(0, 0) + x(0, 1)h(0, 1) + x(1, 0)h(1, 0)

+x(1, 1)h(1, 1) = 4

M.R. Azimi Digital Image Processing

Page 18: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

y(0, 1) = x(0, 0)h(0, 1) + x(0, 1)h(0, 0) + x(1, 0)h(1, 1)

+x(1, 1)h(1, 0) = 3

y(1, 0) = x(0, 0)h(1, 0) + x(0, 1)h(1, 1) + x(1, 0)h(0, 0)

+x(1, 1)h(0, 1) = 3

y(1, 1) = x(0, 0)h(1, 1) + x(0, 1)h(1, 0) + x(1, 0)h(0, 1)

+x(1, 1)h(0, 0) = 2

Now, let us find the result of the desired linear convolution firstusing the direct method and then using the DFT.

y(0, 0) = x(0, 0)h(0, 0) = 1

y(0, 1) = x(0, 0)h(0, 1) + x(0, 1)h(0, 0) = 0...

y(2, 1) = x(1, 1)h(1, 0) + x(1, 0)h(1, 1) = 3

y(2, 2) = x(1, 1)h(1, 1) = 1

M.R. Azimi Digital Image Processing

Page 19: Digital Image Processing Lectures 9 & 10 · 2015. 2. 12. · Digital Image Processing Lectures 9 & 10 M.R. Azimi, Professor Department of Electrical and Computer Engineering Colorado

y(m,n) =1 0 03 2 02 3 1

Note: y(m,n) is the aliased version of this array. Extend arrays tosize P = 4 ≥M1 +M2 − 1, Q = 4 ≥ N1 +N2 − 1 i.e.

x′(m,n) =

1 0 0 02 1 0 00 0 0 00 0 0 0

, h′(m,n) =

1 0 0 01 1 0 00 0 0 00 0 0 0

Taking 4× 4 DFT and IDFT of the product gives

y′(m,n) =

1 0 0 03 2 0 02 3 1 00 0 0 0

which is y(m,n) if the last row and column are removed.M.R. Azimi Digital Image Processing