In today’s post, I will show you how to perform a two-dimensional Fast Fourier Transform in Matlab. The 2D Fourier Transform is an indispensable tool in many fields, including image processing, radar, optics and machine vision. In image processing, the 2D Fourier Transform allows one to see the frequency spectrum of the data in both dimensions and lets one visualize filtering operations more easily. In radar, the 2D Fourier Transform is used as a fast way to create a map from a series of coherent radar pulses. Additionally, the far-field pattern of a 2D antenna can be calculated using a 2D Fourier Transform. In Fourier Optics, the 2D Fourier Transform is used to calculate the propagation of electromagnetic waves and through space and optical elements.
The 2D Fourier Transform is simply a Fourier Transform over one dimension of the data, followed by a Fourier Transform over the second dimension of the data. The 2D Inverse Fourier Transform is just the inverse Fourier Transform performed over both dimensions of the data. The formulas for the 2D Digital Fourier Transform and Inverse Transform, courtesy of Rice University, are as follows:
In the following example, I will perform a 2D FFT on two images, switch the magnitude and phase content, and perform 2D IFFTs to see the results. This exercise will hopefully provide some insight into how to perform the 2D FFT in Matlab and help you understand the magnitude and phase in Fourier domain. Here is the code for this example:
%2D FFT Demo
imageA = imread('greekchurch','jpg'); imageB = imread('aishwarya','jpg');
figure, imshow(imageA) title('Image A - Greek Church') figure, imshow(imageB) title('Image B - Aishwarya Rai')
%Perform 2D FFTs
fftA = fft2(double(imageA)); fftB = fft2(double(imageB));
%Display magnitude and phase of 2D FFTs
figure, imshow(abs(fftshift(fftA)),[24 100000]), colormap gray title('Image A FFT2 Magnitude') figure, imshow(angle(fftshift(fftA)),[-pi pi]), colormap gray title('Image A FFT2 Phase') figure, imshow(abs(fftshift(fftB)),[24 100000]), colormap gray title('Image B FFT2 Magnitude') figure, imshow(angle(fftshift(fftB)),[-pi pi]), colormap gray title('Image B FFT2 Phase')
%Switch magnitude and phase of 2D FFTs
fftC = abs(fftA).*exp(i*angle(fftB)); fftD = abs(fftB).*exp(i*angle(fftA));
%Perform inverse 2D FFTs on switched images
imageC = ifft2(fftC); imageD = ifft2(fftD);
%Calculate limits for plotting
cmin = min(min(abs(imageC))); cmax = max(max(abs(imageC))); dmin = min(min(abs(imageD))); dmax = max(max(abs(imageD)));
%Display switched images
figure, imshow(abs(imageC), [cmin cmax]), colormap gray title('Image C Magnitude') figure, imshow(abs(imageD), [dmin dmax]), colormap gray title('Image D Magnitude')
saveas(1,'imageA.png') saveas(2,'imageB.png') saveas(3,'imageAfftmag.png') saveas(4,'imageAfftpha.png') saveas(5,'imageBfftmag.png') saveas(6,'imageBfftpha.png') saveas(7,'imageC.png') saveas(8,'imageD.png')
Notice that imread is used to import the images into Matlab. This function can handle most of the standard image file formats, such as bmp, jpg, tiff and png. In this example, imshow is used to display the images. Imshow is one of several functions that plots images, but this function automatically eliminates the axes, displaying the images nicely. This function works well for original images, but when the Fourier transform of an image, or any other significant image processing, is performed, color limits should be adjusted to display a sufficient amount of detail in the data. Imagesc can also be used to display the images. Below are the images that were used in this example, a Greek church on the island of Santorini and Aishwarya Rai.
The 2D FFTs are accomplished using fft2. The image files are imported as uint8, so they should be converted to double arrays before doing the FFTs. The FFT of real, non-even data is complex, so the magnitude and phase of the 2D FFTs should be displayed. The function fftshift is used shift the quadrants of the FFT around to see the lowest frequencies in the center of the plot. Fftshift only has to be called once, as it automatically shifts the quadrants around for 2D matrices. Below are the 2D FFTs of the two images.
Look at the FFTs of the two images. Most of the energy in the Fourier domain is present in the center of the image, which corresponds to low frequency data in the image domain. This corresponds to the many gradual changes in the images. However, the FFT of the Greek church, image A, has a diagonal line intersecting the center of the plot. This line is perpendicular to the edge of the wall in the original image. A strong line across an image will produce a perpendicular line in its FFT. Additionally, the Greek church has more sharp, geometric features than the picture of Aishwarya Rai, so there is more energy in the higher frequencies, away from the center of the FFT.
The phase of the FFTs is somewhat hard to interpret visually and generally looks like noise. However, the phase holds a great deal of the information needed to reconstruct the image. To demonstrate the role of the phase of the FFT, I switched the magnitude and phase of the two images in the Fourier domain then performed an inverse 2D FFT to view the results. The two mixed images are shown below.
Image C has the magnitude of the Greek church and the phase of Aishwarya Rai, while Image D has the opposite. You can see that most of the information in the image that we perceive is stored in phase. To display the images properly using imshow, the color range of the plot must the minimum and maximum values in the data. Additionally, because the magnitude and phase were switched, the image will be complex. This means that the magnitude of the image must be taken in order to produce a viewable 2-D image.
So that is it for this week’s post. In my next post, I will create another example that shows how to do image processing with the 2D FFT.
Previous posts on Fourier Transforms