OpenCV Arithmetic Operations on Images

Parthiban Marimuthu Last Updated : 24 Aug, 2022
5 min read

This article was published as a part of the Data Science Blogathon.

Introduction to OpenCV

The images can be subjected to arithmetic operations such as addition, subtraction, and bitwise operations (AND, OR, NOT, XOR). These operations can help to improve the properties of the input images. Image arithmetic is necessary for analyzing the properties of the input image. The operated images can then be used as an enhanced input image, and many more operations can be applied to the image for clarification, thresholding, dilating, and so on.

Image arithmetic is the application of one or more images to one of the standard arithmetic operations or a logical operator. The operators are applied pixel by pixel, so the value of a pixel in the output image is determined solely by the values of the corresponding pixels in the input images. As a result, the images must usually be the same size. When adding a constant offset to an image, one of the input images may be a constant value.

Although image arithmetic is the most basic form of image processing, it has many applications. One significant advantage of arithmetic operators is that the process is straightforward and thus quick.

Addition of Image

In its most basic form, this operator takes two identically sized images as input and outputs a third image of the same size as the first two, with each pixel value being the sum of the values of the corresponding pixel in each of the two input images. More advanced versions allow the combination of multiple images in a single operation.
A common variant of the operator simply allows the addition of a specified constant to each pixel. Using the function cv2.add(), we can add two images . This adds up the image pixels in the two images directly.

Syntax: cv2.add(image1, image2)

However, adding pixels is not an ideal situation. As a result, we employ cv2.addweighted (). Remember that both input images must be equal in shape and color channels.

Syntax: cv2.add Weighted(image1, weight1, Image2, weight2, gammaValue)

Parameters:

  1. image 1: First Image Array Input
  2. weight 1: The weight of the first image elements in the input image that will be applied to the final image.
  3. Image 2: Second Image Array Input
  4. weight 2: The weight of the second input image elements to be applied to the gamma of the final image.
  5. Value: Light measurement.

Code for Addition

import cv2
import numpy as np
image1 = cv2.imread('input1.jpg')
image2 = cv2.imread('input2.jpg')
weightedSumadd = cv2.addWeighted(image1, 0.6, image2, 0.4, 0)
cv2.imshow('Weighted Image', weightedSumadd)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output image will be,

output image

 

Subtraction of Image

The pixel subtraction operator takes two images as input and outputs the third image with pixel values that are simply the first image’s pixel values minus the corresponding pixel values from the second image. Using a single image as input is common and subtracting a constant value from all the pixels is common. Instead of the straightforward signed output, some versions of the operator will simply output the absolute difference between pixel values.

Syntax:  cv2.subtract(image1, image2)

Parameters:

  1. image 1: First Image Array Input (Single-channel, 8-bit or floating-point)
  2. image 2: Second Image Array Input (Single-channel, 8-bit or floating-point)

Input images 

OpenCV

Code :

import cv2
import numpy as np
image1 = cv2.imread('input1.jpg')
image2 = cv2.imread('input2.jpg')
sub = cv2.subtract(image1, image2)
cv2.imshow('Subtracted Image', sub)
cv2.waitKey(0)

The output subtracted image will be,

OpenCV

Bitwise Operations

Bitwise operations are used in image manipulation to extract important parts. The following Bitwise operations are used in this article:

  1. AND
  2. OR
  3. NOT
  4. XR

Bitwise operations are also useful for image masking. These operations can be used to enable image creation. These operations can help to improve the properties of the input images.
NOTE: Bitwise operations should only be performed on input images of the same dimensions.

AND Bitwise Operation of Image

The AND operator (and the NAND operator in a similar fashion) typically takes two binary or integer graylevel images as input and produces a third image whose pixel values are just those of the first image ANDed with the corresponding pixels from the second. This operator can be modified to produce the output by taking a single input image and ANDing each pixel with a predetermined constant value.

Syntax: cv2.bitwise_and(Image1, Image2, destination, mask)

Parameters:

  1. Image1: First Input Image numpy array
  2. Image1: Second Input Image numpy array
  3. destination: Output array
  4. mask: Operation mask image

Code :

import cv2
import numpy as np
img1 = cv2.imread('input1.png') 
img2 = cv2.imread('input2.png')
dest_and = cv2.bitwise_and(img2, img1, mask = None)
cv2.imshow('Bitwise And', dest_and)
cv2.waitKey(0)

OR Bitwise Operation of Image

The OR operator typically takes two binary or greyscale images as input and outputs a third image whose pixel values are the first image’s pixel values ORed with the corresponding pixels from the second. A variant of this operator takes a single input image and ORs each pixel with a constant value to generate the output.

Syntax: cv2.bitwise_or(source1, source2, destination, mask)

Parameters:

  1. source1: First Input numpy Image array
  2. source2: Second Input numpy Image array
  3. destination: Output array image
  4. mask: Operation mask, input / output 8-bit single-channel mask.

Code :

import cv2
import numpy as np
img1 = cv2.imread('input1.png') 
img2 = cv2.imread('input2.png')
dest_or = cv2.bitwise_or(img1, img2, mask = None)
cv2.imshow('Bitwise OR', dest_or)
cv2.waitKey(0)

NOT Bitwise Operation of Image

Logical NOT, also known as invert, is an operator that takes a binary or grayscale image as input and generates its photographic negative.

Syntax: cv2.bitwise_not(Image1,Destination, mask)

Parameters:

  1. Image1: Input Image array.
  2. Destination: Output array image
  3. mask: Operation mask

Code :

import cv2
import numpy as np
img1 = cv2.imread('input1.png') 
dest_not = cv2.bitwise_not(img1, mask = None)
cv2.imshow('Bitwise Not', dest_not)
cv2.waitKey(0)

XOR Bitwise Operation of Image

The operation is carried out simply and in a single pass. It is critical that all of the input pixel values being processed have the same number of bits, or else unexpected results may occur. When the pixel values in the input images are not simple 1-bit numbers, the XOR operation is typically (but not always) performed bitwise on each corresponding bit in the pixel values.

Syntax: cv2.bitwise_xor(source1, source2, destination, mask)

Parameters:

  1. source1: First Input Image array (Single-channel, 8-bit or floating-point)
  2. source2: Second Input Image array (Single-channel, 8-bit or floating-point)
  3. destination: Output image array
  4. mask: Operation mask, input/ output 8-bit single-channel mask.

Code :

import cv2
import numpy as np
img1 = cv2.imread('input1.png') 
img2 = cv2.imread('input2.png')
dest_or = cv2.bitwise_xor(img1, img2, mask = None)
cv2.imshow('Bitwise XOR', dest_xor)
cv2.waitKey(0)

Conclusion to OpenCV

Many applications use processed images taken from the same scene at different points, such as noise reduction by adding successive images of the same scene or motion detection by subtracting two successive images. Logical operators are frequently used to combine two (mostly binary) images. In the case of integer images, the logical operator is typically used bitwise. Then, for example, we can use a binary mask to select a specific region of an image.

Key Takeaways:

In this article, we learned how to perform various arithmetic operations on images, how different OpenCV methods for performing image arithmetic work, and where these image arithmetic operations are used.

I hope you liked my article on OpenCV. Please share your feedback in the comments section below. Read more articles on OpenCV here.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion. 

I'm Parthiban and I have more than one year experience in Machine Learning and Deep Learning. A Mechanical Engineering enthusiast with the passion to learn more about Machine Learning and emerging technology in the world. Study and transform data science prototypes and Design machine learning systems. Research and implement appropriate ML algorithms and tools. Develop machine learning applications and select appropriate datasets and data representation methods. Perform statistical analysis and fine-tuning using test results, and extend existing ML libraries and frameworks.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details