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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL opencv(("`OpenCV`")) -.-> opencv/BasicOperationsGroup(["`Basic Operations`"]) opencv/BasicOperationsGroup -.-> opencv/imread("`Image Read`") opencv/BasicOperationsGroup -.-> opencv/add("`Image Addition`") opencv/BasicOperationsGroup -.-> opencv/add_weighted("`Image Blending`") opencv/BasicOperationsGroup -.-> opencv/bitwise_and("`Bitwise AND`") opencv/BasicOperationsGroup -.-> opencv/bitwise_or("`Bitwise OR`") opencv/BasicOperationsGroup -.-> opencv/subtract("`Image Subtraction`") opencv/BasicOperationsGroup -.-> opencv/bitwise_not("`Bitwise NOT`") opencv/BasicOperationsGroup -.-> opencv/bitwise_xor("`Bitwise XOR`") opencv/BasicOperationsGroup -.-> opencv/imwrite("`Image Write`") subgraph Lab Skills opencv/imread -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/add -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/add_weighted -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/bitwise_and -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/bitwise_or -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/subtract -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/bitwise_not -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/bitwise_xor -.-> lab-38502{{"`Arithmetic Operations on Images`"}} opencv/imwrite -.-> lab-38502{{"`Arithmetic Operations on Images`"}} end

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:

example_image_image_addition

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:

example_image_image_blending_equation

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:

example_image_image_blending

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:

example_image_image_subtraction

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:
example_image_bitwise_operations1
  • OR Bitwise_OR.jpg:
example_image_bitwise_operations2
  • XOR Bitwise_XOR.jpg:
example_image_bitwise_operations3
  • NOT Bitwise_NOT_Image1.jpg:
example_image_bitwise_operations4

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!

Other OpenCV Tutorials you may like