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:
fft
-for one dimension (useful for audio)fft2
-for two dimensions (useful for images) fftn
-for n dimensionsifft
ifft2
ifftn
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.
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.
The following summarize the basic steps in DFT Filtering (taken directly from page 121 of Digital Image Processing Using MATLAB):
PQ=paddedsize(size(f));
F=fft2(f, PQ(1), PQ(2));
H
, of size PQ(1)
x PQ(2)
....G=H.*F;
g=real(ifft2(G));
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:
Three main lowpass filters are discussed in Digital Image Processing Using MATLAB:
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,[])
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
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,[])
Image 1 |
Image 2 |
two_cats
image to the frequency domain