Вы находитесь на странице: 1из 4

CPSC 505

Assignment 2
Handed out: January 20, 2015
Due: January 29, 2015
We will be using Matlab and its image processing toolbox to manipulate images.
To start Matlab, type matlab. The image processing toolbox should be automatically loaded. Matlab has
extensive online documentation (http://www.mathworks.com/products/image/).
We will use the following 128x128 images available on the Web page: roof and roof-section. Save the TIFF
files to your directory as roof.tif and roof-section.tif. You can read them into Matlab by
[inroof,rmap] = imread (roof.tif);
Matlab can display images. The following command tells it to show everything in 256 gray scales:
colormap(gray(256));
This will create a Figure 1 window in which we will display the image.
The semicolon after a statement in Matlab tells it not to print out the result immediately in the command
window. To show the roof image:
imshow(inroof);
The following shifts the image so that the DC component is 0. Explain what DC component means,
why we remove it. Note that we need to convert the input image, which is stored as an unsigned 8-bit array,
to double precision to use various Matlab builtin tools.
meanroof = mean2(inroof);
roof = double(inroof) - meanroof;
To put a title on the picture:
title(A title you should choose);
To print a copy of an image (for homework):
print -deps afile
This produces the file afile.eps which can be printed. You can also directly print the image using the Print
command under File on the image window (Figure 1).
Now that we have the roof image, lets examine it. We take the Fourier Transform (FT) of the roof image
and display it by:
Froof = fftshift(fft2(roof));
imagesc(sqrt(abs(Froof)));
You can compare this (if you wish) with imagesc(Froof). The fft2 Matlab function creates the FT of an
image and fftshift moves the (0, 0) element of the image to the centre of the image. Print out the FT of
the roof image.
As to why we have to remove the DC component of the roof image before we takes its FT, try computing
the FT with and without removing the DC component.
We are displaying the magnitude of the Fourier transform. Remember: the Fourier transform of an image
will be a matrix of complex numbers which we cannot display directly. Therefore we use the abs operation
to take the magnitude of the complex numbers. We take the sqrt of that magnitudes to make the structure
more visible; some of the values may be relatively small.
Also, look at the phase values, which you can get by using: imagesc(angle(Froof)).
Sinusoidal waves
Before we go on, we will try to understand what the FT contains by looking at some 2D sine waves.
1

[ xv, yv ] = meshgrid (0:2*pi/128:2*127*pi/128, 0:2*pi/128:2*127*pi/128);


creates a mesh of x coordinates and y coordinates, i.e., xv contains the x coordinates at r,c, yv the y coordinate.
An increase in row or column becomes an increase in y or x. The coordinates range from 0 to 2 in 128
increments.
To create a new viewing window (a figure), for comparing images, use:
figure;
Thereafter, when you click on a figure, it becomes the active drawing window and any image will be
displayed in it, until another figure is created, or clicked on.
To create a sine wave varying in x but not in y, we do:
xsin = sin(xv);
imagesc(xsin);
This is essentially a 1D pattern, extended into 2D.
You can also create one varying in y:
ysin = sin(yv);
imagesc(ysin);
To create a sine wave that is rotated by /8 (in which direction?) we do:
% for the particular angle pi over 8:
costh = cos(pi/8)
sinth = sin(pi/8)
imagesc(sin(costh*xv + sinth*yv))
This projects the x and y coordinates in xv and yv onto the direction (costh,sinth), and the sin function
creates the sine wave. Changing the vector changes the orientation of the waves.
How does one get more cycles per image? Produce an image of a 1D pattern in x with sufficient cycles to
make it interesting, i.e., so that you can see more than two full cycles but not so many that the individual
cycles occupy too few pixels. Continue with these new xv and yv coordinates in all that follows. (If there are
not enough cycles per image, the FT of the image will be too close to 0 and will not be visible.)
To get a 2D pattern (the previous is merely a 1D sine wave shifted parallel to itself), we need to add a
sine wave, rotated perpendicular to the previous:
both = sin(costh*xv + sinth*yv) + sin(-sinth*xv + costh*yv);
imagesc (both)
Get a printout of this 2D pattern.
The following explores how one changes the phase of the sine waves produced, of either or both sets of
waves.
Create three images: one like this 2D pattern but varying only in the phase in the first sine, then one with
different phase in the perpendicular sine, and then one that differs in both. Print these three images. While
creating the images, name themwe will use them later.
What happens if you add a sine and a perpendicular cosine wave? How does it compare with the sine plus
perpendicular sine wave with the same parameters?
Compute the FTs of these four images and compare them. Change the rotation of the two sine waves.
What happens to the FT? First keep them orthogonal to each other, i.e., rotate them both equally, and then
use different relative orientations so that they are no longer orthogonal. You dont need to print out the
The two waves, the sine and cosine, added together, are initially perpendicular. The normal direction
to the crests is specified by the constant vector onto which the coordinates are projected. Changing these
changes the orientation of the waves. Using different relative orientations means to change the directions of
the vectors by different amounts, not keeping them orthogonal.
Real images again
To rotate an image:
2

rr = imrotate(roof,45);
This is bigger than the image and contains a background border. To extract an image that contains no
background areas:
crr =imcrop(rr,[48,48,88,88]);
imagesc(crr);
Compute the FT of this image, and compare it with the original. It easiest to compare the FTs if the
rotated and cropped image is padded out to 128x128 before taking the FT.
How do they differ?
Smoothing
We can smooth an image (reduce high frequencies) by filtering it. The Gaussian filter (in 1D) is:

2
2
1/ 2ex /2
To smooth the roof image with a Gaussian filter, where = 1.0, we create a 2D filter of size 11:
fg1 = fspecial(gaussian,11,1);
The size describes the number of elements in the width and height of the square filter. A filter 11 elements
wide (the dimensions are the units of pixels) extends 5 pixels around (0, 0). We should cut off the filter where
the value of the function becomes smaller than some threshold, say 0.005. How many pixels in the positive
direction should a 1D Gaussian filter extend if = 1.0? A filter extending m pixels in the positive direction
and m pixels in the negative direction has half-width m and width 2m + 1. How wide will the filter be?
How does this width scale with ?
To filter an image with filter filt do:
fim = filter2(filt,im);
Filter the roof image with your fg1 filter.
Compute the FT of the filtered image. What is the difference in the image and the FT, before and after
filtering?
How much smoothing (what ) does one have to use to make a significant difference in the FT? Well
measure a significant difference by the mean of the absolute value of the difference of the two magnitudes of
the FTs. The smaller the mean absolute difference (MAD), the more similar they are. Similarly well measure
the difference in two images by the mean of the absolute value of their difference. Determine the required
to create a MAD of 1, 2, 4 and 8 brightness levels between the original and the smoothed image.
Comments on this section:
We explore the connection between and filter size to realize that despite the fact that the Gaussian has
infinite extent (its always positive) in practice we always ignore values that are sufficiently small, so that we
then have a filter of finite size. We usually choose a cutoff threshold. Ive asked you first to think about a
particular threshold and decide how far out you can cut off, i.e., begin to ignore the larger values.
Then I ask you to talk about how the cutoff is influenced by . Please derive a functional (equation)
relationship between m, the filter width, , and T , the threshold, which was 0.005 in the concrete example.
Of course when you implement a filter in Matlab, you choose its width to match the you choose.
Results
What you hand in:
1. What the DC component means and why we remove the DC component.
2. A printout of the FT of the roof image.
3. A printout of a 2D sine image (with only a 1D pattern) with multiple cycles.
4. Printouts of three 2D sine wave images, with different phases, as described.
3

5. your answer about the sine plus sine versus sine plus cosine
6. comments on the FTs of the various sine images, with phase changes, and with varying orientations
7. printout of the FT of the rotated, cropped image
8. comments on the differences between the original and the rotated FTs
9. calculations about and cutoff, and comments
10. comments on FT of filtered image
11. how much smoothing is needed for MAD of 1, 2, 4, and 8 in image

Вам также может понравиться