Arithmetic Operations on Images

OpenCVOpenCVBeginner
Practice Now

Introduction

Welcome to this tutorial on arithmetic operations on images using OpenCV-Python! In this tutorial, we will cover addition, subtraction, and bitwise operations. By the end of this tutorial, you will have a solid understanding of how to manipulate images using these basic operations.

Image Addition

Image addition is the process of adding pixel values to two images. In OpenCV, the function cv2.add() is used for this purpose.

Open the Python Shell

Open Python Shell in terminal

python3

Load two images

Utilize .imread to read targeted images.

import cv2 as cv2
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')

Add the images

Use .add() to aggregate two images that were read in the previous step.

result = cv2.add(img1, img2)

Save the result

Write the result out.

cv2.imwrite('Image_Addition.jpg', result)

Example

The generated image Image_Addition.jpg looks like:

Image addition result example

Image Blending

This is also image addition, but different weights are given to images in order to give a feeling of blending or transparency. Images are added as per the equation below:

Image blending equation diagram

cv.addWeighted() applies this equation to the image.

Add the images

Use .addWeighted() to apply the equation to images that were read in the previous step.

result = cv2.addWeighted(img1,0.7,img2,0.3,0)

Save the result

Write the result out.

cv2.imwrite('Image_Blending.jpg', result)

Example

The generated image Image_Blending.jpg looks like:

blended image result

Image Subtraction

Image subtraction is used to find the difference between two images. It subtracts the pixel values of one image from another. In OpenCV, you can use the cv2.subtract() function.

Subtract the images

Use .subtract() to subtract the pixel values of image1 from image2.

result = cv2.subtract(img1, img2)

Save the result

Write the result out.

cv2.imwrite('Image_Subtraction.jpg', result)

Example

The generated image Image_Subtraction.jpg looks like:

Image subtraction result example

Bitwise Operations

Bitwise operations are used to manipulate the individual bits of pixel values. OpenCV provides functions like cv2.bitwise_and(), cv2.bitwise_or(), cv2.bitwise_xor(), and cv2.bitwise_not() for performing bitwise operations.

Let's see an example of each operation:

Bitwise AND

Use .bitwise_and() to compute bit-wise AND of bits of pixel values in two images.

bitwise_and = cv2.bitwise_and(img1, img2)

Bitwise OR

Use .bitwise_or() to compute bit-wise OR of bits of pixel values in two images.

bitwise_or = cv2.bitwise_or(img1, img2)

Bitwise XOR

Use .bitwise_xor() to compute bit-wise XOR of bits of pixel values in two images.

bitwise_xor = cv2.bitwise_xor(img1, img2)

Bitwise NOT

Use .bitwise_not() to compute bit-wise NOT of bits of pixel values in two images.

bitwise_not_img1 = cv2.bitwise_not(img1)

Save the results

Write the results out.

cv2.imwrite('Bitwise_AND.jpg', bitwise_and)
cv2.imwrite('Bitwise_OR.jpg', bitwise_or)
cv2.imwrite('Bitwise_XOR.jpg', bitwise_xor)
cv2.imwrite('Bitwise_NOT_Image1.jpg', bitwise_not_img1)

Example

The generated image looks like:

  • AND Bitwise_AND.jpg:
Bitwise AND operation result
  • OR Bitwise_OR.jpg:
Bitwise OR operation result
  • XOR Bitwise_XOR.jpg:
Bitwise XOR operation result
  • NOT Bitwise_NOT_Image1.jpg:
Bitwise NOT operation result

Summary

Congratulations! You have now learned how to perform arithmetic operations on images using OpenCV-Python. You have covered image addition, subtraction, and bitwise operations. These basic operations can be very useful for image processing tasks, such as blending images, detecting changes, and masking specific areas of an image.

Keep practicing and experimenting with different images to get a better understanding of how these operations work. Good luck, and happy coding!