Life2Coding
Blending of Images using OpenCV

Blending of two images

A really powerful function for collaging and compositing in OpenCV is addWeighted(). When we want to combine photos, we combine layer masks and gradients, it’s laughably easy to create stunning looking composited that are actually very easy to do. We can even add logos to our images.  By applying this method, a watermark can also be applied to the desired image.

This method can also be helpful when you are trying to apply a transparent mask to the image. So by calling the OpenCV addWeighted() method these things become very easy.

In this tutorial, we will learn how to blend two images together.

Operation:

jjjf Blending of Images using OpenCV

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 Blending of Images using OpenCV

 

 

life2coding_icon [] Blending of Images using OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.