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

• the value of the transform at the origin of the frequency domain, at F(0,0), is called the dc component
• F(0,0) is equal to MN times the average value of f(x,y)
• 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.
• 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:
1. `ifft`
2. `ifft2`
3. `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 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');
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):

```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 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:

• `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 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

• `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,[])`

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

• Identification of high and low pass filters in above images
• Reproduced highpass and lowpass filter for 97.jpg
• Fourier spectrum for 97.jpg

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

• image of horizontal edge
• image of vertical edge
• image of horizontal and vertical edge combined