VOOZH about

URL: https://www.geeksforgeeks.org/python/arithmetic-operations-on-images-using-opencv/

⇱ Arithmetic Operations on Images using OpenCV - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Arithmetic Operations on Images using OpenCV

Last Updated : 7 Aug, 2025

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.

Step-by-Step Implementation

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

Step 1: Install Required Libraries and Import necessary Packages

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

Step 2: Upload the Input Images.

The samples used can be downloaded from here.

  • 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: Visualize Input Images.

Output:

Step 4: Perform Operations

1. Image Addition

1.1 Simple Addition

cv2.add(): Adds pixel values with saturation.

Output:

👁 simpleadd
Simple Addition

1.2 Weighted Addition

cv2.addWeighted(): Blends two images by specified weights and an optional scalar.

Parameters:

  • img1, img2: input images
  • 0.7, 0.3: weights (how much each image contributes)
  • 0: gamma (brightness adjustment)

Output:

👁 weightadd
Weighted Addition

2. Image Subtraction

  • cv2.subtract(): Subtracts each pixel in img2 from img1 (clips negative values to 0).
  • Used for change detection, background subtraction, etc.

Output:

👁 substraction
Subtraction

3. Bitwise Operations

3.1 Bitwise AND

cv2.bitwise_and(): Only keeps pixels where both images have bits "on".

Output:

👁 bitwise-and
Bitwise AND

3.2 Bitwise OR

cv2.bitwise_or(): Keeps pixels if either image has a bit "on".

Output:

👁 bitwise-or
Bitwise OR

3.3 Bitwise XOR

cv2.bitwise_xor(): Keeps pixels if only one image (not both) has a bit "on".

Output:

👁 bitwise-xor
Bitwise XOR

3.4 Bitwise NOT

cv2.bitwise_xor(): Keeps pixels if only one image (not both) has a bit "on".

Output:

👁 bitwise-not
Bitwise NOT
Comment