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

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

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.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.