OpenCV is an image processing library created by intel which includes various packages and several functions in it. OpenCV is used to solve many problems in computer vision and machine learning applications and due to its large community, it is getting updated day by day. OpenCV can be implemented in C++, Python, Java programming languages, and different platforms like Linux, Windows, macOS.
In this article, we will demonstrate one of the interesting applications of OpenCV in performing bitwise operations on images.
Sign up for your weekly dose of what's up in emerging technology.
Topics covered in this article
- Bitwise Operators in Computer Vision
- Bitwise AND
- Bitwise OR
- Bitwise NOT
- Bitwise XOR
Bitwise Operators in Computer Vision
Bitwise operations can be used in image manipulations. These bitwise techniques are used in many computer vision applications like for creating masks of the image, adding watermarks to the image and it is possible to create a new image using these bitwise operators. These operations work on the individual pixels in the image to give accurate results compared with other morphing techniques in OpenCV.
Using the below code snippet, we will create two images – image1, image2 as input images on which the bitwise operations will be performed.
import numpy as np
from google.colab.patches import cv2_imshow
image1 = np.zeros((400, 400), dtype="uint8")
cv2.rectangle(image1, (100, 100), (250, 250), 255, -1)
image2 = np.zeros((400, 400), dtype="uint8")
cv2.circle(image2, (150, 150), 90, 255, -1)
In the above output, we created a square & circle with white pixels and a background with black pixels using these we will differentiate each bitwise function individually.
Now we will start performing the bitwise operations.
This function calculates the conjunction of pixels in both images. This operation only considers pixels that are common with image 1 and image 2 and remaining pixels are removed from the output image.
bit-and = cv2.bitwise_and(img1,img2)
In the above output, by using the AND function only the intersected regions in both images are displayed.
This function calculates the disjunction of the pixels in both images. Here we perform an element-wise product of the array, we will not eliminate pixels but it merges both images.
bit-or = cv2.bitwise_or(img1,img2)
In the above output, we merged both images using the OR function
This function inverts every bit of an array. It replaces the white pixels with black pixels and vice versa. This operation can be performed only on one image.
bit-not = cv2.bitwise_not(img1)
In the above execution, we have given input as a circle with white pixels and background with black pixels by using the NOT function to replace black pixels with white and vice versa.
This function inverts the pixels which are intersected in image 1 & image 2 and the rest of the pixels remains the same.
bit-xor = cv2.bitwise_xor(img1,img2)
As we can see in the above output, by using the XOR function it removes the intersected region, and the remaining pixels are displayed.
In this article, we demonstrated that bitwise functions are used for image manipulations using OpenCV. We illustrated the basic difference between each function. These operations are used in many computer vision real-time applications. But the main drawback of this bitwise function is that we cannot perform these operations with more than images.