How to Implement Bitwise Operations On Images Using OpenCV?

How to Implement Bitwise Operations On Images Using OpenCV?

In this article, we will demonstrate one of the interesting applications of OpenCV in performing bitwise operations on 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, 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)


developers corner bitwise and bitwise not bitwise or bitwise xor opencv image processing

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Complete Guide to Mahotas Python Library for Image Processing

Processing an image in order to derive some meaningful information from the image is known as image processing. It can be called a scientific study where we apply different methods or functions on images to find out what are its different features. We can enhance the image or degrade the image in order to extract unique features.

Guide To OpenCV Functions For Image Processing

In this article, we will demonstrate some important image processing steps and an image by manipulating its pixels using OpenCV functions.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company

How To Create A Watermark On Images Using OpenCV

How To Create A Watermark On Images Using OpenCV - An easiest and quick way for image watermarking in Python.Watermark is a message that may be a logo, signature, or stamp that is used to recognize the ownership of the creator. It visualizes the logo without interpreting the visibility of the image.