Addition and Blending of images using OpenCv in Python

OpenCV allows us to perform mathematical operations on images by treating them as matrices. When working with images, we deal with different matrix types: binary images (0, 1), grayscale images (0-255), or RGB images (0-255 for each channel). To add two images, we simply add their corresponding matrices element-wise.

OpenCV provides the cv2.add() function for image addition and cv2.addWeighted() for blending. Both operations require images of the same dimensions.

Addition of Two Images

The cv2.add() function performs pixel-wise addition of two images. If the sum exceeds 255, it gets clipped to 255 (saturation arithmetic) ?

import cv2
import numpy as np

# Create sample images for demonstration
img1 = np.zeros((300, 400, 3), dtype=np.uint8)
img1[50:150, 50:200] = [100, 150, 200]  # Blue rectangle

img2 = np.zeros((300, 400, 3), dtype=np.uint8)  
img2[100:200, 150:300] = [200, 100, 50]  # Orange rectangle

# Add the two images
result = cv2.add(img1, img2)

# Display the images
cv2.imshow('Image 1', img1)
cv2.imshow('Image 2', img2)
cv2.imshow('Added Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Using Different Images

When working with actual image files, ensure both images have identical dimensions ?

import cv2

# Read two images
img1 = cv2.imread('/path/to/first_image.jpg')
img2 = cv2.imread('/path/to/second_image.jpg')

# Resize images to same dimensions if needed
height, width = 300, 400
img1 = cv2.resize(img1, (width, height))
img2 = cv2.resize(img2, (width, height))

# Add the images
result = cv2.add(img1, img2)

cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Blending of Two Images

The cv2.addWeighted() function creates a weighted sum of two images, allowing you to control the transparency of each image. This is more flexible than simple addition ?

Syntax

cv2.addWeighted(src1, alpha, src2, beta, gamma)

Parameters

  • src1 ? First input image
  • alpha ? Weight for the first image (0.0 to 1.0)
  • src2 ? Second input image
  • beta ? Weight for the second image (0.0 to 1.0)
  • gamma ? Scalar added to each sum (usually 0)

Example

import cv2
import numpy as np

# Create sample images
img1 = np.zeros((300, 400, 3), dtype=np.uint8)
img1[:, :] = [255, 0, 0]  # Blue image

img2 = np.zeros((300, 400, 3), dtype=np.uint8)
img2[:, :] = [0, 255, 0]  # Green image

# Blend images with weights (30% img1, 70% img2)
blended = cv2.addWeighted(img1, 0.3, img2, 0.7, 0)

cv2.imshow('Image 1 (Blue)', img1)
cv2.imshow('Image 2 (Green)', img2) 
cv2.imshow('Blended Result', blended)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comparison

Method Function Result Use Case
Addition cv2.add() Values clipped at 255 Simple combination
Blending cv2.addWeighted() Weighted combination Transparency effects

Conclusion

Use cv2.add() for simple image addition with saturation arithmetic. Use cv2.addWeighted() for blending images with custom transparency levels, which provides more control over the final appearance.

Updated on: 2026-03-25T05:02:50+05:30

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements