Frequecy Domai Filterig Raga Rodrigo October 19, 2010 Outlie Cotets 1 Itroductio 1 2 Fourier Represetatio of Fiite-Duratio Sequeces: The Discrete Fourier Trasform 1 3 The 2-D Discrete Fourier Trasform 4 Filterig i the Frequecy Domai 6 Obtaiig Frequecy Domai Filters from Spatial Domai Filters 8 1 Itroductio I the previous lecture we studied some spatial filterig techiques. However, to thoughtfully uderstad filterig, we eed the kowledge of the frequecy domai ad the Fourier trasform. The Fourier trasforms ad Fourier series are due to the Frech mathematicia Jea Baptiste Joseph Fourier. Fourier s cotributio states that ay periodic fuctio ca be expressed as the sum of sies ad or cosies of differet frequecies, each multiplied by a differet coefficiet. This sum is called the Fourier series. Eve fuctios that are ot periodic, but have fiite areas uder the curve, ca be expressed as a itegral of sies ad cosies multiplied by a weightig fuctio. We call this formulatio Fourier trasform. Both represetatio share the importat characteristic that a fuctio, expressed i either the Fourier series or trasform, ca be recostructed (recovered) via a iverse process. As the images that we will deal with are fuctios of fiite domai, we will study about the Fourier trasform. I particular we will study the two-dimesioal discrete Fourier trasform. 2 Fourier Represetatio of Fiite-Duratio Sequeces: The Discrete Fourier Trasform The Discrete Fourier Trasform 1
The discrete Fourier trasform is the Fourier represetatio of fiite-duratio sequeces. We begi by cosiderig a fiite-legth sequece x[] of legth N samples such that x[] = 0 outside the rage 0 N 1. To each fiite-legth sequece we ca always associate a periodic sequece x[]. The periodic sequece x[] is give by x[] = r = x[ + r N ]. The fiite-legth sequece x[] ca be recovered from x[]. { x[], 0 N 1, x[] = 0, otherwise. (1a) (1b) DFS coefficiets of x[] are samples (spaced i frequecy by 2π/N ) of the Fourier trasform of x[]. Sice x[] is assumed to have fiite legth N, there is o overlap betwee the terms x[ + r N ] for differet values of r. Thus Eq. 1a ca alteratively be writte as Usig the otatio (()) N to deote ( modulo N ); x[] = x[( modulo N )]. (2) x[] = x[(()) N ]. (3) The sequece of discrete Fourier series coefficiets X [k] of the periodic sequece x[] is itself a periodic sequece with period N. To maitai a duality betwee the time ad frequecy domais, we will choose the Fourier coefficiets that we associate with a fiite-duratio sequece to be a fiite-duratio sequece correspodig to oe period of X [k]. This fiite-duratio sequece, X [k], will be referred to as the discrete Fourier trasform (DFT). Thus the DFT, X [k], is related to the DFS coefficiets, X [k], by X [k] = { X [k], 0 k N 1, 0, otherwise, (4) ad X [k] = X [(k modulo N )] = X [((k)) N ]. () From our discussio o DFS, X [k] ad x[] are related by ad X [k] = x[]w k N, (6) x[] = 1 N =0 k=0 X [k]w k N. (7) 2
For coveiece, these equatios are ofte writte i terms of the complex quatity W N, defied as W N = e j (2π/N ). (8) Sice the summatio i Eqs. 6 ad 7 ivolve oly the iterval betwee 0 ad N 1, it follows that ad X [k] = { N 1 k =0 x[]w N 0 k N 1, 0, otherwise, { 1 N 1 x[] = X N [k]w k k=0 N 0 N 1, 0, otherwise. (9) (10) Geerally the DFT aalysis ad sythesis equatios are writte as Aalysis equatio: X [k] = x[]w k N, (11) =0 ad where Sythesis equatio: x[] = 1 N W N = e j (2π/N ). k=0 X [k]w k N. (12) That is, the fact that X [k] = 0 for k outside the iterval 0 k N 1 ad that x[] = 0 for outside the iterval 0 N 1 is implied but ot always stated explicitly. The relatioship betwee x[] ad X [k] implied by Eqs. 11 ad 12 will sometimes be deoted as x[] DF T X [k]. (13) I Eqs. 11 ad 12 for fiite-duratio sequece, we have ot elimiated the iheret periodicity. I defiig the DFT represetatio we are simply recogizig that we are iterested i values of x[] oly i the iterval 0 N 1 because x[] is really zero outside that iterval, ad we are iterested i values of X [k] oly i the iterval 0 k N 1 because these are the oly values eeded i Eq. 12. Example 1. Determie the DFT of the fiite-duratio sequece x[] show i Fig. 1 by cosiderig the legth to be 1. N =, ad 2. N = 10. 3
x[] Figure 1: Fiite-legth sequece x[]. x[] x[] X [k] X (e j ω ) -2-1 0 1 2 3 4 6 7 8 9 10 11 k, ω X [k] -2-1 0 1 2 3 4 6 7 8 9 10 11 k Figure 2: DFT for the fiite-legth sequece x[]. N =. x[] x[] X [k] X (e j ω ) -2-1 0 1 2 3 4 6 7 8 9 10 11 k, ω X [k] -2-1 0 1 2 3 4 6 7 8 9 10 11 k Figure 3: DFT for the fiite-legth sequece x[]. N = 10. 4
3 The 2-D Discrete Fourier Trasform 2-D DFT Equatio The 2-D discrete Fourier trasform (DFT) is give by F (u, v) = M 1 x=0 y=0 f (x, y)e j 2π(ux/M+v y/n ), (14) where f (x, y) is a digital image of size M N. x ad y are the spatial variables ad u ad v are the frequecy-domai variables. Equatio 14 must be evaluated for values of the discrete variables u ad v i the rages u = 0,1,..., M 1 ad v = 0,1,..., N 1. Iverse 2-D DFT Equatio Give the trasform F (u, v), we ca obtai f (x, y) by usig the iverse discrete Fourier trasform (IDFT): f (x, y) = 1 M N M 1 u=0 v=0 for x = 0,1,... M 1 ad y = 0,1,... N 1. F (u, v)e j 2π(ux/M+v y/n ), (1) Zero Ceterig Whe we apply Equatio 14 to obtai the DFT, the four corers of the DFT image will correspod to the low frequecies ad the ceter correspods to high frequecies. Visual aalysis of the spectrum is simplified by movig the values at the origi of the trasform to the ceter of the frequecy rectagle. This ca be accomplished by multiplyig f (x, y) by ( 1) x+y prior to the computatio of the 2-D Fourier trasform. Figure 4 shows the Fourier spectrum, this trasformatio ad visual ehacemet usig log(1 + F ). Usig Matlab for DFT The followig code will produce 4(b). f = imread ( image.bmp ) ; F = f f t 2 ( f ) ; S = abs (F ) ; imshow( S, [ ] ) ; The followig code will produce 4(c). f = imread ( image.bmp ) ; F = f f t 2 ( f ) ; Fc = f f t s h i f t (F ) ; imshow( abs ( Fc ), [ ] ) ; The followig code will produce 4(d). f = imread ( image.bmp ) ; F = f f t 2 ( f ) ; Fc = f f t s h i f t (F ) ; S2 = log (1+ abs ( Fc ) ) ; imshow( S2, [ ] ) ;
(a) Image (b) Fourier spectrum (c) Cetered spectrum (d) Ehaced spectrum Figure 4: DFT of a simple image. Zero Paddig If we are to use the DFT for filterig, it is ecessary to pad the iput image with zeros. I this case the sytax becomes F = f f t 2 ( f, P, Q) ; Here, fft2 pads the iput with the required umber of zeros so that the resultig fuctio is of size P Q. Iverse DFT Iverse DFT is computed as f = i f f t 2 (F ) ; To remove the possible small imagiary part, we may have to use f = r e a l ( i f f t 2 (F ) ) ; 4 Filterig i the Frequecy Domai Filterig i the frequecy domai is coceptually simple. The foudatio for liear filterig i both the spatial ad frequecy domai is the covolutio theorem. ad, coversely f (x, y) h(x, y) H(u, v)f (u, v) (16) f (x, y)h(x, y) H(u, v) F (u, v) (17) Here the symbol icidets the covolutio of the two fuctios. Whe the kerel h(x, y) is symmetric, this is similar to spatial filterig. These equatios show that the covolutio of two spatial fuctios correspod to the product of Fourier trasforms of the kerel ad the image. H(u, v) is referred to as the filter trasfer fuctio. 6
Basic Steps i DFT Filterig 1. Obtai the paddig parameters usig the fuctio paddedsize: PQ = paddedsize ( side ( f ) ) ; 2. Obtai the Fourier trasform with paddig: F = f f t 2 ( f, PQ( 1 ), PQ( 2 ) ) ; 3. Geerate a filter fuctio, H, of size PQ(1) PQ(2). The filter must also me cetered. 4. Multiply the trasform by the filter: G = H. * F ;. Obtai the real part of the iverse FFT of G: g = r e a l ( i f f t 2 (G) ) ; 6. Crop the top-left rectagle to the origial size: g = g ( 1 : s i z e ( f, 1 ), 1 : s i z e ( f, 2 ) ) ; A m-fuctio for Filterig i Frequecy Domai The six steps that we discussed above ca be captured i to a m-fuctio. Example 2. Write a m-fuctio that takes two argumets, a image f ad a filter trasfer fuctio H, ad geerates a filtered image g. What are the coditios ecessary? 7
Obtaiig Frequecy Domai Filters from Spatial Domai Filters I geeral, filterig i the spatial domai is more computatioally efficiet tha frequecy domai filterig whe the filters are small. Spatial filters ca be coverted to equivalet frequecy domai filters usig the freqz2 fuctio: H = freqz2 (h, R,C) where h is a 2-D spatial filter ad H is the correspodig 2-D frequecy domai filter. Here, R ad C are the umber of rows ad colums the we wish the filter H to have. Example 3. Obtai the frequecy domai filter correspodig to the Sobel filter to detect vertical edges. Example 4. Write a program to carry out Sobel filterig i frequecy domai. 8