Saturday, June 25, 2016

Blending of Images using Opencv

Posted by Md. Hanif Ali Sohag on Saturday, June 25, 2016 in | No comments
Blending of two images
In this tutorial we will learn how to blend two images together.

Operation:

Code:


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;


int main()
{
 double alpha = 0.5; double beta; double input;
 Mat src1, src2, dst;
 /// Ask the user enter alpha
   cout << " Simple Linear Blender " << endl;
   cout << " ----------------------- " <<endl;
   cout << " * Enter alpha [0-1]: ";
   cin >> input;
 /// We use the alpha provided by the user if it is between 0 and 1
 if (input >= 0.0 && input <= 1.0)
 {
  alpha = input;
 }
 /// Read image ( same size, same type )
 src1 = imread("c://test//blend1.jpg");
 src2 = imread("c://test//blend2.jpg");
 if (!src1.data) 
 {
  cout<<" Error loading src1";
  return -1; 
 }

 if (!src2.data) 
 {
  cout<<" Error loading src2 \n";
  return -1;
 }
 /// Create Windows

 namedWindow("Blend1",1);
 imshow("Blend1", src1);


 namedWindow("Blend2", 1);
 imshow("Blend2", src2);

 namedWindow("Linear Blend", 1);
 beta = (1.0 - alpha);
 addWeighted(src1, alpha, src2, beta, 0.0, dst);

 imshow("Linear Blend", dst);
 waitKey(0);
 return 0;
}

Output:

Blending of two images

0 comments:

Post a Comment