How to blend two images using OpenCV

Computer vision is taking over the world, providing a novel medium for computers to interact with their surroundings. The foundational concept of computer vision has resulted in a wide array of practical applications that have revolutionized numerous industries. Some remarkable examples of these applications include:

  • Facial recognition and authentication: Computer vision technology can identify and verify individuals based on facial features.

  • Autonomous navigation: It enables autonomous robots and drones to navigate and avoid obstacles, facilitating applications in delivery services, agriculture, and exploration.

  • Quality control and inspection: Computer vision systems are used in manufacturing industries to inspect and ensure the quality of products, identifying defects and deviations from standards.

  • Augmented reality (AR): By overlaying digital content onto the real world, computer vision enhances AR experiences, creating interactive and immersive applications in gaming, education, and design.

  • Environmental monitoring: It is employed in ecological research and conservation efforts, helping track wildlife populations, analyze habitats, and detect environmental changes.

These diverse applications, combined with continuous advancements in computer vision technology, are reshaping industries and opening up exciting possibilities for the future.

Program implementation

The code that we have provided is a Python script that allows users to blend two images together using a simple linear blending technique. Linear blending, also known as alpha blending, is a technique used in image processing and computer graphics to combine two images together in a visually appealing manner. The blending process involves mixing the pixel values of two images based on an alpha value, which represents the intensity or transparency of each image in the final result. The user specifies an alpha value between 0.0 and 1.0 to control the blending intensity. The images are then loaded, resized to match each other, and blended using OpenCV's addWeighted() function. The final blended image is displayed.

Libraries used

The library we have used for the code execution is the OpenCV library.

pip install opencv-python

Code

from __future__ import print_function
import cv2 as cv

alpha = 0.5
 
# [load]
src1 = cv.imread(cv.samples.findFile('image_1.png'))
src2 = cv.imread(cv.samples.findFile('image_2.png'))
# [load]
if src1 is None:
 print("Error loading src1")
 exit(-1)
elif src2 is None:
 print("Error loading src2")
 exit(-1)
# [blend_images]
beta = (1.0 - alpha)
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
# [blend_images]
# [display]
cv.imshow('dst', dst)
cv.waitKey(0)
# [display]
cv.destroyAllWindows()

Code Explanation

  • Line 4: This line initializes the variable 'alpha' with a value of 0.5. This value will be used as the blending factor for the two images.

  • Line 7: This line loads the image using OpenCV's imread function, while cv.samples.findFile function is used to locate the image file within OpenCV's sample data. The loaded image is stored in the variable src1 and src2.

  • Line 10 – 15: This line starts an if statement to check if src1 is None, which means the image was not loaded properly, displaying the error and terminating the program.

  • Line 17: This line calculates the value of beta as (1 - alpha). beta will be used as the blending factor for the second image.

  • Line 18: This function cv.addWeighted() is used to blend the two images using the alpha and beta values. The resulting blended image is stored in the variable dst.

  • Line 21 – 24: This line displays the blended image dst in a window with the title 'dst', waiting for any keyboard event resulting in all windows being closed.

Expected output for the blending code above.
Expected output for the blending code above.

Copyright ©2024 Educative, Inc. All rights reserved