Python OpenCV Overlaying or Blending Two Images

Theory of Computation
In this article, you will learn how to overlay or blend two images, one over another using Python OpenCV.
OpenCV is a free, open source library which is used for computer vision. It provides good support in Machine Learning, Face Recognition, Deep Learning, etc. In the previous articles, we have used the OpenCV to perform different operations on images. This article is also important. The blending of images means mixing two images. The output image is a combination of the corresponding pixel values of the input images. The blending of one image over another is generally used in a variety of computer graphics and image processing applications.
Python OpenCV provides cv2.addWeighted() method to achieve this goal. This method calculates the weighted sum of two arrays. It returns an image in the output, which is a combination of the corresponding pixel values of the input images. It has the following syntax -
cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])
src1: first input array, alpha: weight of the first array elements, src2: second input array of the same size and channel number as src1, beta: weight of the second array elements, gamma: scalar added to each sum, dst: output array that has the same size and number of channels as the input arrays, dtype: optional depth of the output array.
These are the steps taken to overlay one image over another in Python OpenCV. First, we will load both images using the imread() method.
img1 = cv2.imread('forest.png')
img2 = cv2.imread('pigeon.png')
Next, we will blend the image using cv2.addWeighted() method. Here, we have taken 0.5 weights of the first input image and 0.7 weights of the second input image.
dst = cv2.addWeighted(img1,0.5,img2,0.7,0)
The cv2.imshow() method is used to display an image in a window. It accepts 'window name' in the first parameter and 'image name' in the second parameter.
cv2.imshow('Blended Image',dst)
The given line waits for a keyboard button press using cv2.waitKey() method.
cv2.waitKey(0)
Next, use cv2.destroyAllWindows() method to exit the window and destroy all windows.
cv2.destroyAllWindows()
Here is the complete code of overlaying and blending two images using Python OpenCV.
import cv2
import numpy as np

img1 = cv2.imread('forest.png')
img2 = cv2.imread('pigeon.png')
dst = cv2.addWeighted(img1, 0.5, img2, 0.7, 0)

img_arr = np.hstack((img1, img2))
cv2.imshow('Input Images',img_arr)
cv2.imshow('Blended Image',dst)

cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns two windows, one window contains input images and the second contains a blended image.
Reference : https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html

Jan 3
Stateful vs Stateless
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

Dec 29
Best programming language to learn in 2021
In this article, we have mentioned the analyzed results of the best programming language for 2021...

In this article, we have mentioned the analyzed results of the best programming language for 2021...

Dec 20
How is Python best for mobile app development?
Python has a set of useful Libraries and Packages that minimize the use of code...

Python has a set of useful Libraries and Packages that minimize the use of code...

July 18
Learn all about Emoji
In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

Jan 10
Data Science Recruitment of Freshers
In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...






eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.