How to apply bitwise NOT along with XOR on an image using OpenCV

This recipe helps you apply bitwise NOT along with XOR on an image using OpenCV

Recipe Objective: How to apply bitwise NOT along with XOR on an image using OpenCV?

In this recipe, let us learn how to perform bitwise operations (specifically NOT and XOR) on images and why are they important

Step 1: Import library and read Image

The image that we are using in this recipe is

ProjectPro Logo

import cv2
import numpy as np
image1=cv2.imread('project.jpg', flags=cv2.IMREAD_GRAYSCALE)

Step 2: Converting BGR image to Binary image

The image that we have now read in the image1 variable is in BGR format, and we need the image to be in binary form (i.e., 0 or 1) if we want to perform bitwise logical operations. Binary images generally have 0's or 1's in the place of pixel values; if the pixel value is 0, the pixel represents the white color, and if it is 1, then the pixel represents the black color.

Let us convert the image stored in image1 variable to a binary image using the cv2.threshold() function, which takes the following parameters

  • src: Image which is to be converted to binary
  • thresh: Minimum threshold value
  • maxval: Maximum value to be used if the pixel is above the threshold
  • type: Type of thresholding

Let us take 100 as a minimum threshold value, 255 as a maximum value, and cv2.THRESH_BINARY as the type of thresholding

_, mask_image1 = cv2.threshold(image1, 100, 255, cv2.THRESH_BINARY)

The image now looks like this

cv2.imshow("Project Pro Logo", mask_image1)
cv2.waitKey(0)

Output:

ProjectPro Logo masked

Step 3: Creating another image of the same dimension

To perform bitwise operations, we need another image. Let us create another simple image in the shape of a rectangle so that we can understand how bitwise logical operations work in a better manner

For that, let us first initialize a variable with an array of zeros which is of the same dimensions of the image1

image2 = np.zeros((image1.shape[0], image1.shape[1]), dtype="uint8")

Now let us create a simple rectangle in image2 using the cv2.rectange() function

cv2.rectangle(image2, (0,0), (image1.shape[1],image1.shape[0]), 255, 80)

Now our second image looks like this

cv2.imshow("Rectangle", image2)
cv2.waitKey(0)

Output:

rectangle

Step 4: Bitwise XOR and NOT

Since we have the two images mask_image1 and image2 ready with us, let us perform the bitwise OR and AND operations using the cv2.bitwise_xor() and cv2.bitwise_not() functions respectively

The cv2.bitwise_xor() function expect the following input parameters

  • src1: First image
  • src2: Second image

The cv2.bitwise_not() works the same way as cv2.bitwise_xor(), but it only takes one image as an input parameter instead of two. bitwise_xor = cv2.bitwise_xor(mask_image1, image2)
cv2.imshow("Bitwise XOR", bitwise_xor)
cv2.waitKey(0)
bitwise_not = cv2.bitwise_not(mask_image1) cv2.imshow("Bitwise NOT", bitwise_not) cv2.waitKey(0)

Output:

xor.jpg

 

not.jpg

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.