1. Image Edge Detection 
Finding edges in images is a classic problem in computer vision. A simple model of an edge is simply a point in the image where the intensity gradient is large. An elementary form of edge detection can be achieved by evaluating the derivative of the image in the x and y directions, and then finding the magnitude of the gradient by taking the square root of the sum of the squares of the gradients in the x and y directions. That is, if Ix and Iy are the image gradients in the x and y directions respectively, then the edge strength is given by
E = sqrt(Ix^2 + Iy^2)
To implement a basic edge detector you are required to write two functions; a function called imdxdy which calculates image gradients in the x and y directions, and a function called simpleedge that uses these gradients to compute an edge strength image as shown above next to the original image. The function imdxdy should meet the following specification function [Ix, Iy] = imdxdy(im)
The argument im is a grey scale image represented by a 2D matrix of numbers. Normally images have values in the range 0-255. A value of 0 represents black (no light) and a value of 255 represents white (full brightness). The returned data, Ix and Iy are 2D arrays representing the gradients in the x and y directions for every point in the image. The gradient in the x direction at any pixel location is simply approximated by taking the value of the pixel to the right of the point being considered minus the value of the pixel to the left of the point being considered. A similar process is used to determine the gradient in the y direction, but this time using the pixels above and below the one being considered. Note that gradient values cannot be calculated at the boundary of the image, these should be set to zero. To calculate the gradients you will probably want to code up a nested loop that visits every row and column of a matrix. For general information on loops and an example of a nested loop look at of lecture 7. The simpleedge function should meet the following specification function E = simpleedge(im)
This function takes as input a grey scale image, then using a function call to imdxdy obtains the image gradients in the x and y directions. From these the function calculates a 2D matrix representing an image of edge strength values, and returns this as E. Note that before using imdxdy in your simpleedge code you should test it to verify it does what you expect. Construct a simple synthetic image, say make the left half of the image all zeros and the right half all ones (use the zeros and ones functions). This produces an image with a vertical edge down the middle. On such an image you can predict what imdxdy should produce - see that it does! Then check with an image with a horizontal edge. A similar testing procedure should then be used on simpleedge. Once we obtain an image of edge strength values as E, we can invert the colour intensity using 255-E to get an image that looks like intaglio etching: 
We can also use thresholding to turn the image of edge strength E into a pure black and white image with only the edges of the image displayed. To do this, a threshold value is used to convert the image into what is known as a binary image. Any value in the image below the threshold value is set to 0; all other values in the image are set to 1. The example below shows the result after a matrix of image values are thresholded at a value of 130. threshold of 130
100 205 47 9 83 0 1 0 0 0
141 187 33 11 96 ------------> 1 1 0 0 0
98 143 96 17 108 0 1 0 0 0
The pixels with a value > 130 end up with a value of 1 in the new image, all other pixels end up with a value of 0. The threshold value is used to convert a grey scale image, which will typically have element (pixel) values in the range 0-255, into an image having only two possible values, 0 (background) or 1 (object). Below are two images after thresholding on E, one obtained using the mean of all pixel values as the threshold, the other using mean+std of all pixel values. 
Warning: E is a 2D matrix, data analysis functions such as mean()or std() will work on the columns first and produce a vector. Note also std(std(E))will not give you the correct standard deviation of all values in E. Think about how you would resolve this problem. Here are some other images for you to use. Click on the link to the image and then select [File | Save As...] to download and save the image in your own directory. Here is how you can read and display images (or any 2D matrix of numbers) within MATLAB >> im = imread('shape1.jpg'); % Reads image file shape1.jpg and returns
% a 2D array of image values.
>> imagesc(im); % Displays the image in a figure window.
>> axis image; % Sets aspect ratio so that the image
% looks 'right'
>> colormap(gray); % Sets the colormap to gray.
% (note the American spelling!)
>> impixelinfo % If you place the cursor at a point in the
% image you will see the current row and column
% numbers along with the image/matrix value
Once you have read the image in you can pass it to your function as follows: >> im = double(im); % First cast the image data to type 'double' (see below)
>> E = simpleedge(im); % Then pass it to your function
>> imagesc(E); axis image; colormap(gray); % Display your edge image
If you display the gradient images produced by imdxdy you will see an embossed effect, this is how applications such as PhotoShop produce this 'special' effect. Note that the imagesc function shifts and rescales the values in your image to the range 0-255. The negative values in the gradient images will be rescaled to 0 and appear as black in the image. Values close to 0 will appear as a mid-grey and large positive values will be displayed as white. Now it comes to the third function you need to implement, showim, which can be considered as the main entry to test and integrate the previous two functions you have created. Note that the imdxdy and the simpleedge function only do the calculation, but not displaying images. The showim function should meet the following specification: showim(filename, choice) This function takes a string and a number as input, and display a special effect of the image according to the user choice. The input string filename specifies the image filename, and the input choice is a whole number ranged from 1-7, indicating which type of special effect to display. Make sure you display an error message for all invalid choice value. The following help message shows how the function can be used. >> help showim
SHOWIM: Displays a special effect of an image according to user choice.
Usage: showim(filename, choice)
Arguments: filename - the image's filename
choice - a number between 1-7 that indicates type:
1 : original image in gray scale
2 : embossed image based dx
3 : embossed image based dy
4 : edge strength image
5 : etching image using colour inversion
6 : etching image using mean thresholding
7 : etching image using mean+std thresholding
Author: Your name and Student No.
Date: 22 September 2008
Warning: MATLAB reads images in as data of type uint8 (unsigned 8 bit integer) rather than of type double
>> whos
Name Size Bytes Class
im 181x156 28236 uint8 array
^^^^^
MATLAB prevents you doing operations such as multiplication on data of type uint8 because numerical overflow is very likely. If you want to do multiplication, or some other operation, on an image you need to cast it into an array of doubles. >> im = double(im); % cast im from uint8 to double
>> whos
Name Size Bytes Class
im 181x156 225888 double array
^^^^^^
(Note how the image now consumes much more memory - 8 times as much) Note that MATLAB provides some image edge detection and image filtering functions within the image processing toolbox. Unfortunately you may not use these functions in your assignment!
2. Simulation of a Mass on a SpringConsider a mass hanging from a spring
If the mass is raised to the top and then released it will fall and then bounce up again. The oscillations will eventually die away due to wind resistance and internal losses in the spring. At any instant the forces acting on the mass are: - A gravitation force, Mg acting in the positive direction downwards. Here M is the mass of the object and g is gravitational acceleration (9.81 ms-2).
- A spring force, -kx, where k is the spring constant in Newton metres-1, and x is the amount the spring has stretched in metres. Note the negative sign, when x is positive the force is upwards in the negative xdirection.
- A damping force, -cv, where c is the damping constant and v is the velocity ms-1. Again note the sign, when the velocity is positive (downwards) the resulting force will be upwards, and vice-versa. (Actually if the damping was primarily due to wind resistance the force will be proportional to velocity squared - but we shall assume a simple damping model).
Write a function to simulate the motion of a mass on a spring when it is raised to the top and then released. The function should meet the following specification: function [a, v, x, t] = simspring(m, k, c, T, dt)
Arguments:
m - mass of the object.
k - spring constant.
c - damping constant.
T - the total time to simulate the system for.
dt - the time increment size used in the simulation.
Returns:
a - array of accelerations, one value for every time step.
v - array of velocities.
x - array of positions.
t - array of time values corresponding to each element
in a, v and x.
The function should also plot on the screen the position of the mass
as a function of time. The plot should be annotated with the values
of mass, spring constant and damping constant.
Hints: Your simulation code will be contained in a for loop that increments time in steps of dt until we get to T. Initially the position, x and velocity v will be zero. - At any step we can calculate the gravitational force, spring force and damping force on the mass. From this we can get the net force acting on the object.
- Given the net force we can then calculate the acceleration of the object using the equation
F = ma hence a = F/m
- From the acceleration we can update the velocity estimate. The new velocity will be the velocity at the last time step, plus the change in velocity caused by the new acceleration acting over the time step interval
v = vlast + a dt
- From the velocity and acceleration we can update the position estimate. We take the last position and add the distance traveled due to the mass moving at its old velocity times the time step interval, and then we add the distance traveled as a result of the mass accelerating over the time interval.
x = xlast + vlast dt + 0.5 a dt2
Then with the new position and velocity estimates we can go back and calculate the new spring and damping forces on the mass for the next iteration of the loop. Hopefully these equations will be familiar to you from your school physics or first year dynamics. If not, feel free to ask your lab demonstrator for extra explanation. Here is the output from my code using a mass of 1kg, a spring constant of 10Nm-1, a damping coefficient of 1Nsm-1, over a time interval of 10 seconds with a time increment of 0.01 seconds. Note to make the vertical axis positive downwards use the command axis ij (look at the help info for axis) 
You can also look at the plots of acceleration and velocity as a function of time. Think how you might test your code, what position should the mass converge to? What frequencies of oscillations do you expect? See how the results vary with the size of time increment. For every time step we assume acceleration is constant, this will introduce some error.
What are we looking for in your MATLAB code?I assume that you will discuss your project work with your friends, this is fine. Indeed, the laboratory demonstrators will help you (within reason). However, ultimately the work you submit must be yours. Plagiarism will incur severe penalties. You should make sure you are familiar with the School's policy on plagiarism. Submission ProcedureYour assignment work is to be submitted electronically using the cssubmit facility. No other method of submitting your functions is permitted. Submissions via floppy disc, memory sticks, CDs or email will not be accepted. To submit your files enter your username and password at the cssubmit login page http://secure.csse.uwa.edu.au/run/cssubmit You will then be presented with a page that lists your submissions (which will be none initially). Click on the 'submit' button. You will then have to select an item to submit. Select "CITS1005 Project 1". From there you can then specify the files from your directories to submit. The cssubmit facility displays an electronic receipt number that provides evidence of your submission. You may save, print, and keep a copy of the receipt. Your files can be submitted at anytime up to the due date and, if necessary, several times over until you are satisfied with what you have done. Read this or lose marks...The names of the function files that you submit must be imdxdy.m, simpleedge.m, showim.m and simspring.m respectively. These names must be exactly as specified (including case), the functions must take arguments exactly as specified, and return values exactly as specified. Your functions will be invoked from a testing script that will be expecting these file names and passing arguments as specified. Marks will be deducted if you do not follow the specifications. Note that if you have written your code under a Windows environment you should be very careful to check that the function names and filenames have the correct case. Windows is not case sensitive, so errors in case will not be noticed. I will be testing your code under a Linux environment which is case sensitive... It is possible that you have chosen to write some additional functions to support the operations of the functions you have written. This is fine - just make sure all these files are submitted too so that your code will run! Good Luck! |