Addition and Blending of images using OpenCv in Python

PythonProgrammingServer Side Programming

We know that when we solve any image related problem, we have to take a matrix. The matrix content will vary depending upon the image type - either it would be a binary image(0, 1), gray scale image(0-255) or RGB image(255 255 255). So if we want to add of two images then that means very simple we have to add respective two matrices.

In OpenCV library, we have a function cv2.add() to add the images. But for image addition the size of the two images should be same.

Addition of two images

import cv2
# Readingour Image1
my_firstpic = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/mqdefaultILPT6GSR.jpg', 1)
cv2.imshow('image', my_firstpic)
# Readingour Image2
my_secpic = cv2.imread('C:/Users/Satyajit/Pictures/west bengal/bishnupur/pp.jpg', 1)
img = cv2.add(my_firstpic,my_secpic)
cv2.waitKey(0)
cv2.distroyAllWindows()

Output

Adding Images

Blending of two images

cv2.addWeighted() function is used for blending of two images.

Example code

import cv2
# Read our Image1
My_first = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/mqdefaultILPT6GSR.jpg', 1)
# Reading ourImage2
My_second = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/pp.jpg', 1)
# Blending the images with 0.3 and 0.7
My_img = cv2.addWeighted(My_first, 0.3, My_second, 0.7, 0)
# Show the image
cv2.imshow('image', My_img)
# Wait for a key
cv2.waitKey(0)
# Destroy all the window open
cv2.distroyAllWindows()

Output

Blending Images
raja
Published on 23-Nov-2018 05:25:15
Advertisements