OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Bitwise operations are fundamental when dealing with binary images in OpenCV. They enable various image processing tasks like masking, merging, and extracting regions of interest (ROIs). This tutorial will guide you through the process of using bitwise operations on binary images using OpenCV in Python.
Setting Up: Before you begin, make sure you have OpenCV installed. You can install it using pip:
pip install opencv-python
Creating Binary Images: For the sake of this tutorial, let's create two basic binary images using NumPy and OpenCV:
import cv2 import numpy as np # Create a black image of size 300x300 img1 = np.zeros((300, 300), dtype="uint8") img2 = np.zeros((300, 300), dtype="uint8") # Draw a white rectangle on img1 cv2.rectangle(img1, (50, 50), (250, 250), 255, -1) # Draw a white circle on img2 cv2.circle(img2, (150, 150), 100, 255, -1)
Bitwise Operations:
Bitwise AND: It returns white wherever both images have white pixels.
bitwise_and = cv2.bitwise_and(img1, img2) cv2.imshow("AND", bitwise_and)
Bitwise OR: It returns white wherever at least one image has a white pixel.
bitwise_or = cv2.bitwise_or(img1, img2) cv2.imshow("OR", bitwise_or)
Bitwise XOR: It returns white wherever one image has a white pixel and the other doesn't.
bitwise_xor = cv2.bitwise_xor(img1, img2) cv2.imshow("XOR", bitwise_xor)
Bitwise NOT: It inverts the binary colors (black to white and vice-versa).
bitwise_not1 = cv2.bitwise_not(img1) cv2.imshow("NOT Image 1", bitwise_not1) bitwise_not2 = cv2.bitwise_not(img2) cv2.imshow("NOT Image 2", bitwise_not2)
Display Images:
cv2.imshow("Image 1", img1) cv2.imshow("Image 2", img2) cv2.waitKey(0) cv2.destroyAllWindows()
By running the above code snippets, you should be able to view the two original binary images and the results of each of the bitwise operations.
These bitwise operations are especially useful in scenarios like extracting a region of interest (ROI) using a mask. For example, you can use the bitwise_and
operation between a grayscale/color image and a binary mask to extract the area corresponding to the white parts of the mask.
import cv2 import numpy as np # Load binary images (black and white) image1 = cv2.imread('binary_image1.png', cv2.IMREAD_GRAYSCALE) image2 = cv2.imread('binary_image2.png', cv2.IMREAD_GRAYSCALE) # Perform bitwise AND, OR, XOR, and NOT bitwise_and_result = cv2.bitwise_and(image1, image2) bitwise_or_result = cv2.bitwise_or(image1, image2) bitwise_xor_result = cv2.bitwise_xor(image1, image2) bitwise_not_result1 = cv2.bitwise_not(image1) bitwise_not_result2 = cv2.bitwise_not(image2) # Display results cv2.imshow('Bitwise AND Result', bitwise_and_result) cv2.imshow('Bitwise OR Result', bitwise_or_result) cv2.imshow('Bitwise XOR Result', bitwise_xor_result) cv2.imshow('Bitwise NOT Result 1', bitwise_not_result1) cv2.imshow('Bitwise NOT Result 2', bitwise_not_result2) cv2.waitKey(0) cv2.destroyAllWindows()
# Create a circular mask mask = np.zeros_like(image1) cv2.circle(mask, (150, 150), 100, 255, -1) # Bitwise AND with the circular mask masked_result = cv2.bitwise_and(image1, mask) # Display result cv2.imshow('Masked Result', masked_result) cv2.waitKey(0) cv2.destroyAllWindows()
# Create a binary image with a rectangular region rectangular_region = np.zeros_like(image1) cv2.rectangle(rectangular_region, (50, 50), (200, 200), 255, -1) # Bitwise OR to combine binary images combined_result = cv2.bitwise_or(image1, rectangular_region) # Bitwise AND to extract information from a specific region extracted_result = cv2.bitwise_and(image1, rectangular_region) # Display results cv2.imshow('Combined Result', combined_result) cv2.imshow('Extracted Result', extracted_result) cv2.waitKey(0) cv2.destroyAllWindows()