A face-swap pipeline I built for my image processing course back in 2017.
You give it two photos. It finds the face in each one, figures out where the eyes and mouth are, rotates and scales one face to match the other's pose, and then blends them together using Poisson image editing.
face-swapping/
├── src/ # all the MATLAB code
├── Images/ # put your own test images here
├── README.md # you are here
├── CITATIONS.md # academic references
└── LICENSE # MIT for my code
- Find the faces — Viola-Jones, via MATLAB's
vision.CascadeObjectDetector. - Find the landmarks — eyes and mouth, using Caroline Pacheco's landmark toolkit (see attribution below).
- Figure out the angle — compute rotation from the line between the two eyes.
- Warp the source face — affine transform so the source eyes line up with the target's eyes at the right scale and rotation.
- Blend it in — solve the Poisson equation across the mask region so the source face inherits the target photo's lighting at the boundary.
You'll need MATLAB with the Computer Vision Toolbox (for the Viola-Jones detector and imfreehand).
% from MATLAB, with the project root as your current folder:
cd src
poisson_real_V2When the figure window pops up, hold left-click and draw around the face area you want to copy from the source. Release, and the blending takes over.
The scripts expect test images at ../Images/ relative to src/. Drop a couple of face photos in there first; anything front-facing works (LFW faces are great).
My code (in src/):
Affine_fn.m— the affine warping pipeline (Step 3–4 above)my_detect_landmark.m— wrapper that returns clean(left_eye, right_eye)coordinates from the landmark toolkitpoisson_real.m— Poisson editing implementationpoisson_real_V2.m— cleaner annotated version of the abovepoisson_real_mix_gradients.m— the "mixing gradients" variant (Pérez et al. §3.3)eyematch.m— template-matching helper for eye locationtest_smr.m— quick affine transform sanity check
Code by other people that I used (also in src/, fully attributed):
detectFacialRegions.m,eyesProcessing.m,mouthProcessing.m,detectLandmarks.m,calDistance.m,showsLandmarks.m,Test_landmark.m— facial landmark detection toolkit by Caroline Pacheco do E.Silva (2014), shared on MATLAB File Exchange.
The full citation list (papers I implemented or referenced) is in CITATIONS.md.