matthew thurley industrial image analysis – e0005e/file/e0005e_lecture06... · matthew thurley...

36
1 Frequency Domain Filtering Matthew Thurley Industrial Image Analysis – E0005E based on material by Johan Carlson

Upload: lehanh

Post on 11-Mar-2018

234 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

1

Frequency Domain Filtering

Matthew Thurley

Industrial Image Analysis – E0005Ebased on material by Johan Carlson

Page 2: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

——————————————————–

1-1

Page 3: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

2

This Lecture

The concept of frequency – Fourier transformsRepetition of 1D convolution, transforms and filteringGeneralization to the 2D world

2D convolution

Frequency domain filtering

Examples of frequency selective filters

Page 4: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

3

The concept of frequency

Let’s start off in the one-dimensional world...

The period of cos(t) is 2π seconds, i.e. The signal repeats itself after2π seconds.

The period of cos(2π · t) is one second.

The period is usually denoted T .

Frequency is measured in Hz (Hertz) and is the number of periods(or cycles) per second, denoted f . That is,

f =1

T.

The function cos(2π · ft) has a frequency of f and a period of 1/f

Angular frequency is measured in rad/s, denoted ω, so

ω =2π

T.

Page 5: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

4

The concept of frequency (cont’d...)

−10 0 10 20−5

0

5

Time t (ms)

x 1(t)

Cosine, f = 200 Hz

−10 0 10 20−5

0

5

Time t (ms)

x 2(t)

Cosine, f = 100 Hz

−10 0 10 20−5

0

5

Time t (ms)

x 3(t)

Cosine, f = 10 Hz

−10 0 10 20−5

0

5

Time t (ms)

x 3(t)

Cosine, f = 0 Hz

Page 6: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

5

The concept of frequency (cont’d...)

Some comments

Frequency is measured in hertz, cycles or periods per second

Higher frequency means more periods per second.

Higher frequency means shorter period.

Zero frequency implies infinite period, which means a constantsignal.

Fourier transform of a function of time has units, cycles per second

Fourier transform of a function of pixels (like an image) will haveunits, cycles per pixel

Rapid changes in a signal correspond to high frequencies, slowchanges are represented by low frequencies

Rapid changes in intensity in an image are high frequencies.

Page 7: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

6

Background - Complex Numbers

Recall that a complex number contains a real and an imaginarycomponent.

C = R+ jI

And a complex number can be represented in polar coordinates (r, θ),(magnitude, angle), and using these polar coordinates

C = |C|(cosθ + jsinθ)

Using Eulers formula

ejθ = cosθ + jsinθ

And if we use ω = 2π/T angular frequency as our angle

ej2π

T = cos2π

T+ jsin

T

Eulers formula is a sum of cosins and sines that Fourier used to expressany periodic function.

Page 8: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

7

Sum of cosines and sines

Using a sum of cosines and sines to express a periodic function

Page 9: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

8

The 1D Fourier transforms

The Fourier theory states that any periodic function can be expressed asa sum of cosines and sines of different frequencies with each multiplied bya different coefficient. Which means it can be expressed in terms ofcomplex exponents (eulers formula) with varying frequency.A time signal or time series can be representedthis way also.

The continuous-time Fourier transform

X(ω) =

∫ ∞

−∞

x(t)e−jωtdt,

where ω = 2πf , where f (or µ in the textbook) denotes frequency in Hz.The corresponding inverse transform is

x(t) =1

∫ ∞

−∞

X(ω)ejωtdω.

Page 10: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

9

The 1D Fourier transforms (cont’d...)

Example

Consider the following two continuous-time signals:

x1(t) = cos(2πt) + 0.3 cos(16πt)

x2(t) = e−8(t−3)2 cos(8πt)

What are the frequency components in x1(t)?

Page 11: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

10

The 1D Fourier transforms (cont’d...)

...and plotting the signals and the transforms we get

0 2 4 6

−1

0

1

time, t (s)

x 1(t)

−20 −10 0 10 200

0.2

0.4

0.6

frequency, f (Hz)

|X1(f

)|0 2 4 6

−0.50

0.51

time, t (s)

x 2(t)

−10 −5 0 5 100

1

2x 10

−3

frequency, f (Hz)

|X2(f

)|

Page 12: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

11

The 1D Fourier transforms (cont’d...)

There is also a discrete Fourier transform, working on sequences ofnumbers, x[n] where n = 0, 2, . . . , N − 1. This is defined as

X [k] =N−1∑

n=0

x[n]e−j(2πk/N)n,

where we get the discrete frequencies k = 0, 1, . . . , N − 1.

The corresponding inverse transform is defined as

x[n] =1

N

N−1∑

k=1

X [k]ej(2πk/N)n

These can be computed in MATLAB using the fft and ifft commands,respectively.

Page 13: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

12

The 1D Fourier transforms (cont’d...)

Example - same as before

0 20 40 60 80

−1

0

1

n

x 1[n]

7000 8000 90000

0.2

0.4

0.6

k

|X1[k

]|200 250 300 350

−0.50

0.51

time, t (s)

x 2(t)

6500 7000 7500 8000 8500 9000 95000

1

2x 10

−3

k|X

2([k]

|

Note that the bottom right spectrum is also discrete, but plotted here as asolid line just for clarity.

Page 14: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

13

Background - Correlation and Convolution

DIP Textbook 3.4.2 Spatial Correlation and Convolution

Correlation is the processing of filtering a mask over an image,exactly as explained in linear spatial filtering or neighborhoodoperations in lecture 2.Correlation operator ⋄, of a filter w(x, y) of size m× n with an imagef(x, y)

w(x, y) ⋄ f(x, y) =∑a

s=−a

∑bt=−bw(s, t)f(x+ s, y + s)

Convolution is the same except the mask is reflected about both axes(exactly as the transpose in binary morphological image processing.Also described as the mask being rotated by 180 degrees)Convolution operator ∗, of a filter w(x, y) of size m× n with an imagef(x, y)

w(x, y) ∗ f(x, y) =∑a

s=−a

∑bt=−bw(s, t)f(x− s, y − s)

Page 15: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

14

Background - Correlation and Convolution

Page 16: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

15

Background - Correlation and Convolution

Page 17: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

16

Filtering in the time-domain

If we have a system which is linear and time-invariant (LTI)

x(t)T fx(t)g

y(t) = T fx(t)g

the output can be calculated using the convolution integral

y(t) = x(t) ∗ h(t) =

∫ ∞

−∞

x(τ)h(t− τ)dτ

or equivalently in the discrete-time case, by the convolution sum

y[n] = x[n] ∗ h[n] =

∞∑

m=−∞

x[m]h[m− n].

The convolution sum can be calculated in MATLAB using the convcommand.

Page 18: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

17

Filtering in the frequency domain

We also know that for LTI systems, the Fourier transform is very powerful,since the convolution in the time domain can be replaced with amultiplication in the frequency domain , i.e.

y(t) = x(t) ∗ h(t)F←→ Y (ω) = X(ω) ·H(ω)

And correspondingly in the discrete domain (in principle)

y[n] = x[n] ∗ h[n]F←→ Y [k] = X [k]H[k]

Explore this in the development phase this week.There are some details we need to look into as well...

Page 19: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

18

Length of filtered signals

When we filter a signal x[n], n = 0, 1, . . . , N − 1 with a filter h[n],n = 0, 1, . . . ,M − 1, the output signal y[n] will go fromn = 0, 1, . . . , (N − 1) + (M − 1)− 1.

In MATLAB notation: length(y)=length(x)+length(h)-1.

The output signal is longer than the input!

So, if we are doing the filtering in the frequency domain, using theDFT (fft in MATLAB), we need to make the frequency vectorslonger as well.

This is accomplished by zero-padding the transform.

The filtering is done by typing:

y = real(ifft(fft(x,M+N-1).*fft(h,M+N-1)))

Page 20: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

19

Frequency selective filtering

So, by the frequency domain interpretation of filtering (convolution) wesee that we can easily design frequency selective filters.

Recall the previous signal example

0 2 4 6

−1

0

1

time, t (s)

x 1(t)

−50 0 500

0.5

1

frequency, f (Hz)

|X1(f

)|We realize now that we could remove the high-frequency component bymultiplying the frequency-domain with a window that cuts away theundesired frequencies, i.e. a filter which spectrum is zero for frequenciesabove some threshold.

Page 21: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

20

Frequency selective filtering

...the result

0 2 4 6

−1

0

1

time, t (s)

x 1(t)

0 2 4 6

−1

0

1

time, t (s)

y 1(t)

This is an example of a low-pass filter, since low frequencies pass throughundisturbed while high frequencies are cancelled out.

Page 22: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

21

Now, let’s go 2D!

In two dimensions, working with discrete signals (digital images), theconvolution theorem is generalized as follows:

y[m,n] =

M−1∑

k=0

N−1∑

l=0

x[k, l]h[m− k, n− l],

where x[m,n] is the original image and h[m,n] is the two-dimensionalfilter, sometimes called filter mask.

In MATLAB this is done by conv2.

Page 23: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

22

The 2D discrete Fourier transform

The extension of the Fourier transform theory to the two-dimensional caseis straightforward. Since we’re working with digital images, let’s focus onlyon the discrete transform.

The 2D discrete Fourier transform is defined as:

X [u, v] =M−1∑

m=0

N−1∑

n=0

x[m,n]e−j2π(um/M+vn/N)

And the corresponding inverse transform

x[m,n] =1

MN

M−1∑

u=0

N−1∑

v=0

X [u, v]ej2π(um/M+vn/N)

These are available in MATLAB as fft2 and ifft2.

Page 24: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

23

2D filtering in the frequency domain

Details...

Just as in the 1D case, the output image of the filter will be larger,due to the size of the filter mask.

Using conv2, i.e. filtering in the spatial domain, the results can be cutautomatically using the ’same’ option to conv2 (see MATLAB help fordetails).

Doing the filtering in the frequency domain, the transform has to bezero-padded to the proper size.

After the filtering you can then cut the image to the original size.

As in the 1D case, the 2D fourier transform and its inverse areinfinitely periodic (in both dimensions), ie. they wrap around.Consider this when you observe effects at the edge of your imagewhen converting back to spatial coordinates with the inverse fouriertransform.

You will practice these tasks in this weeks development phase.

Page 25: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

24

2D filtering in the frequency domain

As the 2D discrete fourier transform (DFT) is complex, it can beexpressed in polar coordinates with a magnitude, and an anglularfrequency (also known as the phase ).

The components of the spectrum determine the amplitudes of thesinusoids that combine to form the resulting image. At any givenfrequency in the DFT image, a large amplitude implies a greaterprominance of a sinusoid of that frequency in the image.

The phase is a measure of the displacement of the various sinusoidswith respect to their origin.

While the magnitude of the 2D DFT gives the amplitudes of thesinusoids and determines the intensities in the image, the phasespecifies the angles (displacements) of the sinusoids and whereobjects are in the image.

Investigate this in the development phase this week.

Page 26: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

25

The concept of frequency, revisited

When going to 2D in general, and digital images in particular, we need tothink about what we mean with frequencies.

For images, the transformed quantity X [u, v] is a function of twospatial frequency variables.

We no longer talk about Hertz or radians/sec, but simply spatialfrequencies.

Low frequencies still represent slow variations in the image, and highfrequencies represent rapid changes.

A smooth image, e.g. a photograph often contains large areas ofslowly varying intensity. These images have a dominant part of theirenergy concentrated to a few low-frequency components in theFourier-domain.

Edges in images imply rapid changes in the intensity, and willtherefore be represented by the high-frequency components of theimage’s spectrum.

Page 27: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

26

2D filtering

Example

Consider the following filter mask:

h[m,n] =

{

1/25, m, n = 0, 1, . . . , 4

0, otherwise,

i.e. a 5× 5 moving average filter (taking the average of 25 pixels).

Let H[u, v] be the 2D discrete Fourier transform of the filter mask.

Also, consider an inverse filter, G[u, v] = 1/(H[u, v] + µ). (Where µ is asmall value to prevent division by zero)

Page 28: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

27

2D filtering

Example (cont’d...)

Looking at the 1D frequency response of the uniform averaging filter weget the following

Page 29: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

28

2D filtering

Example (cont’d...)

Looking at the frequency responses of the two filters we get the following

u

v

|H(u,v)|

−0.5 0 0.5

−0.5

0

0.5

0.2

0.4

0.6

0.8

1

u

v

|G(u,v)|

−0.5 0 0.5

−0.5

0

0.5

1

2

3

4

Consider, which frequencies respond strongly in each filter? Whichfrequencies are ”passed” (not removed) by each filter?

Page 30: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

29

2D filtering

Example (cont’d...)

Some comments:

The first filter seems to be a low-pass filter, since low frequencies areallowed to pass through, but higher frequencies are damped.

The second filter is the inverse of the low-pass filter, so it should be ahigh-pass filter.

The low-pass filter should cause edges and other rapid changes inthe image to be blurred, while smooth surfaces should be left more orless unaffected.

The second filter should keep edges but smooth surfaces should beattenuated (reduced in intensity / magnitude).

Let’s look at the result...

Page 31: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

30

2D filtering

Example (cont’d...)

original grayscale low−pass filtered high−pass filtered

Page 32: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

31

2D filtering

Some general comments

Being able to analyze the spectral content of an image provides toolsto construct filters.

For example, if a distortion of an image can be modeled as a linearfilter, then the inverse filter should be able to compensate for thedistortion. One example could be motion blurring, caused by themotion of the object or the camera.

In the presence of noise, inverse filtering becomes difficult. This isbecause the filter may contain zeros in the spectrum. If there is noiseat those frequencies, the inverse filter will blow up the noise,drowning all other interesting information. Hence the µ (smallconstant) in the inverse example.

You will experiment with this in this weeks development phase.

Page 33: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

32

Summary

Repetition of convolution and Fourier transforms in the 1D case.

Generalization to the 2D world.

Examples of filtering.

Chapter 4 covers frequency domain filtering in detail

Chapter 5 covers image restoration and topics involving removingblur and periodic noise from an image.

Page 34: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

33

Tips for Development Phase 6

Be aware of what the range of intensities (values) are in your images.

Be careful with image rescaling as it can hide the range of intensitiesfrom you and makes small magnitures look as significant as largemagnitudes.

You could plot them in 3D with the mesh command.

Extra reading4.6.3 Periodicity4.6.5 Fourier Spectrum and Phase Angle4.7 The Basics of Filtering in the Frequency Domain4.7.2 Frequency domain filtering fundamentals4.7.3 Summary of steps for filtering in the frequency domain4.7.4 Correspondance between filtering in the spatial andfrequency domain

Page 35: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

34

Fourier Descriptors for Dev Phase 8

Consider we have the perimeter of a simple convex object and wewant to characterise the shape of the object.

Consider a centre point within that object, and then consider that werepresent the perimeter of the object in polar coordinates (r, θ)

If we plot a graph of r,theta we will have a periodic function

We could apply a 1D discrete fourier transform to this plot to identifythe major frequencies that characterise the shape of the object.

More on this in Development Phase 8, and in DIP 11.2.3

Page 36: Matthew Thurley Industrial Image Analysis – E0005E/file/E0005E_Lecture06... · Matthew Thurley Industrial Image Analysis – E0005E ... These can be computed in MATLAB using the

35

Summing Up

Consider the following three questions;What do i need to work on?What have I learnt today?What was the main point left unanswered?

Write your answers in the journal with the lecture number at the topof the page.