How to do Face Morphing using OpenCV

Share

Its been some time since I posted on this website. And I am sorry to myself for not keeping up the promise I made to myself in the last post. I was locked out of this website for some time and I had nothing original to share. Thanks to my friend and colleague Mahesh Sonawane (He has some good PHP, MySQL and WordPress skills), I got my access back to this WordPress site. Now that I do have something (don’t expect a lot) original, without much ado, here it is:

I’ve been tinkering around with OpenCV 2.45 since some time now and there was this tutorial showcasing (adding) Alpha-blending of two images using OpenCV by Ana Huamán. (Btw, these are some very very good and detailed tutorials on OpenCV, even if you are a beginner or an expert who needs a revisit to some concepts).

My mind has this amazing quality to almost always think spontaneously about different possibilities whenever I am learning something new or watching something new unfold before my eyes. So, when I was writing (rather copying by typing, which is the only way I can understand someone else’s code. So, no point copying others’ source code with selection and Ctrl+C – Ctrl+V, you are NOT gonna understand it, at least not me. See and write it, every word and comma and semi-colon, tinker with it, apply breakpoints and change the default behavior and values, draw some conclusions, only then you can claim to understand it at least partially!). So, again I digressed, see!

So, when I was writing the code for this tutorial in my own C++ file, I happened to see the result of this tutorial, which was:

OpenCV Adding (Blending) images result

I thought to myself, this was quite bland, lets try this on human faces. I tried with these 2 faces:

f1

Face_1

Face_2

Face_2

to achieve the image below:

Alpha_Blending of 2 faces

Alpha_Blending of 2 faces

Now, I thought I could do more, something like you’ve seen in some advertisements where one face morphs and just smoothly blends into another. I love those effects! e.g.:

Face morph-blending

Though, not as smooth as I would have liked, I ended up making this:

Face Morphing using OpenCV

Face Morphing using OpenCV

The code consists of a simple ‘for’ loop making this animation possible.

The crux of the code:

 for(int k = 0; k <= 100; ++k )
 {
     alpha = (double)k/100;
     beta = 1 − alpha;

     addWeighted( src2, alpha, src1, beta, 0.0, dst );

     imshow( "Linear Blend", dst );

     cvWaitKey(50);
 }

You may see it here as CV_Alpha_Blending on GitHub. You may also get the executable file  and other files here on my Google Drive. Use it by having 2 images in the same folder as the .exe file and renaming the images to f1.jpg and f2.jpg. Remember, the images need to be of same size with approximately the same locations of eyes and other face-features to have good results.

Future Scope: Index the face feature-points and warp those points while blending.

Suggestions, improvements and comments welcome!

Have fun face-morphing! 🙂

 

How to reach us?

Global Popularity of StomatoBot.com since inception! 🙂

Get in touch! Let us know what you think about this article. 🙂

Share
  • Kitthing

    There is an error, Error loading src1. What does this mean? My files are named f1 and f2, type jpg. But I don’t really get, what !src1.data is doing.

    • Anand Muglikar

      Thanks for following through Kitthing! 🙂 And I’m glad you asked the question.

      Basically what !src1.data does is checks out if the image file loaded into the OpenCV Mat object using imread function, has indeed valid image data or not.

      Now, if you get an error loading src1 that just means that your image file f1.jpg is not being loaded into the program either because:

      the file doesn’t exist OR
      exists in another folder OR
      is corrupt or empty image file

      Check for these conditions and you’re good to go.

      Do let me know if you’re getting this error on line no. 26 or 29 of this code:

      https://github.com/muglikar/testCV/blob/master/CV_Alpha_Blending

      and if you need further help, I’d be more than happy if I can be useful! 🙂

  • Sanket

    Nicely compiled. My C++ skills have gone rusty (a lot, I would say) and have never seen the second header file you used, but good work. However, I would like to know that the namespaces you used, are they system declared or you created them? Same question for the addweighted function that you are calling……

    • Hi Sanket, nice to see a reply from our long lost friend! 😛

      The second header file highgui.h is for High-level GUI and Media IO in OpenCV. The namespaces std and cv are pre-built in VC++ and OpenCV respectively. We may edit the OpenCV one as we have the source for it. The addweighted function too is a function already defined in core.hpp of OpenCV. I just tweaked it into a loop for faces. 🙂

  • P A Kulkarni

    Good work Anand. Continue… All the Best!

    • Thank you for the appreciation and encouragement Ma’am! 🙂

  • Abhishiktha

    Nice informative post Anand. Keep posting

    • Thank you for the encouragement and appreciation Abhishiktha! 🙂