Apply bitwise AND along with OR on an image using OpenCV

This recipe explains how to apply bitwise AND along with OR on an image using OpenCV. With the help of cv2 bitwise or and cv2 bitwise and functions you will be able to perform AND along with OR operator on an image

Recipe Objective: How to apply bitwise AND along with OR on an image using OpenCV? Why are they important?

This recipe teaches us how to perform bitwise operations (precisely AND and OR) on images and why they are essential.

Access YOLO OCR Character Recognition Project with Source Code

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 OR and AND

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_or() and cv2.bitwise_and() functions, respectively

Both these functions expect the same kind of input parameters as given below

  • src1: First image
  • src2: Second image

bitwise_or = cv2.bitwise_or(mask_image1, image2)
cv2.imshow("Bitwise OR", bitwise_or)
cv2.waitKey(0)
bitwise_and = cv2.bitwise_and(mask_image1, image2)
cv2.imshow("Bitwise AND", bitwise_and)
cv2.waitKey(0)

Output:

or.jpg

 

and.jpg

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

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.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.