CS425 Lab: 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:

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 with a white rectangle and black background.
f=zeros(30,30);
f(5:24,13:17)=1;
imshow(f,'InitialMagnification','fit')
Calculate the DFT. Notice how there are real and imaginary parts to F. You must use abs to compute the magnitude (square root of the sum of the squares of the real and imaginary parts).
F=fft2(f);
F2=abs(F);
figure, imshow(F2,[],'InitialMagnification','fit')
To create a finer sampling of the Fourier transform, you can add zero padding to f when computing its DFT
F=fft2(f, 256,256);
F2=abs(F);
figure, imshow(F2, []) 
The zero-frequency coefficient is displayed in the upper left hand corner. To display it in the center, you can use the function fftshift.
F2=fftshift(F);
F2=abs(F2);       
figure,imshow(F2,[])
To brighten the display, you can use a log function
F2=log(1+F2);
figure,imshow(F2,[])     

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

MATLAB code to create filtered image
entry=imread('entry2.JPG');
hz=fspecial('sobel');
filter_hz=imfilter(double(entry),hz,'replicate', 'conv');
entry=imread('entry2.JPG');
hz=fspecial('sobel');
PQ=paddedsize(size(entry));
HZ=fft2(double(hz), PQ(1), PQ(2));
F=fft2(double(entry),PQ(1),PQ(2));
FDF=HZ.*F;
fdf=ifft2(FDF);
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:

2.1 Lowpass Frequency Domain Filters

Lowpass filters:

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 Filter Formula Mesh Image
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.

Original Image Fourier Spectrum of Image Image with Gaussian lowpass filter

 

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

2.2 Highpass Frequency Domain Filters

Highpass filters:

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 Filter Mesh Image
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.

Original Image Fourier Spectrum of Image Image with Gaussian highpass filter

 

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

 


3. References


4. Exercises

Part 1: Identifying and Using High and Low Pass Filters

  1. Download the following image "97.jpg" and store it in MATLAB's "Current Directory".
  2. Identify which of the following is the result of a lowpass or highpass Butterworth filter and reproduce the results.
    Image 1
    Image 2
  3. Display the Fourier spectrum for 97.jpg

Deliverables:

Part 2: Filtering in the Frequency Domain (using spatial filters)

  1. Download the following image "two_cats.jpg" and store it in MATLAB's "Current Directory".
  2. Load the image data.
  3. Create a spatial filter to get the horizontal edge of the image
  4. Create a spatial filter to get the vertical edge of the image (read the MATLAB documentation of fspecial).
  5. Transform both of these filters to the frequency domain.
  6. Transform the two_cats image to the frequency domain
  7. Apply the appropriate operations in the frequency domain
  8. Transform the data back into the spatial domain
  9. Sum the horizontal and vertical edge components together
  10. The resulting image should look like this:

Deliverables: