How to Implement Bitwise Operations On Images Using OpenCV?
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. Topics covered in this article Bitwise Operators in Computer VisionBitwise ANDBitwise ORBitwise NOTBitwise 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 import cv2 from google.colab.patches import cv2_imshow image1 = np.zeros((400, 400), dtype="uint8") cv2.rectangle(image1, (100, 100), (250, 250), 255, -1) cv2_imshow(image1) image2 = np.zeros((400, 400), dtype="uint8") cv2.circle(image2, (150, 150), 90, 255, -1) cv2_imshow(image2) Bitwise example images Bitwise example images 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. Bitwise AND 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) cv2_imshow(bit-and) Bitwise AND operations In the above output, by using the AND function only the intersected regions in both images are displayed. Bitwise OR 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) cv2_imshow(bit-or) Bitwise OR operations In the above output, we merged both images using the OR function Bitwise NOT 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) cv2_imshow(bit-not) Bitwise not operations 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. Bitwise XOR 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) cv2_imshow(bit-xor) Bitwise XOR operations As we can see in the above output, by using the XOR function it removes the intersected region, and the remaining pixels are displayed. Conclusion 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.
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, […]
In this article, we will demonstrate one of the interesting applications of OpenCV in performing bitwise operations on images.
["Opencv image processing"]
["Opencv image processing", "NumPy", "machine learning", "TPU", "AI", "computer vision", "OpenCV", "Colab", "Ray", "Python", "R"]
["AI", "machine learning", "computer vision", "Ray", "Colab", "OpenCV", "NumPy", "TPU", "Python", "R"]
AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with  Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.