Lab: Fourier Transforms (Part 1)
CSC 295 - Computer Vision - Weinman
- Summary:
- In this lab you'll explore the effect of frequency,
phase and orientation shifts on the Fourier transforms of some simple
functions and refresh your knowledge of complex numbers to swap the
phase and magnitude of two images' transforms.
- Due:
- Mon 2/22
Deliverables
- The Matlab script used to make your comparisons and generate all figures
- (5 points) Basis image observations (B.5)
- (5 points) Basis image and transform (B.4, 8)
- (5 points) Basis transform observations/explanations (B.7)
- (10 points) Basis image/transform comparisons (B.10
and B.12)
- (10 points) Sinusoid predictions/observations/explanations (C.3)
- (10 points) Phase angle plot (C.5)
- (10 points) Phase angle explanation (C.6)
- (10 points) Rotated cosine transform figure (D.4)
- (10 points) Rotated cosine transform observations/explanations (D.4)
- (10 points) Professionalism of write-up
Extra Credit
- (10 points) Images resulting from phase swap (F.6)
- (5 points) Commentary on phase swap (F.7)
Preparation
Background
Images are two-dimensional functions over some spatial domain. For
instance, if the size of the image is M×N=50×100, then
x (the columns) ranges over the values 1 to 50. Instead of images,
sometimes we want to plot arbitrary two-dimensional functions, such
as the paraboloid z=x2+y2 . You know how to create one-dimensional
functions in Matlab. For instance, to plot the parabola y=x2
in Matlab you might do something like
-
xx = 1:256;
yy = xx.^2;
plot(xx,yy);
How can we plot our two-dimensional function z when it depends
on the values of x and y independently?
The function meshgrid allows us to transform vectors
representing the domains x and y of a function into an array.
We would plot our paraboloid using something like
-
[xx,yy] = meshgrid(0:255, 0:255);
zz = xx.^2 + yy.^2;
surf(xx,yy,zz);
Matlab Reference
- fft2
- Two-dimensional fast fourier transform
- fftshift
- Swap quadrants resulting from FFT calculation
- real
- Extract the real component of a complex number
- imag
- Extract the imaginary component of a complex number
- abs
- Calculate the magnitude of a complex number
- angle
- Calculate the phase angle of a complex number
Exercises
A. Fourier Transform Basics
- Load the cameraman image and convert it to doubles. Conversion will
always be important before using fourier transforms.
-
X = im2double(imread('cameraman.tif'));
- The Matlab function fft2 allows us to take the 2-D fast fourier
transform of an image. Use this to get the FFT of the image. For example,
-
Xf = fft2(X);
- Recall that the fourier coefficients are complex numbers. What is
more, the largest values are often much, much larger than the
smaller values, so it is helpful to compress these and view the logarithm
of their magnitudes. Use imshow to display the logarithm
(Matlab function log) of the magnitude of the
fourier transform.
- Where are values the strongest? Is it where you expect?
- The discrete implementation of Matlab's Fourier transform leaves the
quadrants in the wrong place. For visualization, we need to flip these
around using fftshift on the transform result.
- What orientations do you see strong responses for? Can you account
for these in the image?
B. Basis Function Transforms: Frequency
- If you haven't already, use meshgrid to create the square
domain over 0:255 as demonstrated in the background above.
- Create a (co)sinusoid function using this domain having the form
- Show your image, and don't forget that the cosine ranges from -1 to
1, so you'll need to tell imshow to relax its display conventions.
-
Save your cosine to disk using imwrite
and imadj.
-
How many cycles of the periodic function are
there? Image frequencies are usually measured in cycles/pixel. What
is the frequency of this image?
- Take the FFT of your cosine image (you may also wish to apply fftshift
to the result directly for later visualization). In terms of (u,v),
where do you expect the Fourier coefficients to respond most strongly?
(Note that 256/2+1=129 will be the center of the image.)
-
Verify your prediction. Where are the peaks
(relative to the center point of [129,129])? What frequency or frequencies
(scale and orientation) does this say are present?
Note: Two things can help you navigate an image. The command axis
on will turn on tick marks so you can see what pixels you are looking
at as you zoom in. Also, the command impixelinfo will tell
you (within the figure window) what coordinate you are moused over
and what its value is. Note that impixelinfo gives (X,Y)
coords, which is (Col,Row), the reverse of the usual ordering.
-
Save your (log and shifted) FFT magnitude
image to disk (using either print from the figure window
or imwrite and imadj).
-
Without over-writing your original function
(and its transform), create another simple cosine function on the
same domain having the form
-
How many cycles of the periodic function
are there there? What is the frequency of this image?
- Where should the peaks in the FFT magnitude be (consider them relative
to A.7)?
-
Verify your prediction. Where are the peaks
(relative to the center point of [129,129])? What frequencies (scale
and orientation) does this say are present? How does this compare
to the previous image and its transform?
C. Basis Function Transforms: Phase
- Create a sinusoid function on the same domain having the form
- How many cycles of the periodic function are there there? What is
the frequency of this image?
-
Where should the peaks in the FFT magnitude
be? Verify this. Are the magnitudes any different between the sin
and cos?
- Together on the same axes (don't forget to use hold on),
use the angle procedure to plot just the middle row (129)
of the sin and cos transforms' phase right around the peaks (i.e.,
columns 124:134). Use different line styles to differentiate
them. Don't forget to label your axes! (Last reminder.)
Important: Make sure you use fftshift before
extracting the suggested row and columns.
Optional: Explore the legend command to label your
curves.
-
Save your phase plot.
-
How do the two phases differ? Why?
D. Basis Function Transforms: Orientation
You may recall from geometry that you can rotate the x-coordinate
of a point (x,y) around the origin by angle a with the transformation
equation
- Use the equation above to create a version of your second cosine function
(i.e., from B.9) rotated by pi/4 (45
degrees).
Hint: Now now you'll need to use both values returned from
meshgrid.
- Display and inspect your image. Does it look as you'd expect?
- Where do you expect the peaks in the FFT magnitude to be for this
image?
-
Display the (shifted) FFT magnitude, but do not
take the log. Does it look like you'd expect? Save your image and
explain the results.
Extra. Swapping Phases
Now the fun part. We can verify the conjecture that the Fourier transform
phase is more important than the coefficient magnitude for representing
image content. First, we'll need to refresh some simple geometry of
the complex plane (covered in class). Say we have two complex numbers
Recall that the Matlab abs(r) command would give (a2+b2)1/2,
the magnitude of r and that angle(r) gives tan-1(b/a)
the (four-quadrant) phase angle of r. Thus, the magnitude and angle
of the complex number give its polar-coordinate representation. In
this representation, it is quite easy to swap the phase of two complex
numbers. How then, do we convert from polar back to the normal representation?
It is just as in geometry. If we let m be the magnitude of a complex
number and p be its phase angle, we can cast it back to the form
above using the following transformation
- Load the bug (from our previous lab) and the cameraman images and
resize the bug so that it is the same as the cameraman:
-
Icam = im2double(imread('cameraman.tif'));
Ibug = im2double(imread('~weinman/courses/CSC295/images/bug.png'));
Ibug = imresize(Ibug,size(Icam),'bicubic'));
- Calculate the FFT of both images. No need to use fftshift.
- Create variables that store the magnitude and phase of each image's
transform.
- Create two variables that swap the phases for each images' transforms.
- Take the inverse transforms of each of your two swapped versions using
ifft2. Since the (complex) transform coefficients are now
longer matched, you will need to extract and store only the real part
of the result.
-
Display and save both of your images.
-
What do the results reveal about the importance
of each portion of the coefficients?
Copyright © 2010 Jerod
Weinman.
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.