13.07.2015 Views

Introduction to the Frequency Domain

Introduction to the Frequency Domain

Introduction to the Frequency Domain

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 2 / 1


Fourier transforms use periodic boundary conditions (we had talked aboutthis before). So, in order <strong>to</strong> make our signal looks nice and periodic, wemodify <strong>the</strong> image so that it goes smoothly <strong>to</strong>wards zero around <strong>the</strong> edgesof <strong>the</strong> image. In practice, we don’t need <strong>to</strong> do something as drastic asthis, but in <strong>the</strong> example, <strong>the</strong> intermediate results look nicer.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 3 / 1


1 image = mean ( imread (" bridge . jpg "),axis =2)2 image = image - mean ( image )3 w,h = image . shape4 ys ,xs = meshgrid ( range (-h//2 ,h-h //2) ,range (-w//2 ,w-w //2) )5 r = minimum (h//2 - abs (ys),w//2 - abs (xs))6 r = 1.0 - exp (-r **2/80.0**2)7 image = r* image8 imrow ( image )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 4 / 1


1 plot ( image [200 ,:])Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 5 / 1


Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 6 / 1


The Fourier transform is a linear, invertible transformation of <strong>the</strong> imagethat yields a complex result. The Fourier transform for images isimplemented by <strong>the</strong> fft2 function, and its inverse by <strong>the</strong> ifft2 function.In principle, ifft2(fft2(image)) should be <strong>the</strong> identity function, butifft2 returns complex numbers; <strong>the</strong> imaginary part is nearly zero(roundoff error), so we are just interested in <strong>the</strong> real part.Note that <strong>the</strong> Fourier transformation is not shift invariant.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 7 / 1


1 f = fft2 ( image )2 imrow ( image , real ( ifft2 (f)))3 print amax ( abs ( imag ( ifft2 (f))))6.03959719156e-13Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 8 / 1


What is this mysterious Fourier transform f?We can’t really easily tell, because it is complex. However, <strong>the</strong> magnitudeis kind of instructive. Since <strong>the</strong> magnitude spans a very large range, wecompress <strong>the</strong> range with a γ = 0.2 .Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 9 / 1


1 imrow ( image , abs (f),abs (f) **0.2)Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 10 / 1


Let’s try some simple test images. The most obvious one is <strong>the</strong> impulse.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 11 / 1


1 impulse = zeros ((256 ,256) )2 impulse [128 ,128] = 1.03 spectrum = abs ( fft2 ( impulse )) **0.24 imrow ( impulse , spectrum )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 12 / 1


Didn’t that work? Actually, it did, but <strong>the</strong> output is constant.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 13 / 1


1 plot ( spectrum [10 ,:])Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 14 / 1


1 f = fft2 ( impulse ) [50 ,50:100]2 plot (f. real )3 plot (f. imag )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 16 / 1


Maybe something slightly bigger than an impulse gives us a better idea.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 17 / 1


1 step = zeros ((256 ,256) )2 step [112:144 ,112:144] = 1.03 spectrum = abs ( fft2 ( step )) **0.24 imrow (step , spectrum )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 18 / 1


That’s still pretty mysterious.Actually, it turns out that <strong>the</strong> signal with <strong>the</strong> simplest 2D Fouriertransform is a sine wave. This will become clearer when we look at thisagain in processing of sound signals later.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 19 / 1


1 ys ,xs = meshgrid ( range (300) ,range (300) )2 wave = sin (( xs +2.0* ys)*pi /15.0)3 spectrum = abs ( fft2 ( wave )) **0.24 imrow (wave , spectrum , spectrum [:50 ,:50])Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 20 / 1


Notice that <strong>the</strong>re is just a single pixel at (10,20). Why is it <strong>the</strong>re?Roughly speaking, because <strong>the</strong>re are 30 repeats in <strong>the</strong> y direction, and 60repeats in <strong>the</strong> x direction.We can now mix multiple of <strong>the</strong>se signals and <strong>the</strong> Fourier transform willresolve <strong>the</strong>se components for us.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 21 / 1


1 wave = sin (( xs +2.0* ys)*pi /15.0) + cos ((3.0* xs+ys)*pi /15.0) + sin (( xs+ys)*pi /15.0) +3.0* sin (xs*pi /150.0) +3.0* sin (ys*pi /150.0)2 spectrum = abs ( fft2 ( wave )) **0.23 imrow (wave , spectrum , spectrum [:40 ,:40])Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 22 / 1


Fourier transforms model images as being composed of sine waves that areshifted and added.This turns out not <strong>to</strong> be a very good statistical model of real-worldimages. It is, however, a good model for audio and radio signals, which iswhere most of <strong>the</strong> <strong>the</strong>ory comes from. And <strong>the</strong> <strong>the</strong>ory works even if <strong>the</strong>images are not naturally represented using <strong>the</strong> basis functions of <strong>the</strong> filters.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 23 / 1


Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 24 / 1


The basic idea behind frequency domain filtering is <strong>to</strong> perform a Fouriertransform, select a subset of <strong>the</strong> frequencies–those we are interestedin–and <strong>the</strong>n transform back.Now, <strong>the</strong> low frequencies happen <strong>to</strong> be in <strong>the</strong> corners of <strong>the</strong> Fouriertransform (this is just a convention).So, we take <strong>the</strong> original Fourier transform and just copy values from itscorners, <strong>the</strong>n transform back. And let’s also transform back everything but<strong>the</strong> values we selected.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 25 / 1


1 f = fft2 ( image )2 f2 = zeros (f. shape , dtype = complex )3 d = 304 f2 [:d ,:d] = f[:d ,:d]5 f2 [:d,-d:] = f[:d,-d:]6 f2[-d:,:d] = f[-d:,:d]7 f2[-d:,-d:] = f[-d:,-d:]8 imrow ( abs (f) **0.2 , abs (f2)**0.2 , abs (f-f2) **0.2)9 imrow ( image , real ( ifft2 (f2)),real ( ifft2 (f-f2)))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 26 / 1


What we can see here is that if we just select low frequencies (a low passfilter), we get a filter that looks like a smoothing filter.If we just select high frequencies (a high pass filter, we get a filter thatlooks like a derivative filter.However, <strong>the</strong>re is a lot of ringing in <strong>the</strong>se images, <strong>the</strong> little wavy blacklines surrounding edges in <strong>the</strong> filtered images. Strict low-pass or high-passfilters show this effect.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 27 / 1


Filters in <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong>


Speedup from FFTThe Fourier transform is a very special kind of filter, because it transformsconvolutions in<strong>to</strong> multiplications.Let’s write F[s] for <strong>the</strong> Fourier transform of <strong>the</strong> signal s . Then,F[s ∗ t] = F[s] · F[t] (1)ors ∗ t = F −1 [F[s] · F[t]] (2)This matters a great deal because using <strong>the</strong> fast Fourier transformalgorithm (FFT), we can compute <strong>the</strong> Fourier transform of a signal withN elements in time N log N .So, a naive implementation of convolution of two sequences of length Ntakes time O(N 2 ) , while a convolution implemented with Fouriertransforms takes time O(N log N) . Radios, disk drive, cell phones, andmany o<strong>the</strong>r modern devices take advantage of this.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 29 / 1


Let us explore this issue computationally a bit more by looking at <strong>the</strong>Gaussian filter in <strong>the</strong> image domain and <strong>the</strong> frequency domain.1 def impulse (w,h):2 image = zeros ((w,h))3 image [w//2 ,h //2] = 1.04 return imageThomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 30 / 1


Let’s compute <strong>the</strong> impulse response of <strong>the</strong> Gaussian filter.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 31 / 1


1 from scipy . ndimage import filters2 gaussian = filters . gaussian_filter ( impulse (64 ,64) ,8.0)3 imrow ( gaussian )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 32 / 1


We can convolve with that directly <strong>to</strong> obtain <strong>the</strong> following image.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 33 / 1


1 result1 = filters . convolve ( image , gaussian )2 imrow ( result1 )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 34 / 1


Alternatively, we can multiply with <strong>the</strong> Fourier transform of <strong>the</strong> impulseresponse.(We recompute <strong>the</strong> Fourier transform because <strong>the</strong> fft2 function wants<strong>the</strong> impulse response centered on (0,0), while <strong>the</strong> convolve functionwants it centerd on (w//2,h//2). Fur<strong>the</strong>rmore, fft2 requires an impulseresponse padded with zeros <strong>to</strong> <strong>the</strong> size of <strong>the</strong> image.)Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 35 / 1


1 gaussian0 = filters . gaussian_filter ( impulse (* image . shape ) ,8.0)2 gaussian = roll2 ( gaussian0 . copy () ,w//2 ,h //2)3 imrow ( gaussian0 , gaussian )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 36 / 1


1 result2 = real ( ifft2 ( fft2 ( image )* fft2 ( gaussian )))2 imrow ( result2 )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 37 / 1


These two are <strong>the</strong> same.Since we’re going <strong>to</strong> look at more spectra (magnitudes of Fouriertransforms), let’s define a function that makes this more convenient. Thisfunction also shifts <strong>the</strong> low frequencies <strong>to</strong> <strong>the</strong> center of <strong>the</strong> image, where<strong>the</strong>y are easier <strong>to</strong> see.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 38 / 1


1 def spectrum ( image , mode = lambda x:x **0.2 , eps =1e -6) :2 w,h = image . shape3 return roll2 ( mode ( maximum (eps , abs ( fft2 ( image )))),w//2 ,h //2)Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 39 / 1


1 imrow ( image , spectrum ( image ))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 40 / 1


So, <strong>the</strong> spectrum of <strong>the</strong> original image has a lot of high frequencycomponents. These are organized in “spikes”, and <strong>the</strong>se are actuallyperpendicular <strong>to</strong> <strong>the</strong> largest edges found in <strong>the</strong> image.Let’s look at <strong>the</strong> filtered outputs.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 41 / 1


1 imrow ( result1 , spectrum ( result1 ))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 42 / 1


The above filter was implemented by <strong>the</strong> convolve function. It got rid ofmost of <strong>the</strong> high frequency components, although <strong>the</strong>re are smallishstreaks still visible.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 43 / 1


1 imrow ( result2 , spectrum ( result2 ))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 44 / 1


That filter was implemented by Fourier transform. The image looks <strong>the</strong>same, but it really got rid of all high frequency components. Thisdifference is actually very slight and probably related <strong>to</strong> <strong>the</strong> tructation in<strong>the</strong> convolve function. It is only visible because we are using γ = 0.2 fordisplay.Now, we multiply <strong>the</strong> image with <strong>the</strong> Fourier transform of <strong>the</strong> Gaussian.What does that look like?Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 45 / 1


1 imrow ( roll2 ( gaussian ,w//2 ,h //2) , spectrum ( gaussian ))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 46 / 1


The smooth falloff of <strong>the</strong> Gaussian is really important. Let’s construct alowpass filter of exactly <strong>the</strong> same size as <strong>the</strong> Gaussian and compare <strong>the</strong>two.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 47 / 1


1 f = fft2 ( gaussian )2 mag = abs (f)3 lowpass = where (mag >0.5 , f/mag ,0.0)4 imrow ( roll2 ( abs (f),w//2 ,h //2) ,roll2 ( abs ( lowpass ),w//2 ,h //2) )Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 48 / 1


1 imrow ( real ( ifft2 ( fft2 ( image )*f)),real ( ifft2 ( fft2 ( image )* lowpass )))Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 49 / 1


As we can see above, <strong>the</strong> Gaussian-filtered image looks smoothly blurred,while <strong>the</strong> lowpass filtered image has roughly <strong>the</strong> same amount of blur, butlots of additional “structure” due <strong>to</strong> ringing.Thomas Breuel (UniKL) <strong>Introduction</strong> <strong>to</strong> <strong>the</strong> <strong>Frequency</strong> <strong>Domain</strong> 50 / 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!