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


image2 = np.zeros((400, 400), dtype="uint8"), (150, 150), 90, 255, -1)


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)


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)


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)


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)


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.


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.

Download our Mobile App

Subscribe to our newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day.
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Our Recent Stories

Our Upcoming Events

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox