0% found this document useful (0 votes)
4 views

Chapter6 - Fourier Transform

Chapter 6 introduces the Fourier Transform, a mathematical tool that converts images from the spatial domain to the frequency domain, enhancing image processing capabilities such as filtering and compression. It covers both continuous and discrete Fourier transforms, including their definitions, properties, and applications in image processing. The chapter also provides Python code for implementing the Fast Fourier Transform (FFT) and its inverse, illustrating the process with an example of a 2D FFT on an image.

Uploaded by

Trangg Nguyễnn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter6 - Fourier Transform

Chapter 6 introduces the Fourier Transform, a mathematical tool that converts images from the spatial domain to the frequency domain, enhancing image processing capabilities such as filtering and compression. It covers both continuous and discrete Fourier transforms, including their definitions, properties, and applications in image processing. The chapter also provides Python code for implementing the Fast Fourier Transform (FFT) and its inverse, illustrating the process with an example of a 2D FFT on an image.

Uploaded by

Trangg Nguyễnn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 6

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

ematicians were interested in and studied regular functions. It took over


a century to recognize the importance and power of Fourier series and
transforms. After the development of the fast Fourier transform algo-
rithm, FFT, the applications of Fourier transforms have affected several
fields, remote sensing, signal processing and image processing.
In image processing, Fourier transforms are used for:

• Image filtering

• Image compression

• Image enhancement

• Image restoration

• Image analysis

• Image reconstruction

In this chapter we discuss image filtering and enhancement in detail.

6.2 Definition of Fourier Transform


A Fourier transform of a continuous function in one variable f (x)
is given by the following equation:
Z ∞
F (u) = f (x)e−i2πux dx (6.1)
−∞

where i = −1. The function f (x) can be retrieved by finding the
inverse Fourier transform of F (u) which is given by the following equa-
tion:
Z ∞
f (x) = F (u)ei2πux du. (6.2)
−∞
Fourier Transform 111

The Fourier transform of a one variable discrete function, f (x) for


x = 0, 1, ...L − 1 is given by the following equation:

L−1
1 X −i2πux
F (u) = f (x)e L (6.3)
L
x=0

for u = 0, 1, 2, ..., L − 1. Equation 6.3 is known as the discrete


Fourier transform, DFT. Likewise, the inverse discrete Fourier trans-
form, (IDFT) is given by the following equation:

L−1
i2πux
X
f (x) = F (u)e L (6.4)
x=0

for x = 0, 1, 2, ..., L − 1. Using the Euler’s formula eiθ = cos θ + i sin θ,


the above equation simplifies to

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

splits or separates a function into its coefficients which depend on the


frequency. These Fourier coefficients form a Fourier spectrum in the
frequency domain.
From Equation 6.6, we know that the Fourier transform is comprised
of complex numbers. For computational purposes, it is convenient to
represent the Fourier tranform in polar form as:

F (u) = |F (u)|e−iθ(u) (6.7)


p
where |F (u)| = R2 (u) + I 2 (u)
h is called
i the magnitude of the Fourier
−1 I(u)
transform and θ(u) = tan R(u) is called the phase angle of the
transform. Power, P (u), is defined as the following:

P (u) = R2 (u) + I 2 (u) = |F (u)|2 . (6.8)

The first value in the discrete Fourier transform is obtained by set-


ting u = 0 in equation (6.3) and then summing the product over all x.
Hence, F (0) is nothing but the average of f (x) since e0 = 1. F (0) has
the real part while the imaginary part is zero. Other values of F can
be computed in a similar manner.
Let us consider a simple example to illustrate the Fourier transform.
Let f (x) be a discrete function with only four values: f (0) = 2, f (1) =
3, f (2) = 2 and f (3) = 1. Note that the size of f is 4, hence L = 4.
3
1X f (0) + f (1) + f (2) + f (3)
F (0) = f (x) = =2
4 4
x=0
Fourier Transform 113

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 .

6.3 Two-Dimensional Fourier Transform


The Fourier transform for two variables is given by the following
equation:
Z ∞ Z ∞
F (u, v) = f (x, y) e−i2π(ux+vy) dx dy (6.9)
−∞ −∞

and the inverse Fourier transform is


Z ∞ Z ∞
f (x, y) = F (u, v)ei2π(ux+vy) du dv. (6.10)
−∞ −∞

The discrete Fourier transform of a 2D function, f (x, y) with size


L and K is given by the following equation:

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

for u = 1, 2, ..., L − 1 and v = 1, 2, ..., K − 1. Similar to 1D Fourier


transform, f (x, y) can be computed from F (u, v) by computing the
inverse Fourier transform, given by the following equation:

L−1
X K−1 vy
F (u, v)ei2π( L + K )
ux
X
f (x, y) = (6.12)
u=0 v=0

for x = 1, 2, ..., L−1 and y = 1, 2, ..., K −1. As in the case of 1D DFT, u


and v are the frequency variables and x and y are the spatial variables.
The magnitude of the Fourier transform in 2D is given by the following
equation:

p
|F (u, v)| = R2 (u, v) + I 2 (u, v) (6.13)

and the phase angle is given by


 
−1 I(u, v)
θ(u, v) = tan (6.14)
R(u, v)
and the power is given by

P (u, v) = R2 (u, v) + I 2 (u, v) = |F (u, v)|2 . (6.15)

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:

1. The 2D space with x and y as variables is referred to as spatial


domain and the space with u and v as variables is referred to as
frequency domain.

2. F (0, 0) is the average of all pixel values in the image. It can be


obtained by substituting u = 0 and v = 0 in the equation above.
Hence F (0, 0) is the brightest pixel in the Fourier transform im-
age.

3. The two summations are separable. Thus, summation is per-


Fourier Transform 115

formed along the x or y-directions first and in the other direction


later.

4. The complexity of DFT is N 2 . Hence a modified method called


Fast Fourier Transform (FFT) is used to calculate the Fourier
transform. Cooley and Tukey developed the FFT algorithm [14].
FFT has a complexity of N logN and hence the word ”Fast” in
its name.

6.3.1 Fast Fourier Transform using Python

The following is the Python function for the forward Fast Fourier
transform:

numpy.fft.fft2(a, s=None, axes=(-2,-1))

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.

axes is an integer used to compute the FFT. If axis


is not specified, the last two axes are used.
116 Image Processing and Acquisition using Python

Returns: output is a complex ndarray.

The Python code for the forward fast Fourier transform is given
below.

import math, numpy


import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/fft1.png').convert('L')
# a is converted to an ndarray
b = numpy.asarray(a)
# performing FFT
c = abs(fftim.fft2(b))
# shifting the Fourier frequency image
d = fftim.fftshift(c)
# converting the d to floating type and saving it
# as fft1_output.raw in Figures folder
d.astype('float').
tofile('../Figures/fft1_output.raw')

In the above code, the image data is converted to a numpy array


by using the asarray() function. This is similar to the fromimage()
function. The Fast Fourier transform is obtained using the fft2 function
and only the absolute value is obtained for visualization. The absolute
value image of FFT is then shifted, so that the center of the image is
the center of the Fourier spectrum. The center pixel corresponds to a
frequency of 0 in both directions. Finally, the shifted image is saved as
a raw file.
The image in Figure 6.1(a) is a slice of Sindbis virus from a trans-
mission electron microscope. The output after performing the FFT is
Fourier Transform 117

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.

(a) Input for FFT. (b) Output of FFT.

FIGURE 6.1: An example of 2D Fast Fourier transform. Original im-


age reprinted with permission from Dr. Wei Zhang, University of Min-
nesota.

The Python function for inverse Fast Fourier transform is given


below.

numpy.fft.ifft2(a, s=None, axes=(-2,-1))

Necessary arguments:
a is a complex ndarray comprising of Fourier
transformed data.
118 Image Processing and Acquisition using Python

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.

axes is an integer used to compute the FFT. If axis


is not specified, the last two axes are used.

Returns: output is a complex ndarray.

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

A photograph of a star obtained from a telescope is blurred by the


particles in the atmosphere. A wide-field microscope image of an object
is blurred by a signal from out-of-plane. Such blurring can be modeled
as convolution operation and eliminated by the inverse process called
deconvolution.
We begin the discussion with convolution in Fourier space. Con-
volution in spatial domain already has been dealt with in Chapter 4,
Spatial Filters. The operation is simpler in Fourier space than in real
space but depending on the size of the image and the functions used,
the former can be computationally efficient. In Fourier space, convo-
lution is performed on the whole image at once. However, in spatial
domain convolution is performed by sliding the filter window on the
image.
The convolution operation is expressed mathematically as:
Z t
[f ∗ g](t) = f (τ )g(t − τ )dτ (6.16)
0

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

6.4.1 Convolution in Fourier Space

Let us assume that the convolution of f and g is the function h.

h(t) = [f ∗ g](t). (6.17)

If the Fourier transform of this function is H, then H is defined as

H = F.G (6.18)
120 Image Processing and Acquisition using Python

where F and G are the Fourier transforms of the functions f and g


respectively and the . (dot) represents multiplication. Thus, in Fourier
space the complex operation of convolution is replaced by a more sim-
ple multiplication. The proof of this theorem is beyond the scope of this
book. You can find details in most mathematical textbooks on Fourier
transform. The formula is applicable irrespective of the number of di-
mensions of f and g. Hence it can be applied to a 1D signal and also
to 3D volume data.

6.5 Filtering in Frequency Domain


In this section, we discuss applying various filters to an image in the
Fourier space. The convolution principle stated in Equation 6.18 will
be used for filtering. In lowpass filters, only low frequencies from the
Fourier transform are used while high frequencies are blocked. Similarly,
in highpass filters, only high frequencies from the Fourier transform are
used while the low frequencies are blocked. Lowpass filters are used to
smooth the image or reduce noise whereas highpass filters are used
to sharpen edges. In each case, three different filters, namely; ideal,
Butterworth and Gaussian, are considered. The three filters differ in
the creation of the windows used in filtering.

6.5.1 Ideal Lowpass Filter

The convolution function for the 2D ideal lowpass filter (ILPF) is


given by

1, if d(u, v) ≤ d
0
H(u, v) = (6.19)
0, else

where d0 is a specified quantity and d(u.v) is the Euclidean distance


from the point (u, v) to the origin of the Fourier domain. Note that for
Fourier Transform 121
 
M N
an image of size M by N , the coordinates of the origin are , .
2 2
So d0 is the distance of the cutoff frequency from the origin.

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

# opening the image and converting it to grayscale


a = Image.open('../Figures/fft1.png').convert('L')
# a is converted to an ndarray
b = numpy.asarray(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
122 Image Processing and Acquisition using Python

# defining the convolution function for ILPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to eliminate
# high frequency
if r > d_0:
H[i,j] = 0.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)
# saving the image as ilowpass_output.png in
# Figures folder
f.save('../Figures/ilowpass_output.png')

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

concept of lowpass filtering. In this technique, all high frequency data


is cleared and only the low frequency data is stored. This reduces the
number of Fourier coefficients stored and consequently needs less stor-
age space on the disk. During the process of displaying the image, an
inverse Fourier transform can be obtained to convert the image to spa-
tial domain. Such an image will suffer from blurring, as high frequency
information is not stored. A proper selection of the cut-off radius is
more important in image compression to avoid blurring and loss of
crucial data in the decompressed image.

6.5.2 Butterworth Lowpass Filter

The convolution function for the Butterworth lowpass filter (BLPF)


is given below:

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:

import numpy, math


import scipy.misc
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/fft1.png').convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
124 Image Processing and Acquisition using Python

d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 1 # the order of BLPF
t2 = 2*t1

# defining the convolution function for BLPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to
# eliminate high frequency
if r > d_0:
H[i,j] = 1/(1 + (r/d_0)**t1)

# 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).

6.5.3 Gaussian Lowpass Filter

The convolution function for the Gaussian lowpass filter (GLPF) is


given below:

−d2 (u,v)
2d2
H(u, v) = e 0 (6.21)

where d0 is the cut-off frequency and d(u, v) is the Euclidean distance


from origin. The filter creates a much more gradual change in intensity
at the cut-off radius compared to Butterworth lowpass filter.
The Python code for the Gaussian lowpass filter is given below.

import numpy, math


import scipy.misc
from scipy.misc import imshow
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/fft1.png').convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
126 Image Processing and Acquisition using Python

d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 2*d_0

# defining the convolution function for GLPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to
# eliminate high frequency
if r > d_0:
H[i,j] = math.exp(-r**2/t1**2)

# 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.

6.5.4 Ideal Highpass Filter

The convolution function for the 2D ideal highpass filter (IHPF) is


given by

0, if d(u, v) ≤ d
0
H(u, v) = (6.22)
1, else

where d0 is the cutoff frequency and d(u, v) is the Euclidean distance


from the origin.
The Python code for ideal highpass filter is given below.

import scipy.misc
import numpy, math
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image
128 Image Processing and Acquisition using Python

(a) Input for lowpass filters. (b) Output of ILPF.

(c) Output of BLPF. (d) Output of GLPF.

FIGURE 6.2: An example of lowpass filters. The input image and all
the output images are displayed in spatial domain.

# opening the image and converting it to grayscale


a = Image.open('../Figures/endothelium.png').
convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
Fourier Transform 129

# shifting the Fourier frequency image


d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius

# defining the convolution function for IHPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to
# eliminate low frequency
if 0 < r < d_0:
H[i,j] = 0.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()
130 Image Processing and Acquisition using Python

# saving the image as ihighpass_output.png in


# Figures folder
f.save('../Figures/ihighpass_output.png')

In this program, the filter (H) is created by assigning pixel value of


1 to all pixels above the cut-off radius and 0 otherwise.

6.5.5 Butterworth Highpass Filter

The convolution function for the Butterworth highpass filter


(BHPF) is given below:

1
H(u, v) =  2n (6.23)
d0
1+ d(u,v)

where d0 is the cut-off frequency, d(u, v) is the Euclidean distance from


origin and n is the order of BHPF.
The Python code for BHPF is given below.

import numpy, math


import scipy.misc
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/endothelium.png').
convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
d = fftim.fftshift(c)
Fourier Transform 131

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 1 # the order of BHPF
t2 = 2*t1

# defining the convolution function for BHPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to
# eliminate low frequency
if 0 < r < d_0:
H[i,j] = 1/(1 + (r/d_0)**t2)

# 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')

6.5.6 Gaussian Highpass Filter

The convolution function for the Gaussian highpass filter (GHPF)


is given below:

−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.

import numpy, math


import scipy.misc
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/endothelium.png').
convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and values in H are initialized to 1
Fourier Transform 133

H = numpy.ones((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # cut-off radius
t1 = 2*d_0

# defining the convolution function for GHPF


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
# euclidean distance from
# origin is computed
r = math.sqrt(r1)
# using cut-off radius to
# eliminate low frequency
if 0 < r < d_0:
H[i,j] = 1 - math.exp(-r**2/t1**2)

# 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')

The image in Figure 6.3(a) is the endothelium cell. The images in


134 Image Processing and Acquisition using Python

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.

(a) Input for highpass filters. (b) Output of IHPF.

(c) Output of BHPF. (d) Output of GHPF.

FIGURE 6.3: An example of highpass filters. The input image and all
the output images are displayed in spatial domain.

6.5.7 Bandpass Filter

A bandpass filter, as the name indicates, allows frequency from a


band or range of values. All the frequencies from outside the band
are set to zero. Similar to the lowpass and highpass filters, bandpass
Fourier Transform 135

filter can be Ideal, Butterworth or Gaussian. Let us consider the ideal


bandpass filter, IBPF.
The Python code for IBPF is given below.

import scipy.misc
import numpy, math
import scipy.fftpack as fftim
from scipy.misc.pilutil import Image

# opening the image and converting it to grayscale


a = Image.open('../Figures/fft1.png').convert('L')
# a is converted to an ndarray
b = scipy.misc.fromimage(a)
# performing FFT
c = fftim.fft2(b)
# shifting the Fourier frequency image
d = fftim.fftshift(c)

# intializing variables for convolution function


M = d.shape[0]
N = d.shape[1]
# H is defined and
# values in H are initialized to 1
H = numpy.zeros((M,N))
center1 = M/2
center2 = N/2
d_0 = 30.0 # minimum cut-off radius
d_1 = 50.0 # maximum cut-off radius

# defining the convolution function for bandpass


for i in range(1,M):
for j in range(1,N):
r1 = (i-center1)**2+(j-center2)**2
136 Image Processing and Acquisition using Python

# euclidean distance from


# origin is computed
r = math.sqrt(r1)
# using min and max cut-off to create
# the band or annulus
if r > d_0 and r < d_1:
H[i,j] = 1.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 ibandpass_output.png in
# Figures folder
f.save('../Figures/ibandpass_output.png')

The difference between this program compared to highpass or low-


pass filters is in creation of the filter. In the bandpass filter, the min-
imum cut-off radius is set to 30 and the maximum cut-off radius is
set to 50. Only intensities between 30 and 50 are passed and every-
thing else is set to zero. Figure 6.4(a) is the input image and Figure
6.4(b) is the output image for the IBPF. Notice that the edges in the
output image of IBPF is sharp compared to the input. Similar filters
can be created for Butterworth and Gaussian filters using the formula
discussed earlier.
Fourier Transform 137

(a) Input of IBPF. (b) Output of IBPF.

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.

• In lowpass and highpass filters ideal, Butterworth and Gaussian


were considered.

• A bandpass filter has minimum cut-off and maximum cut-off


radii.

• Convolution can be viewed as the process of combining two im-


ages. Convolution is multiplication in Fourier domain. The inverse
process is called deconvolution.

• Fourier transform can be used for image filtering, compression,


enhancement, restoration and analysis.
138 Image Processing and Acquisition using Python

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

2. An example for determining Fourier coefficient was shown earlier.


However the discussion was limited to 4 coefficients. Determine
the 5th coefficient assuming f (4) = 2.

3. The central pixel in the Fourier image is brighter compared to


other pixels. Why?

4. The image in Figure 6.2(b) has a fuzzy structure next to the


object. What is this called? What causes the artifact? Why are
there fewer artifacts in BLPF and GLPF output images.

5. Consider an image of size 10,000-by-10,000 pixels that needs to


be convolved with a filter of size 100-by-100. Comment about the
most efficient method for convolving. Would it be convolution in
spatial domain or Fourier?

You might also like