Frequency Domain Filtering Dr. Sk. Md. Masudul Ahsan Dept. of CSE, KUET Background Any function that periodically repeats itself can be expressed as the sum of sines and/or cosines of different frequencies, each multiplied by a different coefficient (Fourier series). Fourier transform even functions that are not periodic (but whose area under the curve is finite) can be expressed as the integral of sines and/or cosines multiplied by a weighting function Background Periodic function f Frequency Weight f1 w1 f2 w2 f3 w3 f4 w4 Jean Baptiste Joseph Fourier (1768-1830) French mathematician had crazy idea (1807): Any univariate function can be rewritten as a weighted sum of sines and cosines of different frequencies. • ...the manner in which the author arrives at these equations is not exempt of difficulties and...his analysis to integrate them still leaves something to be desired on the score of generality and even rigour. Don’t believe it? – – Neither did Lagrange, Laplace, Poisson and other big wigs Not translated into English until 1878! Laplace But it’s (mostly) true! • – – called Fourier Series there are some subtle restrictions Lagrange Legendre Background Fourier transform we will be dealing only with functions (images) of finite duration, so the Fourier transform is the tool in which we are interested. A function, expressed in either a Fourier series or transform, can be reconstructed (recovered) completely via an inverse process, with no loss of information The frequency domain refers to the plane of the two dimensional discrete Fourier transform of an image. Frequency Spectra example : g(t) = sin(2πf t) + (1/3)sin(2π(3f) t) = + Slides: Efros Frequency Spectra Frequency Spectra 1 = A sin(2 kt ) k 1 k 1D Fourier Transform Fourier transform F(u) of a continuous function f(x) is: e Forward Fourier transform: F (u ) f ( x )e j 2 ux dx j 2 ux du j cos j sin Inverse transform: f (x) F (u )e j 1 • (u) is the frequency variable. • F(u) is composed of an infinite sum of sine and cosine terms • Each value of u determines the frequency of its corresponding sine-cosine pair. 1D Discrete Fourier Transform (DFT) A continuous function f(x) is discretized into a sequence: { f ( x0 ), f ( x0 x), f ( x0 2x),..., f ( x0 [ N 1]x)} by taking N or M samples x units apart. 1D DFT Where x assumes the discrete values (0,1,2,3,…,M-1) then f ( x) f ( x0 xx) The sequence {f(0),f(1),f(2),…f(M-1)} denotes any M uniformly spaced samples from a corresponding continuous function. 1D DFT f(x), x=0,1,…,M-1. discrete function F(u), u=0,1,…,M-1. DFT of f(x) Forward discrete Fourier transform: 1 F (u ) M M 1 f ( x )e M 1 u x M x0 Inverse transform: f (x) j2 F (u )e j2 u x M u0 The location of 1/M multiplier doesn’t matter in front of the Fourier transform in front of the inverse Fourier transform 1D DFT Since e j cos j sin and the fact cos( ) cos then discrete Fourier transform can be written as 1 F (u ) M M 1 f ( x)[cos 2ux / M j sin 2ux / M ] x 0 for u 0,1,2,..., M 1 1D DFT The Fourier transform of a real function is generally complex imaginary F(u) can be expressed in polar m coordinates: real j ( u ) F (u ) F (u ) e 2 2 where F (u ) R (u ) I (u ) 1 2 I (u ) (u ) tan R ( u ) R(u): the real part of F(u) I(u): the imaginary part of F(u) Magnitude or spectrum 1 Power spectrum: Phase angle or phase spectrum 2 P(u ) F (u ) R 2 (u ) I 2 (u ) 1D – Fourier Transform-example 1D DFT - example 2D DFT Forward transform F (u , v) f ( x, y )e j 2 ( ux vy ) a-periodic signal periodic transform x y 1 F (u , v) MN M 1 N 1 f ( x, y )e j 2 (ux / M vy / N ) x 0 y 0 for u 0,1,2,..., M 1, v 0,1,2,..., N 1 Inverse transform: M 1 N 1 f ( x, y ) F (u , v)e j 2 ( ux / M vy / N ) u 0 v 0 for x 0,1,2,..., M 1, y 0,1,2,..., N 1 u, v : the transform or frequency variables x, y : the spatial or image variables periodized signal periodic and sampled transform 2D DFT Similarly, we define the Fourier spectrum, phase angle, and power spectrum as follows: F (u , v) F (u , v) e j (u ,v ) F (u , v) R 2 (u, v) I 2 (u , v) 1 2 ( spectrum) I (u , v) (u, v) tan (phase angle) R(u , v) 1 2 P(u,v) F (u , v) R 2 (u , v) I 2 (u, v) (power spectrum) R(u,v): the real part of F(u,v) I(u,v): the imaginary part of F(u,v) 2D DFT – Properties: Separability The 2D FT can be implemented as two consecutive 1D FTs: first in the x direction, then in the y direction (or vice versa). 1 F1 (u, y ) M 1 F (u , v) N Proof: DIY M 1 f ( x, y )e j 2 (ux / M ) x 0 M 1 j 2 ( vy / N ) F ( u , y ) e 1 y 0 2D DFT – Properties: Separability Separable: The 2D DFT F(u,v) can be obtained by 1. taking the 1D DFT of every row of image f(x,y), F(u,y), 2. taking the 1D DFT of every column of F(u,y), F(u,v), (a)f(x,y) (b)F(u,y) (c)F(u,v) 2D DFT – Properties: Shifting The FT of a shifted function is unaltered except for a linearly varying phase factor. f ( x x 0 , y y 0 ) F ( u , v ) e j 2 ( ux / M vy / N ) 0 0 Time / spatial shift and f ( x, y ) e j 2 (u 0 x / M v0 y / N ) F (u u 0 , v v 0 ) frequency shift 2D DFT – Properties: Shifting The FT of a shifted function is unaltered except for a linearly varying phase factor. f ( x x 0 , y y 0 ) F ( u , v ) e j 2 ( ux / M vy / N ) 0 0 2D DFT – Properties: Shifting The FT of a shifted function is unaltered except for a linearly varying phase factor. f ( x x 0 , y y 0 ) F ( u , v ) e j 2 ( ux / M vy / N ) 0 0 Proof: 1 DFT [ f ( x x 0 , y y 0 )] MN e j 2 [ ux 0 / M vy 0 / N ] 1 MN M 1 N 1 f (x x 0 , y y 0 )e j 2 ( ux / M vy / N ) x0 y 0 M 1 N 1 f (x x , y y 0 x 0 y 0 e j 2 [ ux 0 / M vy 0 / N ] F ( u , v ) 0 )e j 2 [ u ( x x0 ) v ( y y 0 ) ] M N 2D DFT – Properties: Shifting If, u0 = M/2, v0 = N/2 f ( x, y ) e j 2 (u0 x y v0 ) M N F (u u 0 , v v 0 ) Becomes f ( x , y ) ( 1) x y F ( u M / 2 , v N / 2 ) 0 x u u y F v shift v 2D DFT – Properties: Periodicity A [M,N] point DFT is periodic with period [M,N] F(u,v) = F(u+M, v ) = F(u, v+N) = F(u+M, v+N) = F(u+pM, v + qN) 1 Proof: F ( u , v ) MN M 1 N 1 x0 y 0 1 F (u M , v N ) MN 1 MN f ( x , y )e j 2 ( ux / M vy / N ) M 1 N 1 x0 y 0 M 1 N 1 f ( x , y )e v N uM j 2 x y N M f ( x , y )e v u j 2 x y N M .e N M j 2 x y N M x0 y 0 e j 2 ( x y ) F (u , v ) F (u , v ) cos 2 n 1, sin 2 n 0 2D DFT – Properties: Periodicity 2D DFT – Properties: Rotation if a function is rotated, then its Fourier transform rotates an equal amount. DFT DFT 2D DFT – Properties: Rotation y y x x r cos( ) ; y r sin( ) r x x r cos y r sin Define a new coord system (x’, y’), where x r cos cos r sin sin => y r sin cos r cos sin x x cos y sin => y x sin y cos We can also express (x, y) in terms of the new coordinate system, where x x cos y sin ; y x sin y cos 2D DFT – Properties: Rotation v v u u Similarly, in Fourier domain u cos v sin u cos( ) ; v sin( ) u u cos v sin ; v u sin v cos in terms of the new coordinate system, where u u cos v sin ; v u sin v cos 2D DFT – Properties: Rotation Proof: DFT f ( x , y ) F ( u , v ) DFT f ( r cos , r sin ) F ( cos , w sin ) DFT f ( x , y ) DFT f ( r cos( ), r sin( )) f ( x , y )e j 2 ( xu yv ) x y f ( x , y )e j 2 ( x cos y sin ) u ( x sin y cos ) v x y x y j 2 x ( u cos v sin ) y ( u sin v cos ) f ( x , y )e 2D DFT – Properties: Rotation f ( x , y )e j 2 x u y v x y F ( u , v ) F ( cos( ), sin( )) 2D DFT – Properties: Convolution theorem ~ states that convolution in time domain corresponds to multiplication in frequency domain and vice versa The discrete convolution of two functions f(x,y) and h(x,y) of size MXN is defined as M 1 N 1 f ( x, y ) h( x, y ) f (a, b)h( x a, y b) a 0 a 0 Let F(u,v) and H(u,v) denote the Fourier transforms of f(x,y) and h(x,y), then f ( x, y ) h( x, y ) F (u , v) H (u , v) Eq. (4.2-31) f ( x , y ) h ( x , y ) F (u , v ) H ( u , v ) Eq. (4.2-32) 2D DFT – Properties: Convolution theorem Proof: M 1 N 1 M 1 N 1 j 2 ( ux / M vy / N ) DFT f ( x, y ) h( x, y ) f (a, b)h( x a, y b)e x 0 y 0 a 0 b 0 M 1 N 1 M 1 N 1 f (a, b)h( x a, y b)e u ( x a a ) v ( y b b ) j 2 M N x 0 y 0 a 0 b 0 M 1 N 1 f (a, b)e ua vb j 2 M 1 N 1 M N a 0 b 0 F (u , v) H (u , v) h( x a, y b)e x 0 y 0 u ( x a ) v ( y b ) j 2 M N 2D DFT Some other properties of Fourier transform: 1 F ( 0 ,0 ) MN M 1 N 1 f ( x, y) x0 y0 F(0,0) is the average intensity of an image 2-D DFT – importance of phase angle Properties in the frequency domain Each term of F(u,v) contains all values of (x,y), modified by the values of the exponential terms DC component: the slowest varying frequency component (u=v=0) corresponds to the average gray level of an image The higher frequencies begin to correspond to faster and faster gray level changes in the image Edges of objects Components of an image characterized by abrupt changes in gray level, such as noise Properties in the frequency domain Fourier transform works globally No direct relationship between a specific components in an image and frequencies Intuition about frequency Frequency Rate content of change of gray levels in an image 2D DFT - example 2D DFT - example Strong edges that run approximately at ±45. And the two white oxide protrusions resulting from thermally induced failure the Fourier spectrum shows prominent components along the ±45 directions that correspond to the edges Vertical component that is off-axis slightly to the left this component was caused by the edges of the oxide protrusions the off-axis angle of the frequency component corresponds to the inclination off horizontal of the long white element zeros in the vertical frequency component, corresponding to the narrow vertical span of the oxide protrusions Basics of Filtering in the Frequency Domain To filter an image in the frequency domain: 1. 2. 3. Compute F(u,v) the DFT of the image Multiply F(u,v) by a filter function H(u,v) Compute the inverse DFT of the result Steps for Filtering in the Frequency Domain Steps for Filtering in the Frequency Domain Steps for Filtering in the Frequency Domain In general, the components of F are complex quantities, but the filters with which we deal typically are real zero-phase-shift filters: do not change the phase of the transform if the input image and the filter function are real, the imaginary components of the inverse transform should all be zero in practice, the inverse DFT generally has parasitic imaginary components due to computational round-off errors Notch Filter Suppose that we wish to force the average value of an image to zero set F(0,0) to zero & leave all other frequency components of the Fourier transform untouched 0 if (u , v) ( M / 2, N / 2) H (u , v) otherwise. 1 a constant function with a hole (notch) at the origin Note the drop in overall average gray level resulting from forcing the average value to zero make prominent edges stand out Notch Filter In reality the average of the displayed image cannot be zero display the most negative value as 0, with all other values scaled up from that Smoothing Frequency-Domain Filters The basic model for filtering in the frequency domain G (u , v) H (u, v) F (u , v) where F(u,v): the Fourier transform of the image to be smoothed H(u,v): a filter transfer function Smoothing (blurring) is achieved in the frequency domain by attenuating a specified range of high-frequency components in the transform of a given image Smoothing is a low pass operation in the frequency domain. There are several standard forms of low pass filters (LPF). Ideal low pass filter Butterworth low pass filter Gaussian low pass filter Ideal Low pass Filter (ILPF) The simplest low pass filter is a filter that “cuts off” all high-frequency components of the Fourier transform that are at a distance greater than a specified distance D0 from the origin of the transform. The transfer function of an ideal low pass filter 1 H (u , v) 0 if D(u , v) D0 if D(u , v) D0 where D(u,v) : the distance from point (u,v) to the center of their frequency rectangle D(u , v) (u M / 2) 2 (v N / 2) 1 2 2 ILPF ILPF ILPF - example ILPF - example As the filter radius increases, less & less power is removed, resulting in less server blurring The ringing effect is a characteristic of ideal filters For α=99.95, only slight blurring in the noisy squares are shown the little edge information is contained in the upper 0.5% of the spectrum power in this case ILPF – ringing effect It has a central dominant circular component (providing the blurring) It has concentric circular components (rings) giving rise to the ringing effect spatial Figure 4.13 (a) A frequency-domain ILPF of radius 5. (b) Corresponding spatial filter. (c) Five impulses in the spatial domain, simulating the values of five pixels. (d) Convolution of (b) and (c) in the spatial domain. f ( x, y ) h( x, y ) F (u , v) H (u, v) spatial Butterworth Low pass Filters (BLPFs) With order n 1 H (u, v) 2n 1 D(u , v) / D0 Filter function for high values of “filter order” the Butterworth filter approaches the form of the ideal lowpass filter for lower-order values, the Butterworth filter has a smooth form similar to the Gaussian filter BLPF BLPF does not have a sharp discontinuity that establishes a clear cutoff between passed and filtered frequencies H(u,v)=0.5 (down 50% from its maximum value of 1) when D(u,v)=D0 BLPF - example A smooth transition in blurring as a function of increasing cutoff frequency No ringing is visible due to the smooth transition between low & high frequencies n=2 D0=5,15,30,80,and 230 BLPF - Spatial Representation Ringing becomes a significant factor in filters of higher order Order 2: a good compromise between effective lowpass filtering & acceptable ringing characteristics n=1 n=2 n=5 n=20 Gaussian Lowpass Filters (GLPF) GLPF The inverse Fourier transform of the GLPF also is Gaussian Guarantees no ringing GLPF - example As in the case of the BLPF, a smooth transition in blurring as a function of increasing cutoff frequency No ringing The GLPF did not achieve as much smoothing as the BLPF of order 2 for the same value of cutoff frequency BLPF is a more suitable choice if: tight control of the transition between low & high frequencies about the cutoff frequency are needed the price: ringing D0=5,15,30,80,and 230 Additional Examples of Lowpass Filtering Examples: fax transmissions, duplicated material & historical records The characters have distorted shapes due to lack of resolution, and many of the characters are broken solution: bridge small gaps by blurring D0: 80 Additional Examples of Lowpass Filtering Printing and publishing industry: “cosmetic” processing Different lowpass Gaussian filters used to remove blemishes in a photograph Sharpening Frequency Domain H hp (u , v) 1 H lp (u , v) Ideal highpass filter 0 H (u , v) 1 if D (u , v) D0 if D (u, v) D0 Butterworth highpass filter H (u , v ) 1 2n 1 D0 / D(u , v) Gaussian highpass filter H (u , v ) 1 e D 2 ( u ,v ) / 2 D02 Highpass Filters Spatial Representations A dot in the center Ideal Highpass Filters 0 H (u, v) 1 if D (u, v) D0 if D (u, v) D0 Highpass Filters Butterworth 1 H (u, v) 2n 1 D0 / D (u, v) Gaussian H (u , v) 1 e D 2 ( u ,v ) / 2 D02 Text orientation finding - Example Text recognition using image processing techniques is simplified if we can assume that the text lines are in a predefined direction. Here we show how the Fourier Transform can be used to find the initial orientation of the text and then a rotation can be applied to correct the error. Text orientation finding - Example The logarithm of the magnitude of its Fourier transform are We can see that the main values lie on a vertical line, indicating that the text lines in the input image are horizontal.