This article was published as a part of the Data Science Blogathon.
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.
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:
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,
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:
Input images
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,
Bitwise operations are used in image manipulation to extract important parts. The following Bitwise operations are used in this article:
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.
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:
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)
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:
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)
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:
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)
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:
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)
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.