OPENCV-PYTHON | Bitwise Operations | AND, OR, XOR, NOT with Images | Image BITWISE Masking
Bitwise operations in OpenCV provide a powerful toolkit for manipulating and combining images at the pixel level. This article explores the fundamental bitwise operations—AND, OR, XOR, and NOT—while delving into the practical applications of image bitwise masking. By understanding these operations, developers gain the ability to create complex masks, extract regions of interest, and perform dynamic image blending.
Flow of the Article:
- Understanding bitwise operations
- various bitwise operations – AND, OR, NOT, XOR
- Applying them using codes
- Image masking and bit wise operations
Video Explanation:
Understanding Bitwise Operations:
Bitwise operations involve the manipulation of individual bits of binary representations of pixel values. OpenCV provides bitwise AND, OR, XOR, and NOT operations, allowing users to perform pixel-wise operations on images.
Mathematical Explanation:
Bitwise AND (&): R=A & B sets each pixel in RR to 1 only if the corresponding pixels in both AA and BB are 1.
Bitwise OR (|): R=A || B sets each pixel in RR to 1 if at least one corresponding pixel in AA or BB is 1.
Bitwise XOR (^): R = A ^ B sets each pixel in RR to 1 if the corresponding pixels in AA and BB are different.
Bitwise NOT (~): R=∼A inverts the bits of each pixel in AA.
OpenCV Code for Bitwise Operations:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load two images
img1 = cv2.imread(‘image1.jpg’)
img2 = cv2.imread(‘image2.jpg’)
# Bitwise AND operation
bitwise_and = cv2.bitwise_and(img1, img2)
# Bitwise OR operation
bitwise_or = cv2.bitwise_or(img1, img2)
# Bitwise XOR operation
bitwise_xor = cv2.bitwise_xor(img1, img2)
# Bitwise NOT operation
bitwise_not = cv2.bitwise_not(img1)
# Display the results
plt.figure(figsize=(12, 8))
plt.subplot(2, 3, 1)
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
plt.title(‘Image 1’)
plt.subplot(2, 3, 2)
plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
plt.title(‘Image 2’)
plt.subplot(2, 3, 3)
plt.imshow(cv2.cvtColor(bitwise_and, cv2.COLOR_BGR2RGB))
plt.title(‘Bitwise AND’)
plt.subplot(2, 3, 4)
plt.imshow(cv2.cvtColor(bitwise_or, cv2.COLOR_BGR2RGB))
plt.title(‘Bitwise OR’)
plt.subplot(2, 3, 5)
plt.imshow(cv2.cvtColor(bitwise_xor, cv2.COLOR_BGR2RGB))
plt.title(‘Bitwise XOR’)
plt.subplot(2, 3, 6)
plt.imshow(cv2.cvtColor(bitwise_not, cv2.COLOR_BGR2RGB))
plt.title(‘Bitwise NOT’)
plt.show()
Image Bitwise Masking:
Image bitwise masking involves using a binary mask to selectively apply bitwise operations to specific regions of an image. This technique is invaluable for isolating and manipulating regions of interest.
OpenCV Code for Image Bitwise Masking:
# Create a binary mask
mask = cv2.imread(‘mask.jpg’, cv2.IMREAD_GRAYSCALE)
# Resize the mask to match image dimensions
mask = cv2.resize(mask, (img1.shape[1], img1.shape[0]))
# Bitwise AND with mask
bitwise_and_masked = cv2.bitwise_and(img1, img1, mask=mask)
# Display the results
plt.figure(figsize=(12, 4))
plt.subplot(1, 3, 1)
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
plt.title(‘Image 1’)
plt.subplot(1, 3, 2)
plt.imshow(mask, cmap=’gray’)
plt.title(‘Binary Mask’)
plt.subplot(1, 3, 3)
plt.imshow(cv2.cvtColor(bitwise_and_masked, cv2.COLOR_BGR2RGB))
plt.title(‘Bitwise AND with Mask’)
plt.show()
Practical Applications:
Object Extraction:
- Bitwise operations are used to extract specific objects or regions from an image, especially when combined with binary masks.
Dynamic Image Blending:
- Creating smooth transitions or blends between images based on a dynamic mask is achievable through bitwise operations.
Image Encryption and Watermarking:
- XOR operations can be employed for simple image encryption, and bitwise AND or OR operations can be used for watermarking.
Region-of-Interest (ROI) Selection:
- Bitwise masking allows the selection of specific regions in an image for further analysis or processing.
Creating Special Effects:
- XOR operations can be used to create special effects by combining two images in a unique way.
Understanding the nuances of bitwise operations and image masking opens up a plethora of possibilities for creative image manipulation and processing in computer vision and image processing applications.
Here are some important links related to the above article :
1. OpenCV Documentation:
– The official documentation for OpenCV provides in-depth information about the library, including functions and usage.
2. OpenCV Bitwise Operations
– This tutorial from the OpenCV documentation specifically covers bitwise operations, providing code examples and explanations.
3. Histogram Equalization in OpenCV:
– OpenCV tutorial on histogram equalization, explaining the concept and providing code examples for both grayscale and color images.
4. Image Thresholding in OpenCV:
– OpenCV tutorial on image thresholding, covering global and adaptive thresholding techniques with code examples.
5. Image Processing in Python with OpenCV:
– A DataCamp tutorial providing an introduction to image processing in Python using OpenCV, covering various techniques.
infoaryan, aryan verma blog, aryan verma, opencv computer vision articles, best opencv explanations, image processing tutorials, video processsing, computer vision and image processing, aryan verma blog, infoaryan.com, bitwise operations tutorials, bitwise and or operations.