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:
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:
Latest posts by Life2Coding (see all)
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022