Introductory Computer Programming (5DV035) – the Matlab part, for

advertisement
UMEÅ UNIVERSITY
Computing Science
Per Lindström
2008-02-19
Introductory Computer Programming (5DV035) – the
Matlab part, for students who have not taken
“Flervariabelanalys”, Spring 2008
Assignment 1
Introduction
The main purpose of this assignment is to solve some simple execises using elementary
Matlab functions, learn how more complex functions might be implemented and how images
can be manipulated in Matlab.
The solutions of the exercises 1, 2, 3 must be presented to an Assistant in a Computer
Lab. The running of each code must also be demonstrated to the Assistant.
Exercise 4 is the main exercise of this assignment. The task is to write MATLAB code that
transforms a given begin-picture to a given end-picture visualazing the morphed images at
certain time-steps between the begin- and end-picture.
The solution of Exercise 4 will be presented orally, by one group of students to another,
at a seminar on the last day of the course. On Friday March 14.
Exercises
Exercise 1. Write a function mean(X) that takes a vector of numbers X=
[x1, x2,…,xn] and returns the mean value of the vector.
Exercise 2. Write a function negate(X) that takes a vector X= [x1, x2,…,xn] and
returns the same vector, but with every second element negated.
Exercise 3. Produce a graphical visualization (using the command surf) of
2
the scalar function f ( x, y)  e (1 xy ) for x   3,3 and y   3,3
Exercise 4
The technique to continuously transform a picture into another is called morphing. In this
assignment, you will implement a function in Matlab that computes the morphing from a
begin-picture to an end-picture. How this is accomplished is described in chapter 11.21 in
Elementary Linear Algebra by H. Anton and C. Rorres. Especially, see the section about
morphs (pages 772−775). We will only consider the case when the pictures are in greyscale.
An example of a morph between Per and Stefan can be seen below. The left-most and rightmost pictures are the original begin- and end pictures. These are available at Per and Stefan.
Determine vertex points
There exists a function, getpoints(), which takes two pictures and let you select
matching vertex points/control points in both pictures. This can, for example, be done by
showing the two pictures side by side and letting the user select a point from each picture
alternately. Store the pixel coordinates for these vertex points in two separate vectors (one for
the x-coordinates and one for the y-coordinates) for each picture.
Note 1: Two corresponding vertex points in the pictures must mark the same (or similar)
characteristics of the two pictures. For example, in the morph from Per to Stefan, the vertex
point vk at the tip of Per’s nose must correspond to vertex point wk at Stefan’s nose tip.
Note 2: Make sure that no three vertex points are collinear, i.e. they must not be on a straight
line.
Note 3: Remember to add vertex points in each corner of the picture. The simplest way is to
do this in the code, the way it is done in the function getpoints().
Useful function: ginput
Morphs
The second part of the assignment is to implement a function that performs the morphing. The
algorithm for this is presented on pages 773−774 (Step 4 to Step 9) in Elementary Linear
Algebra. For the triangulation you can use the Matlab function delaunay(). Read the help
pages for delaunay for a detailed description of the function. You must use the same
triangulation in all pictures, i.e., you only need to call delaunay once (for example for the
begin-picture). To determine which triangle a pixel belongs to in a picture, use the Matlab
function tsearch().
To plot the triangulation in the picture you can follow these steps:
1. Plot the picture.
2. Use the Matlab command hold on.
3. The triangulation is now plotted with:
‘trimesh(TRI,x,y,zeros(size(x)),’FaceColor’,’none’,...
’EdgeColor’,’blue’,’Marker’,’.’,’MarkerEdgeColor’,’red’)’,
where TRI is the triangulation returned by delainay(), and x and y are the vectors
with the vertex points.
See and run the existing Matlab function showtri()to see the triangulation of the two
pictures.
To simplify the testing of your implementation the function head of the morph function must
be:
function [IMorph] = mymorph(x1,y1,x2,y2,TRI,I1,I2,t)






IMorph − The morphed picture at time t.
x1, y1 − Vectors with the vertex points for the begin-picture.
x2, y2 − Vectors with the vertex points for the end-picture.
TRI − The triangulation of the begin-picture returned by delaunay().
I1, I2 − The two matrices with the begin- and end-picture.
t − The morph time.
General issues
To load a picture (in grey scale) into Matlab, use the function imread(). The function
returns the picture as a matrix with elements between 0 and 255. The datatype of the elements
in this matrix is uint8, which is not defined for all kinds of operations in Matlab. Therefore it
can be suitable to convert the matrix to double (with the Matlab function im2double). To
show the picture on the screen use the function call imshow(I,[]), where I is the picture.
To simplify the testing you may want to save the vertex points to disc. This can be done by
the command save, e.g. ‘save vertexpoints x1 y1 x2 y2’ saves the vectors x1,
y1, x2 and y2 in the file vertexpoints.mat in the current directory. To load these vectors write
‘load vertexpoints x1 y1 x2 y2’.
It is possible that tsearch() returns NaN (not a number) for some points. E.g. for
boundary points. This can be solved by expanding the vertex points in the corners a small
amount (≈0.1). E.g. instead of having pixel coordinates (1,1), (n,1), (1,m), (n,m) they are
expanded to (0.9, 0.9), (n+0.1, 0.9), (0.9, m+0.1), (n+0.1, m+0.1) where m and n are the
dimensions of the density matrix for the begin-picture. ([m n]=size(I1))
The following line of code can be useful (only an example) ‘x(x<1) = 1’
If x is a vector then the line of code sets all values less than 1 in x to 1. It can be used when
checking for values outside of a boundary, e.g. to check that all computed points are inside
the boundary of an image.
Useful functions: size, figure, subplot, hold, line, imread, imshow, image,
save and load.
Your task:




Use the function getpoints() which lets the user select a number of vertex points
in two pictures.
Implement the function mymorph() which computes the morphed picture at time t.
To test your function mymorph() it is practical to implement a functiion that calls
the function mymorph() with different times t and showing the resulting picture(s).
An example of such a function is morphall.m
The last day of the course you/your group will present the morphing problem and your
solution of it. Prepare some OH-slides that show your mymorph() function and a
printout of some morphed pictures (the begin- and end-pictures and at least 4 morphed
pictures in between). You are encouraged to use your own pictures!
Hint: some comments on the implementation of morphing
Download