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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

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.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.