Tuesday , December 1 2026
Arithmetic Operations on Images using OpenCV

Arithmetic Operations on Images using OpenCV

Updated: 28 November 2026Level: BeginnerReading time: about 6 minutesOpenCV 4.x

Because images are arrays of numbers, you can add and subtract them. Image arithmetic is used to blend two pictures, add watermarks, adjust brightness, and find what changed between two frames (motion detection).

What You Will Learn

  • Adding two images with cv2.add()
  • Blending images with cv2.addWeighted()
  • Subtracting images with cv2.subtract()
  • Why OpenCV arithmetic is different from NumPy arithmetic

Prerequisites

Python 3 with OpenCV and NumPy installed, and two images ocv3.jpg and ocv4.jpg of the same size in your script folder.

Adding Two Images

Example
import cv2
import numpy as np

image1 = cv2.imread('ocv3.jpg')
image2 = cv2.imread('ocv4.jpg')

weightedSum = cv2.add(image1, image2)

cv2.imshow('Added Image', weightedSum)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation: cv2.add() adds the two images pixel by pixel. If a sum goes above 255 it is clipped to 255 (this is called saturation), so bright areas turn white.

OpenCV add vs NumPy +

Example
x = np.uint8([250])
y = np.uint8([10])
print(cv2.add(x, y))   # [[255]]  saturated
print(x + y)           # [4]      wraps around

Explanation: NumPy wraps around after 255 (250 + 10 = 260 becomes 4), which creates strange colors. Always use cv2.add() for images.

Blending Two Images

Example
weightedSum = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
cv2.imshow('Blended Image', weightedSum)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation: The formula is result = image1 × alpha + image2 × beta + gamma. With 0.5 and 0.5 both images are equally visible. Try 0.7 and 0.3 to make the first image stronger.

Parameter Meaning
image1, 0.5 First image and its weight (alpha)
image2, 0.5 Second image and its weight (beta)
0 Gamma, a value added to every pixel to brighten

Subtracting Images

Example
diff = cv2.subtract(image1, image2)
cv2.imshow('Subtracted Image', diff)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation: Values below 0 are clipped to 0 (black). Where the two images are the same, the result is black, so subtraction shows what is different. For motion detection, cv2.absdiff() is usually better because it keeps differences in both directions.

Common Errors

Problem Cause and fix
img is None / error (-215:Assertion failed) !_src.empty() OpenCV could not find the file. cv2.imread() does not raise an error for a wrong path, it returns None. Check the file name and run the script from the folder that contains the image.
ModuleNotFoundError: No module named 'cv2' Install OpenCV: pip install opencv-python
Window does not open in Jupyter / Google Colab cv2.imshow() needs a desktop. In Colab use from google.colab.patches import cv2_imshow, or display with Matplotlib.
error: (-209:Sizes of input arguments do not match) Both images must have the same size and number of channels. Resize one with image2 = cv2.resize(image2, (image1.shape[1], image1.shape[0])).

Practice Exercises

  1. Make a fade effect by looping alpha from 0 to 1 with addWeighted.
  2. Increase brightness by adding a constant: cv2.add(img, np.full(img.shape, 50, np.uint8)).
  3. Use cv2.absdiff on two webcam frames to detect motion.

Interview Questions

Q: What is the difference between cv2.add and NumPy addition?
cv2.add saturates at 255; NumPy uint8 addition wraps around (modulo 256).

Q: What formula does addWeighted use?
dst = src1·α + src2·β + γ.

Summary

Use cv2.add and cv2.subtract for safe pixel arithmetic and cv2.addWeighted to blend. The images must be the same size.

Next: Color Channels of an Image · Access Camera Using OpenCV

About Machine Learning

Check Also

Image Classification in Python: From SVM to CNN

Image classification with tested scikit-learn models on handwritten digits (SVM 99%), error analysis, the flat-pixel weakness and a Keras CNN for real images.