You are on page 1of 4

Digital Image Processing: Lab Assignements

#2: Image Filtering in the Spatial Domain and Fourier


Transform

Todor Stoyanov and Achim Lilienthal, Orebro


University
Issue date: 2011-04-28

Due date: 2011-05-05

Instructions
One report has to be handed in per team. Nevertheless, you must be able to answer
questions about your lab report individually.
Clearly state your names on the lab report.
The printed lab report (including source code listings) must be handed in within one (1)
week. Additionally, you have to send your source code (Matlab m-files) by electronic
mail to todor.stoyanov@oru.se. Use the subject DIP Lab, clearly state your names
and the lab session in the message and attach your files (in plain ASCII format).
Each lab report is either marked as fail or pass. To pass the labs, all reports must have
been marked as pass. If the report is handed in more than three days after the deadline,
the report is marked as fail. Up to five bonus points may be awarded to the team for
very good lab assignments that comply with the criteria described below:
+1p Report is clearly written and easy to follow.
+1p Code is well documented.
+1p Efficient and correct use of matlab API (vectorization, API functions, plots,
etc.)
+1p Awarded for particularly well explained procedures succinct, but descriptive,
use of references from scientific articles, textbooks, etc.
+1p Reserved for overall exceptional reports, that conform to all scientific writing
standards .
No bonus points are awarded to late reports (handed in after the due date).
If the assignment is not accepted, due to a serious error in one of the problems, no
bonus points will be awarded for this assignment and subsequent resubmissions.
Any additional materials used during the completion of the assignment must be
cited. Failure to correctly reference sources will result in zero points.
You will find more material (assignments, images, source code, tutorials, etc.) for the labs
on the course web page:
http://aass.oru.se/Research/Learning/courses/dip/2011/index.html.
1

Image Filtering

In this task you will write your own convolution operator for linear spatial filtering of digital
images in MATLAB and investigate the effects of different filter masks or kernels.

1.1

Convolution Operator

Write your own m-function that implements the discrete 2D convolution operator (see lecture
slides and course literature): i.e., given an intensity image and a filter mask (a matrix with
coefficients) your function should convolve the source image with the filter mask and return
the resulting output image. You may assume that the filter mask is a square matrix with odd
size (e.g. 3 x 3, 5 x 5, 7 x 7). To make life easier, you could start by assuming a 3 x 3 filter
mask, and then generalize your code later when you have time. You will need to decide on a
sensible strategy for dealing with the image borders. An example skeleton for you m-function
code is given as follows.
function [im_out] = my_conv2(im_in, kernel)
% IM_OUT = MY_CONV2(IM_IN, KERNEL) convolves the input image IM_IN
% with the filter KERNEL and returns the result in IM_OUT
% IM_IN, IM_OUT, and KERNEL are of DOUBLE precision
% ... here goes your code ...
end
For example, you might call your function as follows to implement a Gaussian filter:
>> gauss_mask = [0.0625 0.125 0.0625; 0.125 0.25 0.125; 0.0625 0.125, 0.0625];
>> g = my_conv2(f, gauss_mask);
To convert an intensity image loaded with imread to DOUBLE precision you can use the
function im2double. This automatically rescales the pixel values to the interval [0, 1] (0.0 is
black and 1.0 is white, by convention). It is not necessary to convert the resulting image
back to integer (imshow can handle this; otherwise you can use the function double2im).
Please note: your implementation will be slow, depending on the input image size and
kernel size, as the MATLAB interpreter is not very good at processing FOR loops! So you
may want to test your code with smaller, thumbnail images during debugging. (Later you
can also investigate the built-in commands for linear spatial filtering, but the purpose of this
task is to help develop your MATLAB programming skills and to deepen your understanding
of spatial filters.)

1.2

Smoothing Filters

Using your implementation of the convolution operator, try out and compare the following
smoothing filters on the image clown.tif. The filters are described in the lecture slides and
the course book.
Mean filter: sizes 3 3, 5 5, and 9 9. What effect has the size of the filter kernel?
2

Gaussian filter: sizes 3 3 and 5 5.


Median filter.
Because of its practical importance, the Image Processing Toolbox (IPT) of MATLAB
also provides a specialized implementation of the 2-d median filter:
>> g = medfilt2(f, [m n], padopt);
where [m n] defines a neighbourhood over which the median is calculated, and padopt
specifies the strategy for dealing with the image border (see help medfilt2 for further
details). The default form of the function
>> g = medfilt2(f);
uses a 3 3 neighbourhood to compute the median, and pads the border of the input
with zeros.
Show the original image and all result images (labeled with the used filter kernel and filter
kernel size). Compare the results and give a qualitative comment.
Compare the results of spatial filtering with mean and median filters of the same size.

1.3

Edge Filters

Using your implementation of the convolution operator, try out and compare the following
edge filters on the image clown.tif. The filters are described in the lecture slides and the
course book.
Sobel edge detector: size 33, vertical Gx and horizontal Gy . Compute the approximate
magnitude |G| of the filter responses: |G| = |Gx | + |Gy |.
Laplacian for edge detection: size 3 3.
Show the original image and all result images (labeled with the used filter kernel and filter
kernel size). Compare the results and give a qualitative comment.

Unsharp Masking

Write your own m-function for unsharp masking of a given image to produce a new output
image (see lecture slides). Your function should apply the following steps:
1. apply smoothing to produce a blurred version of the original image,
2. subtract the blurred image from the original image to produce an edge image,
3. add the edge image to the original image to produce a sharpened image.
Apply your unsharp filter to the image clown.tif and show the original image and the
sharpened image.
3

Fourier Transform

In this exercise we take a look at the discrete 2D Fourier transform in Matlab. The function
fft2 computes the 2D fast Fourier transform (FFT) of an input matrix (the output is complex
valued!); ifft2 computes the inverse FFT of a complex valued input matrix (the output might
be complex valued due to numerical inaccuracies; therefore use the function real to get the
real part only, or abs for the magnitude); and fftshift shifts the zero-frequency component
to the center of spectrum by swapping quadrants (this is useful for visualization).

3.1

Phase and Magnitude

For a complex number z = x + y with x = <{z} and y = ={z}, write down the mathematical
definitions (formulas) for the magnitude r and phase of z, and a formula (*) to compute z
from r and only.
Write Matlab code that computes and displays the magnitude and phase of an intensity
image (in this example the image file mandrill.tif). Useful functions are (you may want
to read the help pages): fft2, fftshift, abs, angle, log. Convert the input image with
rgb2gray and im2double beforehand. To display the results, use the function imagesc and
the command colormap(gray).
The report should include the Matlab code (either command line, or a small function),
and the magnitude and phase plot of the mandrill image.

3.2

Phase vs Magnitude

Investigate the information content of phase and magnitude. Swap the phase of the clown
image (file clown.tif) and the phase of the mandrill image (previous task) using the equation
(*), i.e. replace the mandrills phase information with the phase information from the clown
image and vice versa, and combine them into new matrices. Then compute the inverse Fourier
transform of the created complex matrices.
The report should include magnitude and phase plots of the images before and after the
transformation.
Now, comment on the following: What do the magnitude plots tell you? Can you recognize
the clown or the mandrill? Are the phase plots more informative than the magnitude plots?
Can you still recognize the mandrills fur, for example? Take a look at the generated images:
which information is more important phase or magnitude?

You might also like