Modifying Images and Sounds using the Mathematics of Fourier Transforms

advertisement
Modifying
x
Images and Sounds using the
Mathematics of Fourier Transforms
Colin Macdonald
www.math.sfu.ca/∼cbm
Mathematics Department
Simon Fraser University
Burnaby, BC
Image processing demo code is based on code by Dr. Dave Muraki
Colin Macdonald
SFU Applied Mathematics
typeset with LATEX and prosper
Scientific I, SFU, 2005-02-18
0/18
Sine functions
f (x) = sin(x)
1
f(x)
0.5
0
−0.5
−1
−1
Colin Macdonald
SFU Applied Mathematics
0
1
x
2
3
4
Math Camp, SFU, 2005-02-18
1/18
Sine functions
f (x) = a sin(k · 2πx)
Increasing the “wavenumber” k:
k=1
1
1
1
0.5
0.5
0.5
0
0
0
−0.5
−0.5
−0.5
−1
0
0.2
0.4
0.6
0.8
1
k=3
−1
0
0.2
0.4
0.6
0.8
1
k=5
−1
0
0.2
0.4
0.6
0.8
1
Changing the amplitude a:
a=1
Colin Macdonald
2
2
2
1.5
1.5
1.5
1
1
1
0.5
0.5
0.5
0
0
0
−0.5
−0.5
−0.5
−1
−1
−1
−1.5
−1.5
−1.5
−2
0
0.2
SFU Applied Mathematics
0.4
0.6
0.8
1
a=2
−2
0
0.2
0.4
0.6
0.8
1
a=
1
2
−2
0
0.2
0.4
0.6
0.8
1
Math Camp, SFU, 2005-02-18
2/18
Decomposing a function
Can express many functions as a sum of various sine functions:
1.5
1
1
1
0.8
0.6
0.5
0.5
0.4
=1
0
+
0
1
2
0.2
0
−0.2
−0.4
−0.5
−0.5
−0.6
−0.8
−1
−1
−1
0
−1.5
0
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1
1
(k = 1, a = 1) and (k = 10, a = 12 ).
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
3/18
Decomposing a function
Another example:
1
1
1
0.5
1
0.5
0.5
+
0
−0.5
=
0
1
3
0.2
0.4
0.6
0.8
0.4
(k
(k
(k
(k
(k
(k
Colin Macdonald
0.6
0.8
1
0.2
0.4
0.6
0.8
−1
0
1
+
0.4
0.6
0.8
1
+
0
−1
0
0.4
0.6
0.8
1
0.5
−0.5
0.2
0.2
1
0.5
1
9
+
0
1
0
−1
0
1
5
−0.5
−1
0
1
1
−0.5
0.2
+
0
0.5
−1
0
0.5
−0.5
−1
0
1
7
−0.5
1
1
11
0
−0.5
0.2
0.4
0.6
0.8
1
−1
0
0.2
0.4
0.6
0.8
1
= 1, a = 1),
= 3, a = 13 ),
= 5, a = 15 ),
= 7, a = 17 ),
= 9, a = 19 ),
1
).
= 11, a = 11
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
4/18
Reconstruction
Give amplitude and frequency information: “(k = 1, a = 1),
(k = 3, a = 13 ), . . . ”
Reconstruction:
1
1
f (x) = (1) sin(1 · 2πx) +
sin(3 · 2πx) +
sin(5 · 2πx) + . . .
3
5
1
(k
(k
(k
(k
(k
(k
= 1, a = 1),
= 3, a = 13 ),
= 5, a = 15 ),
= 7, a = 17 ),
= 9, a = 19 ),
1
)
= 11, a = 11
0.5
=⇒
0
−0.5
−1
0
Colin Macdonald
SFU Applied Mathematics
0.2
0.4
0.6
0.8
1
Math Camp, SFU, 2005-02-18
5/18
Demo – decomposing into sine waves
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
6/18
Decomposition/Reconstruction seems hard. . .
Difficult with three wavenumbers. But 1 second of CD audio has
44000 possible wavenumbers!
Use student labour?
Colin Macdonald
SFU Applied Mathematics
↓
Math Camp, SFU, 2005-02-18
7/18
Decomposition/Reconstruction seems hard. . .
Difficult with three wavenumbers. But 1 second of CD audio has
44000 possible wavenumbers!
Use student labour?
Not reliable enough:
Calculus can provide a solution.
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
7/18
The Fourier transform
Fourier Transform decomposes f (x) into amplitude and frequency
(wavenumber) information.
Transforms f (x) into a(k).
Transforms functions of space x into functions of wavenumber k.
Colin Macdonald
SFU Applied Mathematics
↓
Math Camp, SFU, 2005-02-18
8/18
The Fourier transform
Fourier Transform decomposes f (x) into amplitude and frequency
(wavenumber) information.
Transforms f (x) into a(k).
Transforms functions of space x into functions of wavenumber k.
Warning!
x
more equations:
1
a(k) = √
2π
Z
∞
f (x)e−i2πkx dx
−∞
discrete case:
N −1
1 X
fn e−i2πkn/N
ak =
N n=0
Understanding
x
is more important than equations!
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
8/18
The inverse Fourier transform
Inverse Fourier Transform recovers f (x) from amplitude and
frequency (wavenumber) information.
Transforms a(k) into f (x).
Transforms functions of wavenumber k into functions of space x.
Colin Macdonald
SFU Applied Mathematics
↓
Math Camp, SFU, 2005-02-18
9/18
The inverse Fourier transform
Inverse Fourier Transform recovers f (x) from amplitude and
frequency (wavenumber) information.
Transforms a(k) into f (x).
Transforms functions of wavenumber k into functions of space x.
The gory details:
1
f (x) = √
2π
Z
∞
a(k)ei2πkx dk
−∞
discrete case:
fn =
N
−1
X
ak ei2πkn/N
n=0
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
9/18
Fourier transform summary
10
f(x)
5
0
−5
Fourier
Transform
“Decompose”
−10
0
⇓
0.2
0.4
x
0.6
0.8
1
⇑
2
Inverse
Fourier
Transform
“Reconstruct”
a(k)
1
0
−1
−2
0
10
20
k
30
40
“. . . as if functions circulated at ground level and their transforms
in the underworld” [Bracewell, 1965]
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
10/18
Audio signals and Fourier transforms
Sound is pressure against your ears changing in time:
pressure
10
5
0
−5
−10
0
0.2
0.4
0.6
0.8
1
time
⇓
amplitude
Fourier
Transform
⇑
2
Inverse
Fourier
Transform
1
0
−1
−2
0
10
20
30
40
frequency
Time domain ⇐⇒ frequency domain.
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
11/18
Fourier transforms for music
Visualization:
“Bass Boost”:
music → F.T. → Increase “small k” freq. → Inv. F.T. → music
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
12/18
Fourier transform for images
Images are 2-D
monocolor image of skylab.jpg
fourier coefficents for skylab.jpg (magnitude)
1
−100
20
−80
40
0.8
60
−40
0.6
100
120
0.4
140
6
−20
l−axis
y−axis
80
4
0
20
2
40
160
0.2
180
200
8
−60
0
60
80
50
100
x−axis
150
200
0
−100
−2
−50
0
k−axis
50
Transform f (x, y) into a(k, l). Horizontal wavenumber k and
vertical wavenumber l.
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
13/18
Fourier transform for images
partial image for skylab.jpg
−100
−80
−60
−40
Broad features:
y
−20
0
20
40
60
80
−100
−50
0
x
50
partial image for skylab.jpg
−100
−80
−60
−40
Detail:
y
−20
0
20
40
60
80
−100
Colin Macdonald
SFU Applied Mathematics
−50
0
x
50
Math Camp, SFU, 2005-02-18
14/18
Image compression
partial image for skylab.jpg
partial image for skylab.jpg
−100
−100
100% Fourier
coefficients
−60
−40
y−axis
−20
−80
−60
0
−40
20
40
−20
20
80
−100
80
−50
0
x−axis
−50
0
masked k−axis
50
50
−20
0
20
40
80
−100
80
−50
0
x−axis
−50
0
masked k−axis
50
50
partial image for skylab.jpg
−100
10% Fourier
coefficients
−60
−40
−60
−40
y−axis
fourier coefficents for masked skylab.jpg (magnitude)
−100
−20
−80
−60
0
masked l−axis
−40
20
40
−20
20
80
−100
80
−50
0
x−axis
50
SFU Applied Mathematics
−80
−60
0
−40
40
40
60
fourier coefficents for masked skylab.jpg (magnitude)
−100
−20
20
0
60
1% Fourier
coefficients
−80
masked l−axis
−80
y−axis
−40
60
partial image for skylab.jpg
Colin Macdonald
−60
60
−100
−100
−100
−80
0
40
40
60
fourier coefficents for masked skylab.jpg (magnitude)
−100
−20
20
0
60
−100
−60
fourier coefficents for masked skylab.jpg (magnitude)
−100
masked l−axis
y−axis
−40
33% Fourier
coefficients
−80
masked l−axis
−80
−50
0
masked k−axis
50
−20
0
20
40
60
60
80
−100
−100
80
−50
0
x−axis
−50
0
masked k−axis
50
50
Math Camp, SFU, 2005-02-18
15/18
Demo
Colin Macdonald
SFU Applied Mathematics
Math Camp, SFU, 2005-02-18
16/18
Exercises
Can you draw a Fourier coefficient mask to produce the following?
partial image for triangle.jpeg
partial image for triangle.jpeg
−100
−100
−80
−80
−80
−60
−60
−60
−40
−40
−40
−20
−20
−20
0
y
0
y
y
triangle.jpeg
−100
0
20
20
20
40
40
40
60
60
60
80
80
80
−100
−50
0
x
50
100
−100
−50
0
x
50
100
−100
−50
0
x
50
100
partial image for triangle.jpeg
−100
−80
−60
−40
y
−20
0
20
40
60
80
−100
Colin Macdonald
−50
SFU Applied Mathematics
0
x
50
100
Math Camp, SFU, 2005-02-18
17/18
Solutions to exercises
partial image for triangle.jpeg
partial image for triangle.jpeg
−100
−80
−80
−80
−60
−60
−60
−40
−40
−40
−20
−20
−20
0
0
0
y
−100
y
y
partial image for triangle.jpeg
−100
20
20
20
40
40
40
60
60
60
80
80
80
−100
Colin Macdonald
−50
0
x
50
SFU Applied Mathematics
100
−100
−50
0
x
50
100
−100
−50
0
x
50
100
Math Camp, SFU, 2005-02-18
18/18
Download