MITB Banner

How to Implement Bitwise Operations On Images Using OpenCV?

Share

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 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

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.

Share
Picture of Prudhvi varma

Prudhvi varma

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.