In this OpenCV Python article we are going to create an example of Bitwise Operations On Images, this includes bitwise AND, OR, NOT and XOR operations. They will be highly useful while extracting any part of the image , defining and working with non rectangular ROI etc. below we will see an example on how to change a particular region of an image.
OpenCV Python Bitwise Operations On Images?
Bitwise operations on images in OpenCV Python are operations that manipulate the pixel values of images at a binary level. These operations include bitwise AND, bitwise OR, bitwise XOR, and bitwise NOT. They are commonly used in image processing tasks such as masking, blending, and extracting regions of interest.
So this is the complete source code for this tutorial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import cv2 import numpy as np # Create black image (all zeros) of size 300x300 img1 = np.zeros((300, 300), dtype="uint8") # Draw white rectangle on the black image cv2.rectangle(img1, (100, 100), (250, 250), 255, -1) cv2.imshow("Image 1", img1) # Create another black image (all zeros) of size 300x300 img2 = np.zeros((300, 300), dtype="uint8") # Draw white circle on the black image cv2.circle(img2, (150, 150), 90, 255, -1) cv2.imshow("Image 2", img2) # Perform bitwise AND operation between # the rectangle and circle images rect_and_circle = cv2.bitwise_and(img1, img2) cv2.imshow("AND operation", rect_and_circle) # Perform bitwise OR operation between rectangle and circle images rect_or_circle = cv2.bitwise_or(img1, img2) cv2.imshow("OR operation", rect_or_circle) # Perform bitwise XOR operation between rectangle and circle images rect_xor_circle = cv2.bitwise_xor(img1, img2) cv2.imshow("XOR Operation", rect_xor_circle) # Perform another XOR operation between rectangle and circle images rect_xor_circle2 = cv2.bitwise_xor(img1, img2) cv2.imshow("XOR Operation 2", rect_xor_circle2) # Wait for key press and close all windows cv2.waitKey(0) cv2.destroyAllWindows() |
These line of code are for drawing of our images shapes in OpenCV, basically first we have created an empty image using Numpy and after that we have created one rectangle and one circle, because we want to do our OpenCV Bitwise Operations.
1 2 3 4 5 6 7 |
img1 = np.zeros((300, 300), dtype="uint8") cv2.rectangle(img1, (100, 100), (250, 250), 255, -1) cv2.imshow("Image 1", img1) img2 = np.zeros((300, 300), dtype="uint8") cv2.circle(img2, (150, 150), 90, 255, -1) cv2.imshow("Image 2", img2) |
In here we are going to do our OpenCV Bitwise Operations, there are different operations that we want to perform like AND,OR and XOR operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
rect_and_circle = cv2.bitwise_and(img1,img2) cv2.imshow("AND operation",rect_and_circle) rect_or_circle = cv2.bitwise_or(img1,img2) cv2.imshow("OR operation",rect_or_circle) rect_xor_circle = cv2.bitwise_xor(img1,img2) cv2.imshow("XOR Operation",rect_xor_circle) rect_xor_circle2 = cv2.bitwise_xor(img1,img2) cv2.imshow("XOR Operation",rect_xor_circle2) |
Run the complete code and this will be the result
FAQs:
What are bitwise operations on an image?
Bitwise operations on an image are operations that manipulate the pixel values of images at binary level. These operations include bitwise AND, bitwise OR, bitwise XOR and bitwise NOT. They are commonly used in image processing tasks such as masking, blending and extracting regions of interest.
What does cv2.bitwise_and do?
cv2.bitwise_and function in OpenCV performs a bitwise AND operation between two input images. It computes the bitwise AND of corresponding pixel values in the input images and produces a new image where each pixel is the result of the bitwise AND operation applied to the corresponding pixels in the input images.
What is a mask in bitwise AND in OpenCV?
In bitwise AND operations in OpenCV, a mask is a binary image used to selectively apply the bitwise AND operation to specific regions of an image. mask image should have the same dimensions as the input images, and only pixels with non-zero values in the mask are considered for the bitwise AND operation.
What are the arithmetic operations in OpenCV?
In OpenCV, arithmetic operations are mathematical operations performed on pixel values of images. These operations include addition, subtraction, multiplication and division. They can be applied to entire images or specific regions of interest within images to perform tasks such as contrast adjustment, brightness correction and image blending. OpenCV provides functions such as cv2.add, cv2.subtract, cv2.multiply, and cv2.divide for performing arithmetic operations on images.
Subscribe and Get Free Video Courses & Articles in your Email