Lab: Fourier Transforms (2)

CSC 262 - Computer Vision - Weinman



Summary:
In this lab you will examine the filtering properties of some common kernels in the frequency domain.

Deliverables

Exercises

A. Gaussian Fourier Transform

  1. The command linspace gives a vector of N linearly spaced numbers between an upper and lower bound. We can combine this with meshgrid to generate a domain for creating and plotting functions. Create a 256×256 domain over -20 to 20 as follows.
    [xx,yy] = meshgrid( linspace(-20,20,256), linspace(-20,20,256) );
  2. Using its functional form,
    g(x,y)=exp(−(x2+y2)/2),
    create a matrix containing a 2-dimensional Gaussian with a variance of 1 using the domains for x and y you created above.
    Remember that your powers should be component-wise!
  3. Normalize your Gaussian kernel matrix so that its contents sum to 1.
  4. Use mesh to plot your kernel in a new figure window.
  5. Use fft2 to take the Fourier transform of your Gaussian kernel.
  6. Use mesh to plot the magnitude of the kernel's Fourier transform (don't forget that you need to do fftshift on the result before you visualize it.)
  7. How do the shapes of the two mesh plots compare? (After you've reflected on this, but not before, you may want to have a look at Szeliski Section 3.4.1 and Table 3.2).
  8. What is the largest magnitude in the FFT of the Gaussian kernel matrix? Why?

B. Filtering in the Frequency Domain

Recall that the convolution theorem dictates we can do filtering in the frequency domain. In this part of the lab you'll "do" convolution without calling conv2.
  1. Load an image of my holiday decoration
    /home/weinman/courses/CSC262/images/tree.png
  2. Convert this image from a uint8 image to a double (so that the largest value can be 1).
  3. Take the Fourier transform of the tree.
  4. Use imshow to display the (shifted and log compressed) magnitude of the tree transform.
  5. Use imshow to display the (shifted and log compressed) magnitude of the Gaussian kernel transform.
  6. Take the component-wise product of the tree and Gaussian kernel transforms (both unshifted).
  7. Use imshow to display the (shifted and log compressed) magnitude of the product.
  8. What effect does this product have on the low and high frequencies, respectively?
  9. Take the inverse Fourier transform of the product with ifft2. You will likely need to extract the real component of the result (using the command real), and you may also need to do an ifftshift on the result to get the quadrants back in the right place.
  10. Use imshow to display the "filtered" image result.
  11. Does the operation have the intended effect? How do the borders compare to a version created using conv2? What other artifacts can you see? What causes these differences?

C. Other Filters

The Gaussian kernel smooths images, but there are other filters with different properties. For instance, the so-called "Laplacian of [the] Gaussian" or LoG filter:
LoG(x,y) = −(1−(x2+y2)/2) g(x,y).
  1. Using the functional form given above, create a matrix containing the 2-D LoG with the same x and y domains used above.
    Remember that your powers and multiplication should be component-wise!
  2. Display your LoG kernel using both imshow and mesh.
  3. As a filter, what types of structures would you expect the LoG to detect?
  4. Take the Fourier transform of the LoG.
  5. Use imshow to display the (shifted and log compressed) magnitude of the LoG's transform.
  6. Based on the LoG's FFT, what effect do you expect the LoG to have on the low, mid, and high frequencies?
  7. Take the component-wise product of the tree and LoG kernel transforms.
  8. Take the inverse Fourier transform with ifft2. You'll likely need to extract only the real component (using the command real), and you may also need to do an ifftshift on the result to get the quadrants back in the right place.
  9. Use imshow to display the result (the LoG is not an averaging filter, so you'll have to relax the bounds on the display).
  10. Where are the responses the strongest (whether high or low)? Does it match your predictions?
Note, if you are unsure, you can verify your result by using conv2 on the original image and the LoG kernel.

D. Box Filter

  1. Create a 256×256 kernel of zeros.
  2. Make the first 10 rows and columns of your kernel a uniform box filter. Don't forget to normalize!
  3. Take the Fourier transform of your box filter kernel.
  4. Use imshow to display the (shifted and log compressed) magnitude of the box filter's transform. You may wish to add false color using a color map to more easily see the differences in magnitudes. The command
    colormap(jet)
    renders the grayscale values as indices into a colormap that ranges from cool to warm colors.
  5. Is the result "isotropic?" (That is, is it the same in all directions?) How does its elimination of high frequencies compare to the transformed Gaussian?
  6. Why might we avoid smoothing with the box filter before downsampling?
  7. Visualize the tree image filtered by the box filter to (partially) support your explanation.
The tree image was captured by Jerod Weinman (as a graduate student) and is Copyright 2006, licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 International License.
Copyright © 2010, 2012, 2015, 2019 Jerod Weinman.
ccbyncsa.png
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 International License.