Arithmetic Operations on Images using OpenCV

Last Updated : 1 Jul, 2026

Arithmetic operations such as addition, subtraction and bitwise operations (AND or, NOT, XOR) are fundamental techniques in image processing with OpenCV. These operations allow for the enhancement, analysis and transformation of image characteristics, making them essential for tasks like image clarification, thresholding, dilation and more.

Let's see the step by step implementation of Arithmetic operations,

Step 1: Install and Import Required Libraries

Before performing arithmetic operations, install and import the required libraries.

Python
!pip install opencv - python matplotlib

import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files

Explanation:

  • opencv: Core library for image processing and computer vision.
  • matplotlib.pyplot: For displaying images inside the notebook .
  • numpy: Efficient array operations .

Step 2: Load the Input Images

Next, load the images on which the arithmetic operations will be performed. Make sure both images are available in the current working directory.

Note: The samples used can be downloaded from here.

Python
img1 = cv2.imread('input1.png')
img2 = cv2.imread('input2.png')

Explanation:

  • files.upload() opens a dialog to pick files from our device.
  • cv2.imread() reads an image from disk and loads it as a NumPy array (in BGR color ordering by default).

Step 3: Display the Input Images

Before applying arithmetic operations, it is useful to display both images side by side for comparison. If the images have different dimensions, resize one image so that both have the same size.

Python
if img1.shape != img2.shape:
    img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

line_thickness = 5
height = img1.shape[0]
line = np.full((height, line_thickness, 3), (0, 0, 255), dtype=np.uint8)

side_by_side = np.hstack((img1, line, img2))

side_by_side_rgb = cv2.cvtColor(side_by_side, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(12, 6))
plt.imshow(side_by_side_rgb)
plt.title('input1 input2')
plt.axis('off')
plt.show()

Output

Explanation:

  • img1.shape != img2.shape checks whether the two images have different dimensions.
  • cv2.resize() resizes the second image to match the dimensions of the first image.
  • np.full() creates a vertical red line that acts as a separator between the images.
  • np.hstack() combines both images horizontally into a single image.
  • cv2.cvtColor() converts the image from BGR to RGB format for correct display with Matplotlib.
  • plt.imshow() displays the combined image, allowing you to visually compare both inputs before applying arithmetic operations.

Step 4: Apply Arithmetic Operations

1. Image Addition

Image addition combines the pixel values of two images. OpenCV automatically handles overflow by limiting pixel values to the valid range (0–255).

Simple Addition: cv2.add() adds corresponding pixel values from both images.

Python
added = cv2.add(img1, img2)
added_rgb = cv2.cvtColor(added, cv2.COLOR_BGR2RGB)

plt.imshow(added_rgb)
plt.title('Addition (cv2.add)')
plt.axis('off')
plt.show()

Output

simpleadd
Simple Addition

Explanation:

  • cv2.add() adds pixel values from both images.
  • Values greater than 255 are clipped to 255.
  • Commonly used for increasing image brightness or combining images.

2. Weighted Addition

Weighted addition blends two images by assigning a contribution weight to each image.

Python
weighted = cv2.addWeighted(img1, 0.7, img2, 0.3, 0)
weighted_rgb = cv2.cvtColor(weighted, cv2.COLOR_BGR2RGB)

plt.imshow(weighted_rgb)
plt.title('Weighted Addition (cv2.addWeighted)')
plt.axis('off')
plt.show()

Output

weightadd
Weighted Addition

Explanation:

  • 0.7 determines the contribution of img1.
  • 0.3 determines the contribution of img2.
  • The last parameter (0) is the gamma value used for brightness adjustment.
  • Frequently used for image blending and transparency effects.

3. Image Subtraction

Image subtraction removes pixel values of one image from another.

Python
subtracted = cv2.subtract(img1, img2)
subtracted_rgb = cv2.cvtColor(subtracted, cv2.COLOR_BGR2RGB)

plt.imshow(subtracted_rgb)
plt.title('Subtraction (cv2.subtract)')
plt.axis('off')
plt.show()

Output

substraction
Subtraction

Explanation:

  • cv2.subtract() subtracts each pixel in img2 from the corresponding pixel in img1.
  • Negative values are automatically clipped to 0.
  • Commonly used for change detection and background subtraction.

4. Bitwise AND

Bitwise AND keeps only the pixels that are active in both images.

Python
and_img = cv2.bitwise_and(img1, img2)
and_img_rgb = cv2.cvtColor(and_img, cv2.COLOR_BGR2RGB)

plt.imshow(and_img_rgb)
plt.title('Bitwise AND')
plt.axis('off')
plt.show()

Output

bitwise-and
Bitwise AND

Explanation: Pixels are retained only when corresponding bits are set in both images, which is commonly used for masking operations.

5. Bitwise OR

Bitwise OR keeps pixels that are active in either image.

Python
or_img = cv2.bitwise_or(img1, img2)
or_img_rgb = cv2.cvtColor(or_img, cv2.COLOR_BGR2RGB)

plt.imshow(or_img_rgb)
plt.title('Bitwise OR')
plt.axis('off')
plt.show()

Output

bitwise-or
Bitwise OR

Explanation: A pixel is retained if it exists in either image. Useful for combining image regions.

6. Bitwise XOR

Bitwise XOR keeps pixels that differ between the two images.

Python
xor_img = cv2.bitwise_xor(img1, img2)
xor_img_rgb = cv2.cvtColor(xor_img, cv2.COLOR_BGR2RGB)

plt.imshow(xor_img_rgb)
plt.title('Bitwise XOR')
plt.axis('off')
plt.show()

Output

bitwise-xor
Bitwise XOR

Explanation: Pixels are retained only when the corresponding bits are different. Useful for highlighting differences between images.

7. Bitwise NOT

Bitwise NOT inverts all pixel values in an image.

Python
not_img = cv2.bitwise_not(img1)
not_img_rgb = cv2.cvtColor(not_img, cv2.COLOR_BGR2RGB)

plt.imshow(not_img_rgb)
plt.title('Bitwise NOT (Image 1)')
plt.axis('off')
plt.show()

Output

bitwise-not
Bitwise NOT

Explanation: cv2.bitwise_not() inverts every pixel value and dark pixels become bright and bright pixels become dark. Often used for image inversion and mask generation.

Comment