Python OpenCV Overlaying or Blending Two Images
In this article, you will learn how to overlay or blend two images, one over another using the Python OpenCV.
OpenCV is a free, open source library that is used for computer vision. It provides good support for 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. Overlaying an image over another refers to the method involved with copying the image information of one image over the other. 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 the 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 with 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.
Code Explanation: OpenCV Overlay or Blend Two Images
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 the 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.
The given line waits for a keyboard button press using the cv2.waitKey() method.
Next, use the cv2.destroyAllWindows() method to exit the window and destroy all windows.
Complete Code: OpenCV Overlay or Blend Two Images
Here is the complete code for overlaying and blending two images using the 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
Related ArticlesHuman Body Detection Program In Python OpenCV
Detect Specific Color From Image using Python OpenCV
Capture a video in Python OpenCV and save
Arithmetic Operations on Images using Python OpenCV
Adaptive Thresholding in Python OpenCV
Python OpenCV Erosion and Dilation
Bitwise and OpenCV
OpenCV color detection
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display mongodb data in html page