Arithmetic Operations on Images using OpenCV in Python

In this tutorial, we are going to learn about the arithmetic operations on Images using OpenCV. We can apply operations like addition, subtraction, Bitwise Operations, etc. Let's see how we can perform operations on images.

We need the OpenCV module to perform operations on images. Install the OpenCV module using the following command in the terminal or command line.

pip install opencv-python

Image Addition

We can add two images using the cv2.addWeighted() function. It takes five arguments: two images, the weights for each image, and a scalar value added to the final result.

Syntax

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

Where:

  • src1 ? First input image
  • alpha ? Weight of the first image
  • src2 ? Second input image
  • beta ? Weight of the second image
  • gamma ? Scalar added to each sum

Example

import cv2
import numpy as np

# Create sample images for demonstration
image_one = np.zeros((300, 400, 3), dtype=np.uint8)
image_one[:, :200] = [255, 0, 0]  # Red rectangle

image_two = np.zeros((300, 400, 3), dtype=np.uint8)
image_two[:, 200:] = [0, 255, 0]  # Green rectangle

# Adding two images with equal weights
result_image = cv2.addWeighted(image_one, 0.5, image_two, 0.5, 0)

# Display the images
cv2.imshow('Image One', image_one)
cv2.imshow('Image Two', image_two)
cv2.imshow('Added Image', result_image)

# Wait for key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()

Image Subtraction

We have a method called cv2.subtract() to perform subtraction on two images. This operation subtracts pixel values of one image from another.

Syntax

cv2.subtract(src1, src2)

Example

import cv2
import numpy as np

# Create sample images
image_one = np.ones((300, 400, 3), dtype=np.uint8) * 200
image_two = np.ones((300, 400, 3), dtype=np.uint8) * 100

# Subtracting two images
result_image = cv2.subtract(image_one, image_two)

# Display the result
cv2.imshow('Original Image', image_one)
cv2.imshow('Image to Subtract', image_two)
cv2.imshow('Subtracted Image', result_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Bitwise Operations

OpenCV provides bitwise operations like AND, OR, XOR, and NOT that work on binary images or masks.

Example

import cv2
import numpy as np

# Create two binary images
img1 = np.zeros((300, 300), dtype=np.uint8)
cv2.rectangle(img1, (50, 50), (200, 200), 255, -1)

img2 = np.zeros((300, 300), dtype=np.uint8)
cv2.circle(img2, (150, 150), 100, 255, -1)

# Bitwise operations
bitwise_and = cv2.bitwise_and(img1, img2)
bitwise_or = cv2.bitwise_or(img1, img2)
bitwise_xor = cv2.bitwise_xor(img1, img2)
bitwise_not = cv2.bitwise_not(img1)

# Display results
cv2.imshow('AND', bitwise_and)
cv2.imshow('OR', bitwise_or)
cv2.imshow('XOR', bitwise_xor)
cv2.imshow('NOT', bitwise_not)

cv2.waitKey(0)
cv2.destroyAllWindows()

Key Points

  • Images must have the same dimensions for arithmetic operations
  • cv2.addWeighted() allows blending images with different weights
  • cv2.subtract() performs pixel?wise subtraction
  • Bitwise operations are useful for masking and combining binary images
  • Results are clipped to valid pixel value ranges (0?255)

Conclusion

Arithmetic operations in OpenCV allow you to combine, modify, and analyze images mathematically. Use addWeighted() for blending images, subtract() for difference detection, and bitwise operations for masking operations.

Updated on: 2026-03-25T06:42:50+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements