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

Lab 3 Frequency Domain Processing

The document discusses frequency domain processing and the discrete Fourier transform (DFT). It provides the equations for the DFT and inverse DFT, and explains how to display the Fourier spectrum of an image in MATLAB. Lowpass and highpass filters in the frequency domain are also covered, with Gaussian, Butterworth, and ideal lowpass filters described through their formulas and visual representations. Steps for applying a Gaussian lowpass filter to an image in MATLAB are given as an example.

Uploaded by

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

Lab 3 Frequency Domain Processing

The document discusses frequency domain processing and the discrete Fourier transform (DFT). It provides the equations for the DFT and inverse DFT, and explains how to display the Fourier spectrum of an image in MATLAB. Lowpass and highpass filters in the frequency domain are also covered, with Gaussian, Butterworth, and ideal lowpass filters described through their formulas and visual representations. Steps for applying a Gaussian lowpass filter to an image in MATLAB are given as an example.

Uploaded by

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

Lab 3: Frequency Domain Processing

1. Discrete Fourier Transform


1.1 What is the Discrete Fourier Transform?

This is really a question that is more for your class instructor. The general idea is that
the image (f(x,y) of size M x N) will be represented in the frequency domain (F(u,v)).
The equation for the two-dimensional discrete Fourier transform (DFT) is:

The concept behind the Fourier transform is that any waveform that can be
constructed using a sum of sine and cosine waves of different frequencies. The
exponential in the above formula can be expanded into sines and cosines with the
variables u and v determining these frequencies.

The inverse of the above discrete Fourier transform is given by the following
equation:

Thus, if we have F(u,v), we can obtain the corresponding image (f(x,y)) using the
inverse, discrete Fourier transform.

Things to note about the discrete Fourier transform are the following:

 the value of the transform at the origin of the frequency domain, at F(0,0), is
called the dc component
o F(0,0) is equal to MN times the average value of f(x,y)
o in MATLAB, F(0,0) is actually F(1,1) because array indices in
MATLAB start at 1 rather than 0
 the values of the Fourier transform are complex, meaning they have real and
imaginary parts. The imaginary parts are represented by i, which is the square
root of -1
 we visually analyze a Fourier transform by computing a Fourier spectrum (the
magnitude of F(u,v)) and display it as an image.
o the Fourier spectrum is symmetric about the origin
 the fast Fourier transform (FFT) is a fast algorithm for computing the discrete
Fourier transform.
 MATLAB has three functions to compute the DFT:
1. fft -for one dimension (useful for audio)
2. fft2 -for two dimensions (useful for images)
3. fftn -for n dimensions
 MATLAB has three functions that compute the inverse DFT:
0. ifft
1. ifft2
2. ifftn

1.2 How to Display the Fourier Spectrum using MATLAB?

The following table is meant to describe the various steps behind displaying the
Fourier Spectrum.

Description MATLAB code Image Produced

Create an image
f=zeros(30,30);
with a white f(5:24,13:17)=1;
rectangle and black imshow(f,'InitialMagnification','fit')
background.

Calculate the DFT.


Notice how there
are real and
F=fft2(f);
imaginary parts F2=abs(F);
to F. You must figure,
imshow(F2,[],'InitialMagnification',
use abs to compute 'fit')
the magnitude
(square root of the
sum of the squares
of the real and
imaginary parts).

To create a finer
sampling of the
F=fft2(f, 256,256);
Fourier transform, F2=abs(F);
you can add zero figure, imshow(F2, [])
padding to fwhen
computing its DFT

The zero-frequency
coefficient is
displayed in the
F2=fftshift(F);
upper left hand F2=abs(F2);
corner. To display figure,imshow(F2,[])
it in the center, you
can use the
function fftshift.

To brighten the
F2=log(1+F2);
display, you can figure,imshow(F2,[])
use a logfunction

To get the results shown in the last image of the table, you can also combine
MATLAB calls as in:
f=zeros(30,30);
f(5:24,13:17)=1;
F=fft2(f, 256,256);
F2=fftshift(F);
figure,imshow(log(1+abs(F2)),[])
Notice in these calls to imshow, the second argument is empty square brackets. This
maps the minimum value in the image to black and the maximum value in the image
to white.

1.3 How does the Discrete Fourier Transform relate to Spatial Domain Filtering?

The following convolution theorem shows an interesting relationship between the


spatial domain and frequency domain:

and, conversely,

the symbol "*" indicates convolution of the two functions. The important thing to
extract out of this is that the multiplication of two Fourier transforms corresponds to
the convolution of the associated functions in the spatial domain.

For instance, given a starting image of:


We will apply a sobel filter to detect vertical edges. The following provide a side by side comparison of MATLAB
code required to yield the images in the table:

*Note, this code relies on paddedsize.m

Description Spatial Domain Filtering Frequency Domain Filtering


entry=imread('entry2.JPG');
hz=fspecial('sobel');
PQ=paddedsize(size(entry));
HZ=fft2(double(hz), PQ(1), PQ(2));
MATLAB code entry=imread('entry2.JPG');
F=fft2(double(entry),PQ(1),PQ(2));
hz=fspecial('sobel');
to create filtered FDF=HZ.*F;
filter_hz=imfilter(double(entry),hz,'replicate',
fdf=ifft2(FDF);
image 'conv');
fdf=fdf(1:size(entry,1),1:size(entry,2));
both have
negative values

figure,imshow(filter_hz, []); figure,imshow(fdf,[])

use absolute
function to get rid
of negatives

figure,imshow(abs(filter_hz), []); figure,imshow(abs(fdf), []);


threshold into a
binary image

figure,imshow(abs(filter_hz) >
0.2*abs(max(filter_hz(:)))); figure,imshow(abs(fdf) > 0.2*abs(max(fdf(:))));

You will notice that both approaches result in a very similar looking filtered image.
1.4 Basic Steps in DFT Filtering

The following summarize the basic steps in DFT Filtering (taken directly from page
121 of Digital Image Processing Using MATLAB):

1. Obtain the padding parameters using function paddedsize:


PQ=paddedsize(size(f));
2. Obtain the Fourier transform with padding:
F=fft2(f, PQ(1), PQ(2));
3. Generate a filter function, H, of size PQ(1) x PQ(2)....
4. Multiply the transform by the filter:
G=H.*F;
5. Obtain the real part of the inverse FFT of G:
g=real(ifft2(G));
6. Crop the top, left rectangle to the original size:
g=g(1:size(f, 1), 1:size(f, 2));

2. Lowpass and Highpass Frequency Domain Filters


Based on the property that multiplying the FFT of two functions from the spatial
domain produces the convolution of those functions, you can use Fourier transforms
as a fast convolution on large images. As a note, on small images, it is faster to work
in the spatial domain.

However, you can also create filters directly in the frequency domain. There are two
commonly discussed filters in the frequency domain:

 Lowpass filters, sometimes known as smoothing filters


 Highpass filters, sometimes known as sharpening filters

2.1 Lowpass Frequency Domain Filters

Lowpass filters:

 create a blurred (or smoothed) image


 attenuate the high frequencies and leave the low frequencies of the Fourier
transform relatively unchanged
Three main lowpass filters are discussed in Digital Image Processing Using
MATLAB:

1. ideal lowpass filter (ILPF)


2. Butterworth lowpass filter (BLPF)
3. Gaussian lowpass filter (GLPF)

The corresponding formulas and visual representations of these filters are shown in
the table below. In the formulae, D0 is a specified nonnegative number. D(u,v) is the
distance from point (u,v) to the center of the filter.
Lowpass
Formula Mesh Image
Filter

Ideal

Butterworth

Gaussian

To view the MATLAB calls that were used to create the images in the above table, click on this link.
The following is the result of applying a Gaussian lowpass filter on an image.

Fourier Spectrum of Image with Gaussian


Original Image
Image lowpass filter

The above images were created using two M-files (lpfilter.m and dftuv.m) and the
following MATLAB calls:

 footBall=imread('football.jpg');
 footBall=footBall(:,:,1); % Grab only the Red component to fake gray
scaling
 imshow(footBall)
 PQ = paddedsize(size(footBall));
 D0 = 0.05*PQ(1);
 H = lpfilter('gaussian', PQ(1), PQ(2), D0); % Calculate the LPF
 F=fft2(double(footBall),size(H,1),size(H,2)); % Calculate the discrete
Fourier transform of the image
 LPF_football=real(ifft2(H.*F)); % multiply the Fourier spectrum by the
LPF and apply the inverse, discrete Fourier transform
 LPF_football=LPF_football(1:size(footBall,1), 1:size(footBall,2)); %
Resize the image to undo padding
 figure, imshow(LPF_football, [])
 % Display the Fourier Spectrum
 Fc=fftshift(F); % move the origin of the transform to the center of the
frequency rectangle
 S2=log(1+abs(Fc)); % use abs to compute the magnitude (handling
imaginary) and use log to brighten display
 figure, imshow(S2,[])
2.2 Highpass Frequency Domain Filters

Highpass filters:

 sharpen (or shows the edges of) an image


 attenuate the low frequencies and leave the high frequencies of the Fourier
transform relatively unchanged

The highpass filter (Hhp ) is often represented by its relationship to the lowpass filter
(Hlp):

Because highpass filters can be created in relationship to lowpass filters, the following
table shows the three corresponding highpass filters by their visual representations:

Lowpass
Mesh Image
Filter

Ideal

Butterworth
Gaussian

To view the MATLAB calls that were used to create the images in the above table,
click on this link.

The following is the result of applying a Gaussian lowpass filter on an image.

Fourier Spectrum of Image with Gaussian


Original Image
Image highpass filter

The above images were created using three M-files (lpfilter.m, dftuv.m,
and hpfilter.m) and the following MATLAB calls

 footBall=imread('football.jpg');
 footBall=footBall(:,:,1); % Grab only the Red component to fake gray
scaling
 imshow(footBall)
 PQ = paddedsize(size(footBall));
 D0 = 0.05*PQ(1);
 H = hpfilter('gaussian', PQ(1), PQ(2), D0); % Calculate the HPF
 F=fft2(double(footBall),size(H,1),size(H,2)); % Calculate the discrete
Fourier transform of the image
 HPF_football=real(ifft2(H.*F)); % multiply the Fourier spectrum by the
LPF and apply the inverse, discrete Fourier transform
 HPF_football=LPF_football(1:size(footBall,1), 1:size(footBall,2)); %
Resize the image to undo padding
 figure, imshow(HPF_football, [])
 % Display the Fourier Spectrum
 Fc=fftshift(F); % move the origin of the transform to the center of the
frequency rectangle
 S2=log(1+abs(Fc)); % use abs to compute the magnitude (handling
imaginary) and use log to brighten display
 figure, imshow(S2,[])

3. References
 Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L.
Eddins
 Image Processing Toolbox, For Use with MATLAB (MATLAB's documentation)--available through
MATLAB's help menu or online at:
http://www.mathworks.com/access/helpdesk/help/toolbox/images/

You might also like