OpenCV Python – Bitwise Operations
Bitwise operations in OpenCV are used to manipulate images at the pixel level using logical operations. These operations are very important in computer vision tasks like masking, blending images, and region selection.
OpenCV provides four main bitwise operations:
- AND
- OR
- XOR
- NOT
In this tutorial, you will learn how each operation works with simple examples.
1. What are Bitwise Operations?
Bitwise operations work on binary pixel values (0 and 1). In OpenCV, they are used to combine or modify images based on pixel logic.
They are commonly used for:
- Image masking
- Object isolation
- Region extraction
- Logo overlay
- Background removal
2. Import OpenCV and NumPy
import cv2
import numpy as np
3. Create Sample Images
We create simple images for demonstration:
img1 = np.zeros((300, 300), dtype=np.uint8)
img2 = np.zeros((300, 300), dtype=np.uint8)
cv2.rectangle(img1, (50, 50), (250, 250), 255, -1)
cv2.circle(img2, (150, 150), 120, 255, -1)
4. Bitwise AND Operation
Syntax:
cv2.bitwise_and(img1, img2)
Example:
bit_and = cv2.bitwise_and(img1, img2)
cv2.imshow("AND Operation", bit_and)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
- Keeps only overlapping white regions
- Used for masking
5. Bitwise OR Operation
Syntax:
cv2.bitwise_or(img1, img2)
Example:
bit_or = cv2.bitwise_or(img1, img2)
cv2.imshow("OR Operation", bit_or)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
- Combines both images
- Shows all white regions
6. Bitwise XOR Operation
Syntax:
cv2.bitwise_xor(img1, img2)
Example:
bit_xor = cv2.bitwise_xor(img1, img2)
cv2.imshow("XOR Operation", bit_xor)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
- Shows only non-overlapping regions
- Removes common areas
7. Bitwise NOT Operation
Syntax:
cv2.bitwise_not(img1)
Example:
bit_not = cv2.bitwise_not(img1)
cv2.imshow("NOT Operation", bit_not)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
- Inverts image colors
- 0 becomes 255 and 255 becomes 0
8. Real-World Use Case: Image Masking
img = cv2.imread("image.jpg")
mask = np.zeros(img.shape[:2], dtype=np.uint8)
cv2.circle(mask, (150,150), 100, 255, -1)
result = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("Masked Image", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
9. Why Bitwise Operations are Important
Bitwise operations are widely used in:
- Face filters (like Snapchat/Instagram)
- Background removal
- Object detection masking
- Augmented reality applications
- Image blending effects
10. Common Mistakes
❌ Images not same size
✔ Solution:
Ensure both images have same dimensions:
img1 = cv2.resize(img1, (300,300))
❌ Wrong data type
✔ Solution:
Use grayscale or binary images:
dtype=np.uint8
11. Conclusion
Bitwise operations in OpenCV Python are powerful tools for manipulating images at the pixel level. They are essential for masking, filtering, and combining images in computer vision projects.
Once you master these operations, you can build advanced applications like object segmentation and AR filters.


0 Comments