Skip to content

SoheilaAnsari/Face-swapping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Face Swapping in MATLAB

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.

What's in the box

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

The pipeline (in plain words)

  1. Find the faces — Viola-Jones, via MATLAB's vision.CascadeObjectDetector.
  2. Find the landmarks — eyes and mouth, using Caroline Pacheco's landmark toolkit (see attribution below).
  3. Figure out the angle — compute rotation from the line between the two eyes.
  4. Warp the source face — affine transform so the source eyes line up with the target's eyes at the right scale and rotation.
  5. Blend it in — solve the Poisson equation across the mask region so the source face inherits the target photo's lighting at the boundary.

How to run it

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_V2

When 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).

What I wrote vs. what I used

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 toolkit
  • poisson_real.m — Poisson editing implementation
  • poisson_real_V2.m — cleaner annotated version of the above
  • poisson_real_mix_gradients.m — the "mixing gradients" variant (Pérez et al. §3.3)
  • eyematch.m — template-matching helper for eye location
  • test_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.

About

A classical face-swap pipeline in MATLAB (Viola-Jones + landmarks + affine warp + Poisson blending). Course project, 2017.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages