Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (45 sloc) 1.57 KB
// OpenCV Alpha Blending Tutorial modified for Face Morphing
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
double alpha = 0.5, beta, input;
Mat src1, src2, dst;
// Ask user to enter alpha
cout << " Simple Linear Blender " << endl;
cout << "-----------------------" << endl;
cout << " Enter alpha [0-1]: ";
cin >> input;
// We use the alpha provided by user if its between 0 and 1
if( input >= 0.0 && input <= 1.0 )
alpha = input;
// Read image (same size, same type )
src1 = imread("f1.jpg");//Linux.jpg");//atom.jpg");//"Linux.jpg"); //f1.jpg
src2 = imread("f2.jpg");//Windows.jpg");//fool.jpg");//"Windows.jpg"); //f2.jpg
if( !src1.data ) { printf("Error loadind src1 \n"); return -1;}
if( !src2.data ) { printf("Error loadind src2 \n"); return -1;}
// Create windows
//namedWindow("Windows Logo", CV_WINDOW_AUTOSIZE );
//namedWindow("Linux Logo", CV_WINDOW_AUTOSIZE );
namedWindow("Linear Blend", CV_WINDOW_AUTOSIZE );
//imshow( "Linux Logo", src1 );
//imshow( "Windows Logo", src2 );
// Uncomment the below 'for' loop for morphing effect
for(int k = 0; k <= 100; ++k )
{
alpha = (double)k/100;
beta = 1 - alpha;
//double t = (double)getTickCount(); // To calculate time reqd.
addWeighted( src2, alpha, src1, beta, 0.0, dst );
//t = ((double)getTickCount() - t)/getTickFrequency();
//cout << "Linear blnding took " << t*1000 << " milliseconds." << endl;
//imwrite("CV_Blending_dst_Gamma0.jpg", dst);
imshow( "Linear Blend", dst );
cvWaitKey(50);
}
cvWaitKey();
return 0;
}