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

Extra Credit

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

  1. Load the cameraman image and convert it to doubles. Conversion will always be important before using fourier transforms.
    X = im2double(imread('cameraman.tif'));
  2. 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);
  3. 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.
  4. Where are values the strongest? Is it where you expect?
  5. 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.
  6. What orientations do you see strong responses for? Can you account for these in the image?

B. Basis Function Transforms: Frequency

  1. If you haven't already, use meshgrid to create the square domain over 0:255 as demonstrated in the background above.
  2. Create a (co)sinusoid function using this domain having the form
    z=cos(2 pi  4

    256
    x).
  3. 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.
  4. Save your cosine to disk using imwrite and imadj.
  5. How many cycles of the periodic function are there? Image frequencies are usually measured in cycles/pixel. What is the frequency of this image?
  6. 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.)
  7. 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.
  8. Save your (log and shifted) FFT magnitude image to disk (using either print from the figure window or imwrite and imadj).
  9. Without over-writing your original function (and its transform), create another simple cosine function on the same domain having the form
    z=cos(2 pi  8

    256
    x).
  10. How many cycles of the periodic function are there there? What is the frequency of this image?
  11. Where should the peaks in the FFT magnitude be (consider them relative to A.7)?
  12. 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

  1. Create a sinusoid function on the same domain having the form
    z=sin(2 pi  4

    256
    x).
  2. How many cycles of the periodic function are there there? What is the frequency of this image?
  3. Where should the peaks in the FFT magnitude be? Verify this. Are the magnitudes any different between the sin and cos?
  4. 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.
  5. Save your phase plot.
  6. 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
xrot=x cos(a)+y sin(a)
  1. 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.
  2. Display and inspect your image. Does it look as you'd expect?
  3. Where do you expect the peaks in the FFT magnitude to be for this image?
  4. 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
r
=
a+bi
s
=
c+di
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
r=m cos(p) + i m sin(p).
  1. 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'));
  2. Calculate the FFT of both images. No need to use fftshift.
  3. Create variables that store the magnitude and phase of each image's transform.
  4. Create two variables that swap the phases for each images' transforms.
  5. 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.
  6. Display and save both of your images.
  7. What do the results reveal about the importance of each portion of the coefficients?

Copyright © 2010 Jerod Weinman.
ccbyncsa.png
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.