Assignment 3

advertisement
UMEÅ UNIVERSITY
Computing Science
Stefan Johansson
2005-08-26
Computing with Matlab, 3 credits (TDBA78), Fall 2005
Assignment 3
The main purpose of this assignment is to see how complex functions are implemented and
how images can be manipulated in Matlab.
The source code must be stored in your home directory with the search path
~username/edu/CompMatlab/prac3/ where username is replaced with your login name, e.g.
int05xxx.
You also have to hand in a written report. See this page for how to write a report. The report
must contain a cover sheet, problem description, a print out of the source code and your
reflections on the solution of this assignment.
The report must be written using LaTeX.
Last time for handing in the report is 12.00 Monday October 3, 2005.
The report should be placed in the box marked TDBA78 on the 4:th floor in the MITbuilding.
This assignment is to be solved individually. READ THE GUIDELINES AND RULES
on the homepage such that you are not tempted to try to cheat us. Anyone that doesn’t
follow these rules and guidelines will be reported to the discipline board which might
lead to suspension of studies for some weeks.
Introduction
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 (copies are distributed separately, ask
Per or Stefan if you didn’t get one). Especially, see the section about morphs (pages
772−775). As in the out handed copies, 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.
[to appear]
Implementation
Determine vertex points
First you have to implement a function which takes two pictures and let you select matching
vertex 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
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 lie in a straight line.
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 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.
To simplify the testing of your implementation the function head of the morph function must
be:
function [IMorph] = morph(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 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
can it be suitable to convert the matrix to double (with the Matlab function double). 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’.
Useful functions: size, figure, subplot, hold, line, imread, imshow, image,
save and load.
Your task:




Implement a function that let the user select a number of vertex points in two pictures.
Implement the function morph() that given two pictures compute the morphed
picture at time t.
To test your functions it is practical to implement a small script that calls the function
morph() with different times t and show the resulting picture(s).
Include your resulting pictures in the report (the begin- and end-pictures and at least 4
morphed pictures in between). You are encouraged to use your own pictures!
Download