Pour ce TP, vous produirez une animation qui affichera votre visage, et la métamorphosera vers le visage d'un autre étudiant du cours, sous la forme d'une séquence vidéo. Lorsque le TP sera terminé, nous (les responsables du cours) allons raccorder vos vidéos afin de créer une séquence vidéo finale qui présentera une métamorphose de chacun d'entre nous. Vous devrez aussi produire des animations avec d'autres images.
For this homework, you will produce a morphing animation of your face into another student's face. We (your professor and TA) will connect them in order to create a final video sequence which will show a continuous morph through all the faces of the class. You will also have to produce animations with other images.
Avant même de commencer à implémenter l'algorithme de métamorphose, il nous faut votre photo, ainsi qu'une façon d'établir des correspondances entre les images. Pour ce faire, effectuez les étapes suivantes dans l'ordre :
Par exemple, notez comment le premier point d'intérêt (1) correspond au menton, et les points subséquents (jusqu'à 13) contournent le visage à intervalles réguliers. Il est très important d'effectuer cette tâche attentivement: vous aurez besoin des points d'intérêts de vos collègues, et ils auront besoin des vôtres!
(N+1)_NomDeFamille_Prenom.jpg
et le fichier de points (N+1)_NomDeFamille_Prenom.txt
. Ici, N
est le nombre de photos déjà présentes dans le répertoire, donc (N+1)
représente le prochain nombre. Téléversez ces deux fichiers dans le Drive mentionné ci-haut.Before we implement anything, we need your photo as well as a way to establish correspondences across images. To do so, follow these steps in order:
For example, note that the first point (1) corresponds to the chin, and the following points (up to 13) follow the face region clockwise at regular intervals. It is critical for you to do this task very carefully: you will need your colleague's points, and they will need yours!
(N+1)_LastName_FirstName.jpg
, and the points (N+1)_LastName_FirstName.txt
. Here, N
is the number of photos already present in the Drive, so (N+1)
is the next number. Upload both these files in the Drive.Les étapes de l'algorithme de métamorphose sont:
Chacune des étapes de l'algorithme sont décrites plus bas.
The morphing algorithm is composed of the following steps:
All of these steps are described below.
Tout d'abord, vous devrez définir manuellement des paires de points correspondants sur les deux images. En général, plus il y a de points, mieux c'est. Pour ce faire, vous pouvez utiliser le même code python.
First, you will need to manually define pairs of corresponding points on the two images (usually the more points, the better the morph). You can use the same python code to select points.
Ensuite, vous devez séparer l'image en plusieurs triangles en utilisant les points sélectionnés à l'étape précédente. Une bonne façon de faire est d'utiliser un algorithme de triangulation. Une triangulation de Delaunay (voir scipy.spatial.Delaunay
en python) est un bon choix puisque cet algorithme ne produit pas des triangles trop minces. Pour ce faire, calculez la triangulation sur la moyenne des deux ensembles de points. Ceci diminuera la déformation potentielle des triangles, et réduira les déformations.
tri
ci-bas) qu'une seule fois ! Cette dernière doit demeurer constante tout le long du métamorphose, sinon des triangles changeront abruptement.Then you will need to divide the image into several parts using your previously selected points. A good way is to use a triangulation algorithm. The Delaunay triangulation (see scipy.spatial.Delaunay
in python) is a good choice since this algorithm does not produce triangles that are too thin. Compute the triangulation on the average of the two sets of points, to decrease visual deformations.
tri
variable below) only once! The triangulation must be the same throughout the morph, otherwise triangles will abruptly change. Vous devez écrire la fonction suivante:
You must write the following function:
morphed_img = morph(img1, img2, img1_pts, img2_pts, tri, warp_frac, dissolve_frac)
qui produit une distorsion entre img1
et img2
en utilisant la correspondance des points img1_pts
et img2_pts
et la structure de triangulation tri
. Les paramètres warp_frac
et dissolve_frac
contrôlent respectivement la distorsion de forme ainsi que le niveau de fondu. Plus particulièrement, les images img1
et img2
sont d'abord transformées en une forme intermédiaire contrôlée par warp_frac
et le fondu est ensuite fait en fonction de dissolve_frac
. Ces paramètres varient de 0 à 1. Ce sont les seuls paramètres qui varieront entre chaque trame de l'animation. Pour la trame de départ, ils devraient être chacun égal à 0 et, pour la trame finale, ils devraient être chacun à 1. La variable tri
représente la triangulation, et stocke, pour chaque triangle, les indices des 3 points le composant.
which produces a distortion between img1
and img2
using the correspondence points img1_pts
and img2_pts
and tri
the triangulation structure. The warp_frac
and dissolve_frac
parameters respectively control the distortion of the shape and the level of dissolution. Specifically, images img1
and img2
are first transformed into an intermediate shape which is a weighted mean of both points (where the weight is warp_frac
). The level of dissolution is made according to dissolve_frac
. These parameters vary from 0 to 1. These are the only parameters that should vary between each frame of the animation. At the start, they should each be equal to 0 and, for the final frame, they should each be equal to 1. The tri
variable represents the triangulation and stores, for each triangle, the indices of the 3 points forming the triangle.
Cela consiste à calculer la déformation de chaque triangle de la triangulation à partir des images originales jusqu'à un point intermédiaire entre ces deux images. Cela se fait en calculant simplement une matrice de transformation affine entre deux triangles. Ces transformations doivent être calculées indépendamment pour chaque paire de triangles.
The main task is to calculate the distortion of each triangle of the triangulation from the original images to the intermediate morph. This can be done by computing an affine transformation matrix between the triangles. These matrices must be computed independently for each pair of triangles.
Notez que vous ne pouvez pas utiliser les fonctions calculant les transformations pour vous (par exemple, les fonctions disponibles dans skimage.transform
).
Note that you cannot use functions to calculate the transformation for you (eg. functions available in skimage.transform
).
Après avoir calculé les transformations affines, il vous faut maintenant obtenir la couleur de chacun des pixels. Pour chaque triangle, calculez la transformation affine inverse qui vous permettra d'aller lire la couleur associée à chaque pixel dans les deux images, et d'en calculer une moyenne pondérée. La poids de chaque image correspond à dissolve_frac
et 1-dissolve_frac
.
After the affine transforms are computed, you must now compute the color for each pixel. For each triangle, compute the inverse affine transform, use it to look up the color associated to each pixel in both images, and compute their weighted average. The weight for each image corresponds to (1 - dissolve_frac
) and dissolve_frac
.
matplotlib.path.Path.contains_points
et l'adapter à vos besoins. La fonction scipy.interpolate.RectBivariateSpline
effectue l'interpolation en 2D. Nous vous conseillons fortement d'utiliser ces fonctions.
matplotlib.path.Path.contains_points
and adapt it to your needs, however. The function scipy.interpolate.RectBivariateSpline
performs 2D interpolation. We strongly recommend you use these functions.
Finalement, il vous reste à déterminer quoi faire avec les pixels à l'extérieur de l'objet. Pour ce faire, rajoutez des points d'intérêts en bordure de l'image (aux quatre coins, ou de façon plus dense), et traitez-les comme les autres dans votre algorithme. De cette façon, l'image entière est transformée.
Finally, you have to decide what to do with the pixels outside the region of interest. To do so, add interest points on the image border (at each corner of the image, or more densely), and treat them like the others in your algorithm. This way, the entire image is transformed.
warp_frac
avant d'augmenter dissolve_frac
. Autrement, les caractéristiques pourraient ne pas être correctement alignées lors du fondu. Cela est plus important/utile lorsque vous effectuez du métamorphose entre des visages.warp_frac
a lot before even increasing dissolve_frac
. Otherwise, the features may not be properly aligned when dissolved. This is more important/useful when you perform morphing with faces. matplotlib.path.Path.contains_points
) et vous appliquez les transformations inverses pour trouver les valeurs dans les images originales. Cela est illustré dans l'image plus haut.matplotlib.path.Path.contains_points
) and you apply the inverse transformations to find the values in the original images. This is illustrated in the images above.(r,c)
des coordonnées (x,y)
! Par exemple, dans python: img(r,c) = img(y,x)
. (r,c)
and coordinates (x,y)
! For example, in python: img(r,c) = img(y,x)
. skimage.transform
, car ces dernières fournissent plusieurs aspect du TP que vous devez implémenter vous-mêmes !
skimage.transform
, since they implement several parts of the TP that you must code yourself!
Pour créer une séquence vidéo, vous pouvez utiliser, par exemple, la librairie ffmpeg. Avec ffmpeg, la commande suivante générera le fichier test.mp4
à partir d'images individuelles nommées file_00001.png
, file_00002.png
, ...:
ffmpeg -i file_%05d.png -c:v libx264 -vf "fps=25,format=yuv420p" test.mp4
To create a video sequence, you can use, for example, the ffmpeg library. With ffmpeg, the following command will create a video file test.mp4
from individual input images named file_00001.png
, file_00002.png
, ...:
ffmpeg -i file_%05d.png -c:v libx264 -vf "fps=25,format=yuv420p" test.mp4
Essayez ces idées pour approfondir vos connaissances (et augmenter votre note):
Try these ideas to increase your understanding on this subject (and your score):
Incluez les informations et résultats suivants dans votre rapport:
ffmpeg
avec l'option fps=25
(voir ci-haut). video
.
L'objectif des discussions dans vos rapports est que vous nous fassiez part de vos réflexions sur vos résultats. Tentez d'être précis(es) dans vos descriptions. Voici quelques idées :
Include the following informations and results in your report:
ffmpeg
with the fps=25
option (see above).video
HTML tag.
The main objective is that you share your thoughts on your results with us. No need to write a novel, just try to be precise. Here are some ideas:
Pour la remise de votre travail, créez un fichier tp3.zip
qui contient:
tp3b/web
. Vos images doivent être dans un dossier tp3/web/images
.tp3/video
.tp3/resultats
.tp3/code
.Finalement, veuillez téléverser votre fichier tp3.zip
sur le portail des cours avant la date limite. La politique des retards mentionnée dans le plan de cours sera appliquée.
For this homework, you must create a tp3.zip
file. In this file you'll put:
tp3/web/
. Your result images inside a folder named tp3/web/images
.tp3/video
.tp3/resultats
.tp3/code
.Finally, you should upload your tp3.zip
file on the "portail des cours" before the deadline. The late submission policy described in the course plan will be applied.
Merci à Alyosha Efros d'avoir créé le TP original qui a servi d'inspiration pour celui-ci!
Many thanks to Alyosha Efros for creating the assignment which inspired this one!