TP3: Morphing faces

Morphing algorithm

The algorithm is divided into five parts.

Part one consists of manually picking points from two images. These points need to correspond, because point number one in the first image and point number one in the second image will be linked together by a transform vector and when we morph an image we move the points picked along those vectors. So for example if point number one in the first image is the tip of the nose, it should also be the tip of the nose in the second image.

The second part of the algorithm is creating a triangulation from those points, in this case we are creating a Delaunay triangulation and in order to minimize the deformation of the triangles we create this triangulation using the mean point of each pair of points. The third part is computing the intermediate form of each triangle, this is achieved like so:

imgMed_pts = (img1_pts + warp_frac*(img2_pts-img1_pts))

where warp_frac is a percentage indicating how far away from the image source we are morphing.

We then need to compute two transform matrix for each triangle, one from the intermediate form to the source image and one from the intermediate form to the destination image. A transform matrix can easily be computed in MATLAB by T=triangle2/triangle1 where 'triangle' is a matrix of three points forming a triangle.

Finally, those transform matrix are used on every pixel inside the corresponding intermediate triangle to find its coordinate in the source image and in the destination image. We can then apply those colour value to our morphed image. However, if the coordinate in the source or destination image are between pixels (for example coordinate [3.56,12.65]) we interpolate the colour using the four surrounding pixels (in this case using pixels [3,12],[4,12],[3,13] and [4,13]), for this algorithm the interp2 function is used like so:

interp2(x,y,img1(:,:,1),pts_in_source(1,:),pts_in_source(2,:))

where x and y are a grid, img1 is a colour channel and source_pts are the transformed coordinate in the source image. Once the morphed source and destination image are computed, we then combine them using a weighted value for each image like so:

morphed_img = dissolve_frac*morphed_img2+(1-dissolve_frac)*morphed_img1.


Here is the result of the algorithm where the same warp_frac and dissolve_frac were used for each frame:

The average face

Using this algorithm it is easy to create an "average" face using an unlimited number of people. Simply use the mean of the interest points across all images instead of only two, create a triangulation using those points, find the transform matrix of every triangle and interpolate the colour value of every pixel inside those triangles. Once all the faces have been morphed, add their intensity value together and divide them by the number of images used.