Chapter6 - Fourier Transform
Chapter6 - Fourier Transform
Fourier Transform
6.1 Introduction
In the previous chapters, we focused on images in spatial domain,
i.e., the physical world. In this chapter, we will learn about the fre-
quency domain. The process of converting an image from spatial do-
main to frequency domain provides valuable insight into the nature of
the image. In some cases, an operation can be performed more effi-
ciently in the frequency domain than in spatial domain. We introduce
the various aspects of Fourier transform and its properties. We focus
exclusively on filtering an image in the frequency domain. Interested
readers can refer to [7],[97],[102] etc. for more in-depth treatment of
Fourier transformation.
The French mathematician Jean Joseph Fourier developed Fourier
transforms in an attempt to solve the heat equation. During the pro-
cess, he recognized that a periodic function can be expressed as infinite
sums of sines and cosines of different frequencies, now known as the
Fourier series. Fourier transform is an extension of the Fourier series to
non-periodic functions. Fourier transform is a representation in which
any function can be expressed as the integral of sines and cosines mul-
tiplied with the weighted function. Also, any function represented in
either Fourier series or transform can be reconstructed completely by
an inverse process. This is known as inverse Fourier transform.
This result was published in 1822 in the book ”La Theorie Anali-
tique de la Chaleur.” This idea was not welcomed, as at that time math-
109
110 Image Processing and Acquisition using Python
• Image filtering
• Image compression
• Image enhancement
• Image restoration
• Image analysis
• Image reconstruction
L−1
1 X −i2πux
F (u) = f (x)e L (6.3)
L
x=0
L−1
i2πux
X
f (x) = F (u)e L (6.4)
x=0
L−1
1 X −2uxπ −i2uxπ
F (u) = f (x) cos − i sin (6.5)
L L L
x=0
Now, using the fact that cos is an even function, i.e., cos(−π) =
cos(π) and that sin is an odd function, i.e., sin(−π) = − sin(π), Equa-
tion 6.5 can be simplified to:
L−1
1 X 2uxπ 2uxπ
F (u) = f (x) cos + i sin (6.6)
L L L
x=0
F (u) has two parts; the real part constituting cos is represented as
R(u) and the imaginary part constituting sin is represented as I(u).
Each term of F is known as the coefficient of the Fourier transform.
Since u plays a key role in determining the frequency of the coefficients
of the Fourier transform, u is known as the frequency variable, while x
is known as the spatial variable.
Traditionally many experts have compared the Fourier transform to
a glass prism. As a glass prism splits or separates the light into various
wavelengths or frequencies that form a spectrum, Fourier transform
112 Image Processing and Acquisition using Python
3
1X −2πx −i2πx
F (1) = f (x) cos − i sin
4 4 4
x=0
1 0 0 2π 2π
= f (0) cos + i sin + f (1) cos + i sin
4 4 4 4 4
!
4π 4π 6π 6π
+ f (2) cos + i sin + f (3) cos + i sin
4 4 4 4
1
= (2(1 + 0i) + 3(0 + 1i) + 2(−1 + 0i) + 1(0 − 1i))
4
2i i
= =
4 2
Note that F (1) is purely imaginary. For u = 2, the value of F (2) = 0
−i
and for u = 3, the value of F (3) = 2 . The four coefficients of the
Fourier transform are 2, 2i , 0, −i
2 .
L−1 K−1
1 XX vy
f (x, y)e−i2π( L + K )
ux
F (u, v) = (6.11)
LK
x=0 y=0
114 Image Processing and Acquisition using Python
L−1
X K−1 vy
F (u, v)ei2π( L + K )
ux
X
f (x, y) = (6.12)
u=0 v=0
p
|F (u, v)| = R2 (u, v) + I 2 (u, v) (6.13)
where R(u, v) and I(u, v) are the real and imaginary parts of the 2D
DFT.
The properties of a 2D Fourier transform are:
The following is the Python function for the forward Fast Fourier
transform:
Necessary arguments:
a is the input image as an ndarray
Optional arguments:
s is a tuple of integers that represents the
length of each transformed axis of the output.
The individual elements in s, correspond to
the length of each axis in the input image.
If the length on any axis is less than the
corresponding size in the input image, then
the input image along that axis is cropped. If the
length on any axis is greater than the corresponding
size in the input image, then the input image along
that axis is padded with 0s.
The Python code for the forward fast Fourier transform is given
below.
saved as a raw file since the pixel intensities are floating values. ImageJ
is used to obtain the logarithm of the raw image and the window level
is adjusted to display the corresponding image. Finally, a spanshot of
this image is shown in Figure 6.1(b). As discussed previously, the cen-
tral pixel is the pixel with the highest intensity. This is due to the fact
that the average of all pixel value in the original image consitutes the
central pixel. The central pixel is (0, 0), the origin. To the left (0,0) is
−u and to the right is +u. Similarly, to the top of (0,0) is +v and to
the bottom is −v. The lower frequency is close to the central pixel and
the higher frequency is away from the central pixel.
Necessary arguments:
a is a complex ndarray comprising of Fourier
transformed data.
118 Image Processing and Acquisition using Python
Optional arguments:
6.4 Convolution
Convolution was briefly discussed in Chapter 4, Spatial Filters,
without any mathematical underpinning. In this section, we discuss
the mathematical aspects of convolution.
Convolution is a mathematical operation that expresses the integral
of the overlap between two functions. A simple example is a blurred
image, which is obtained by convolving an un-blurred image with a
blurring function.
There are many cases of blurred images that we see in real life. A
photograph of a car moving at high speed is blurred due to motion.
Fourier Transform 119
where f , g are the two functions and the * (asterisk) represents convo-
lution.
The convolution satisfies the following properties:
1. f ∗ g = g ∗ f Commutative Property
2. f ∗ (g ∗ h) = (f ∗ g) ∗ h Assocoative Property
3. f ∗ (g + h) = f ∗ g + f ∗ h Distributive Property
H = F.G (6.18)
120 Image Processing and Acquisition using Python
For a given image, after the convolution function is defined, the ideal
lowpass filter can be performed with element by element multiplication
of the FFT of the image and the convolution function. Then the inverse
FFT is performed on the convolved function to get the output image.
The Python code for the ideal lowpass filter is given below.
import scipy.misc
import numpy, math
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image
The image is read and its Fourier transform is determined using the
fft2 function. The Fourier spectrum is shifted to the center of the image
using the fftshift function. A filter (H) is created by assigning a value of
1 to all pixels within a radius of d 0 and 0 otherwise. Finally, the filter
(H) is convolved with the image (d) to obtain the convolved Fourier
image (con). This image is inverted using ifft2 to obtain the filtered
image in spatial domain. Since high frequencies are blocked, the image
is blurred.
A simple image compression technique can be created using the
Fourier Transform 123
1
H(u, v) = 2 (6.20)
d(u,v)
1+ d0
where d0 is the cut-off distance from the origin for the frequency and
d(u, v) is the Euclidean distance from the origin. In this filter, unlike the
ILPF, the pixel intensity at the cut-off radius does not change rapidly.
The Python code for the Butterworth lowpass filter is given below:
d = fftim.fftshift(c)
# converting H to an image
H = scipy.misc.toimage(H)
# performing the convolution
con = d * H
# computing the magnitude of the inverse FFT
e = abs(fftim.ifft2(con))
# e is converted from an ndarray to an image
Fourier Transform 125
f = scipy.misc.toimage(e)
# f.show()
# saving the image as blowpass_output.png in
# Figures folder
f.save('../Figures/blowpass_output.png')
This program is similar to the Python code used for ILPF except
for the creation of the filter (H).
−d2 (u,v)
2d2
H(u, v) = e 0 (6.21)
d = fftim.fftshift(c)
# converting H to an image
# H = PIL.toimage(H)
H = scipy.misc.toimage(H)
# performing the convolution
con = d * H
# computing the magnitude of the inverse FFT
e = abs(fftim.ifft2(con))
# e is converted from an ndarray to an image
Fourier Transform 127
f = scipy.misc.toimage(e)
# saving the image as glowpass_output.png in
# Figures folder
f.save('../Figures/glowpass_output.png')
Figure 6.1 is the input image to be filtered using ILPF, BLPF and
GLPF. The images in Figures 6.2(a), 6.2(b) and 6.2(c) are the outputs
of ideal lowpass, Butterworth lowpass, and Gaussian lowpass filters
with cut-off radius at 30. Notice how the blurriness varies in the output
images. The ILPF is extremely blurred due to the sharp change in the
ILPF convolution function at the cut-off radius. There are also severe
ringing artifacts, the spaghetti like structure in the background next to
the foreground pixels. In BLPF, the convolution function is continuous
which results in less blurring and ringing artifacts compared to ILPF.
Since a smoothing operator forms the GLPF convolution function, the
output of GLPF is even less blurred when compared to both ILPF and
BLPF.
import scipy.misc
import numpy, math
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image
128 Image Processing and Acquisition using Python
FIGURE 6.2: An example of lowpass filters. The input image and all
the output images are displayed in spatial domain.
1
H(u, v) = 2n (6.23)
d0
1+ d(u,v)
# converting H to an image
H = scipy.misc.toimage(H)
# performing the convolution
con = d * H
# computing the magnitude of the inverse FFT
e = abs(fftim.ifft2(con))
# e is converted from an ndarray to an image
f = scipy.misc.toimage(e)
# saving the image as bhighpass_output.png in
132 Image Processing and Acquisition using Python
# Figures folder
f.save('../Figures/bhighpass_output.png')
−d2 (u,v)
2d2
H(u, v) = 1 − e 0 (6.24)
where d0 the cut-off frequency and d(u, v) the Euclidean distance from
origin.
The Python code for GHPF is given below.
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 2*d_0
# converting H to an image
H = scipy.misc .toimage(H)
# performing the convolution
con = d * H
# computing the magnitude of the inverse FFT
e = abs(fftim.ifft2(con))
# e is converted from an ndarray to an image
f = scipy.misc .toimage(e)
# f.show()
# saving the image as ghighpass_output.png in
# Figures folder
f.save('../Figures/ghighpass_output.png')
Figures 6.3(b), 6.3(c) and 6.3(d) are the outputs of IHPF, BHPF and
GHPF with cut-off radius at 30. Highpass filters are used to determine
edges. Notice how the edges are formed in each case.
FIGURE 6.3: An example of highpass filters. The input image and all
the output images are displayed in spatial domain.
import scipy.misc
import numpy, math
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image
# converting H to an image
H = scipy.misc.toimage(H)
# performing the convolution
con = d * H
# computing the magnitude of the inverse FFT
e = abs(fftim.ifft2(con))
# e is converted from an ndarray to an image
f = scipy.misc.toimage(e)
# f.show()
# saving the image as ibandpass_output.png in
# Figures folder
f.save('../Figures/ibandpass_output.png')
FIGURE 6.4: An example of IBPF. The input and the output are
displayed in spatial domain.
6.6 Summary
• Lowpass filters are used for noise reduction or smoothing. High-
pass filters are used for edge enhancement or sharpening.
6.7 Exercises
1. Fourier transform is one method for converting any function as
a sum of basis functions. Perform research and find at least two
other such methods. Write a report on their use in image process-
ing.
Hint: Wavelet, z-transform