Image Morphing
Image morphing turns an image into an image through a sequence of frames indexed by . Its goal is an image of the average object. Averaging the pixels of the two images does not give one. Morphing is warping plus cross-dissolving: the warp handles shape (geometry) and the cross-dissolve handles color (photometry).
Both steps use the affine combination with , usually written . It interpolates for and extrapolates outside that range. and can be points, colors, or whole images. A cross-dissolve alone, , shows two faint overlapping objects (ghosting) when the objects are not aligned.
The morph needs corresponding points in and in , for , and one triangulation of them, used in both images. For each :
- Compute the intermediate shape .
- For each triangle of the intermediate shape, find the affine maps and that send it to the matching triangles in and .
- For each pixel in that triangle, set , with interpolation at the non-integer points and . This inverse-warps both images to the intermediate shape and cross-dissolves them.
- Save as the next frame.
A mean face of faces warps each face to the mean shape and then averages the pixels. A caricature pushes a face’s shape away from the mean, to with . That is the morph from the mean shape to the face, evaluated at outside .
Intuition
A cross-dissolve fades one picture into the other. When the two objects sit in different places, the middle frame shows both of them, half transparent. Aligning the images first with one global warp fixes this when the objects differ by a global transformation, such as a shift or a rotation. Two different dogs, or two faces, also differ in proportions. No single affine map puts nose on nose and tail on tail at the same time. Corresponding points state which parts should match. A triangle mesh over those points fills in all pixels between them, and each triangle gets its own affine map. The result is a piecewise affine warp, flexible enough to follow the points and simple enough to compute per triangle.
The figure shows one triangle at from the first example below. The output pixel lies in the intermediate triangle. It pulls a color from each image through the triangle’s two affine maps, and the two colors are blended with weights and .
\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1.2cm,y=1.2cm,>=latex]
\begin{scope}[shift={(-3.2,0)}]
\draw[thick,fill=mid!15] (4.8,-5.2) -- (6.6,-5.0) -- (5.0,-7.4) -- cycle;
\fill[hot] (5.35,-6.25) circle (2pt) node[above left] {\small $80$};
\node at (5.6,-8.0) {$I_0$, shape $\mathbf{p}$};
\end{scope}
\draw[thick,fill=cold!15] (5,-5) -- (7,-5) -- (5,-7) -- cycle;
\fill[cold] (5.5,-6) circle (2pt) node[below right] {$\mathbf{q}$};
\node at (6,-8.0) {shape $\mathbf{p}(t)$, $t = \frac{1}{4}$};
\node[cold] at (6,-8.5) {\small $\frac{3}{4}\cdot 80 + \frac{1}{4}\cdot 160 = 100$};
\begin{scope}[shift={(3.0,0)}]
\draw[thick,fill=mid!15] (5.6,-4.4) -- (8.2,-5.0) -- (5.0,-5.8) -- cycle;
\fill[hot] (5.95,-5.25) circle (2pt) node[above left] {\small $160$};
\node at (6.6,-8.0) {$I_1$, shape $\mathbf{p}'$};
\end{scope}
\draw[->,thick,hot] (5.45,-6.0) .. controls (4.2,-6.6) and (3.2,-6.6) .. (2.25,-6.3) node[midway,below] {$A_0$};
\draw[->,thick,hot] (5.6,-5.95) .. controls (6.8,-6.4) and (8.0,-6.0) .. (8.9,-5.35) node[midway,below] {$A_1$};
\end{tikzpicture}
\end{document}Properties
(No Cracks Between Triangles) Neighboring triangles agree on their shared edge, so the piecewise affine warp is continuous.
An affine map sends a line segment to the segment between the images of its endpoints. Two triangles that share an edge also share its two endpoints, and both maps send those endpoints to the same corresponding points. So both maps agree along the entire edge, and the warped image has no gaps or overlaps there. The warp is not smooth across edges: straight lines in the image can bend at triangle borders. The warp breaks down if a triangle flips orientation, which happens when one corresponding point crosses an edge of its triangle. The intermediate triangle then collapses to a segment at some , and its affine map has no inverse. The third example below shows a case.
(One Triangulation for All Frames) The same triangles, listed by vertex index, are used in , and every intermediate shape.
Triangle-to-triangle correspondence only exists if both meshes use the same index triples. Triangulating and separately can produce different triples. A common choice is to compute the triangulation once, with Delaunay triangulation, on the mean shape . Triangles that are well shaped there tend to stay reasonable in both images. Adding the four image corners as fixed corresponding points makes the mesh cover the full frame, including the background.
(Morph Loop in Code) The lecture’s “life-hack” needs only two nested loops, one over and one over triangles.
Inside the triangle loop, all pixels of the triangle are handled at once: compute their source coordinates in both images and interpolate in one call. In the sketch below, affine_from_triangles(src, dst) returns from the triangle-to-triangle property of 3.1. Affine Transformations.
tri = delaunay((pts0 + pts1) / 2) # index triples, computed once
for t in linspace(0, 1, num_frames):
pts_t = (1 - t) * pts0 + t * pts1 # 1. intermediate shape
frame = zeros_like(im0)
for idx in tri: # idx = [i, j, k], one triangle
A0 = affine_from_triangles(pts_t[idx], pts0[idx]) # 2. shape t -> I0
A1 = affine_from_triangles(pts_t[idx], pts1[idx]) # shape t -> I1
rows, cols = pixels_inside(pts_t[idx]) # e.g. skimage.draw.polygon
q = stack([cols, rows, ones_like(rows)]) # homogeneous (x, y, 1)
w0 = interpolate(im0, A0 @ q) # 3. inverse warp of I0
w1 = interpolate(im1, A1 @ q) # inverse warp of I1
frame[rows, cols] = (1 - t) * w0 + t * w1 # cross-dissolve
save(frame) # 4. next frameThe lecture uses the same for the shape and the color. Some implementations use a separate warp fraction and dissolve fraction, for example to change the shape early and the color late.
(Shape and Appearance Vectors) A face has a short shape vector and a long appearance vector, and warping to a common shape makes appearance vectors comparable.
The lecture’s example uses annotated points, so the shape vector has numbers. A RGB photo is an appearance vector of numbers. Annotation is required, and in return it provides alignment. Averaging photos directly blurs every feature that sits in different places. Warping each photo to the mean shape first puts every eye on the same pixels, so the pixel average gives a sharp mean face. Averaging only the faces in a group gives subpopulation means, such as average male and female faces, happy faces, children, or people wearing glasses. Shape vectors are also the natural input for linear models of face shape, such as PCA.
(Extrapolation) Weights outside go past an endpoint, which exaggerates a difference instead of blending it.
The lecture’s point example still has weights that sum to , so it is a valid affine combination. With the mean shape and one face, the result is a caricature. Every difference between the face and the mean grows by half. Applied to colors, the same rule can push values outside , so results must be clipped. The lecture’s slides show extrapolation for points and mean faces for groups. Caricatures combine the two.
Examples
(Medium) One Pixel of a Face Morph
A triangle of a face mesh has corners , , in and the matching corners , , in , in pixel coordinates. Consider the frame at .
Find the intermediate triangle. For the output pixel , find and . If there is and there is , what is the output value? The intermediate corners are , which gives , and . Its unit-triangle matrix has edge columns and and origin . So has unit-triangle coordinates and .
The map to is
The map to is
Both points have the same barycentric weights in their own triangles. As a check, . Here both source points land on integer pixels. In general they land between pixels and need interpolation.The output value is . At the frame still looks mostly like , in both shape and color.
(Medium) Mean Eyes and a Caricature
Three aligned face photos have their eye centers on one row, at these -coordinates: face A at and , face B at and , face C at and .
Find the mean shape. What goes wrong if the three photos are averaged without warping? Find a caricature of face A with . The mean eye positions are and , so the mean eye distance is .
Averaging the unwarped photos puts dark eye pixels at , and on the left and at , and on the right. Each is one third as dark, so each side shows a blurry smear of three faint eyes. Warping every face so that its eyes move to and first, then averaging, gives one sharp pair of eyes.
Face A has eyes pixels farther out than the mean on each side. The caricature puts them at and . The eye distance grows from to , while the mean’s is . The offset from the mean grows from to , so face A’s wide-set eyes become more noticeable. The same numbers come from the morph between the mean and face A at .
(Hard) A Triangle That Folds
A user clicks the points of a triangle in both images: , , in . In the left and right mouth corners are clicked in the opposite order, so and , with .
Track the signed area of the intermediate triangle. At which does the per-triangle warp fail? The intermediate corners are and , with fixed at the origin. The signed area is It is at , at , at , and at . At both and equal , so the triangle collapses to a segment. Its unit-triangle matrix is singular, and the affine maps and cannot be built. For the triangle is mirrored, and it overlaps its neighbors in the mesh. The fix is to click the points in the same order in both images.
See Also
- 3.6. Delaunay Triangulation, for building the shared mesh.
- 2.6. Multiresolution Blending, another way to combine two images without visible seams.