
- OpenCV Python Tutorial
- OpenCV Python - Home
- OpenCV Python - Overview
- OpenCV Python - Environment
- OpenCV Python - Reading Image
- OpenCV Python - Write Image
- OpenCV Python - Using Matplotlib
- OpenCV Python - Image Properties
- OpenCV Python - Bitwise Operations
- OpenCV Python - Shapes and Text
- OpenCV Python - Mouse Events
- OpenCV Python - Add Trackbar
- OpenCV Python - Resize and Rotate
- OpenCV Python - Image Threshold
- OpenCV Python - Image Filtering
- OpenCV Python - Edge Detection
- OpenCV Python - Histogram
- OpenCV Python - Color Spaces
- OpenCV Python - Transformations
- OpenCV Python - Image Contours
- OpenCV Python - Template Matching
- OpenCV Python - Image Pyramids
- OpenCV Python - Image Addition
- OpenCV Python - Image Blending
- OpenCV Python - Fourier Transform
- OpenCV Python - Capture Videos
- OpenCV Python - Play Videos
- OpenCV Python - Images From Video
- OpenCV Python - Video from Images
- OpenCV Python - Face Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Feature Detection
- OpenCV Python - Feature Matching
- OpenCV Python - Digit Recognition
- OpenCV Python Resources
- OpenCV Python - Quick Guide
- OpenCV Python - Resources
- OpenCV Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
OpenCV Python - Bitwise Operations
Bitwise operations are used in image manipulation and for extracting the essential parts in the image.
Following operators are implemented in OpenCV −
- bitwise_and
- bitwise_or
- bitwise_xor
- bitwise_not
Example 1
To demonstrate the use of these operators, two images with filled and empty circles are taken.
Following program demonstrates the use of bitwise operators in OpenCV-Python −
import cv2 import numpy as np img1 = cv2.imread('a.png') img2 = cv2.imread('b.png') dest1 = cv2.bitwise_and(img2, img1, mask = None) dest2 = cv2.bitwise_or(img2, img1, mask = None) dest3 = cv2.bitwise_xor(img1, img2, mask = None) cv2.imshow('A', img1) cv2.imshow('B', img2) cv2.imshow('AND', dest1) cv2.imshow('OR', dest2) cv2.imshow('XOR', dest3) cv2.imshow('NOT A', cv2.bitwise_not(img1)) cv2.imshow('NOT B', cv2.bitwise_not(img2)) if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
Output



Example 2
In another example involving bitwise operations, the opencv logo is superimposed on another image. Here, we obtain a mask array calling threshold() function on the logo and perform AND operation between them.
Similarly, by NOT operation, we get an inverse mask. Also, we get AND with the background image.
Following is the program which determines the use of bitwise operations −
import cv2 as cv import numpy as np img1 = cv.imread('lena.jpg') img2 = cv.imread('whitelogo.png') rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols] img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY) ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY) mask_inv = cv.bitwise_not(mask) # Now black-out the area of logo img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv) # Take only region of logo from logo image. img2_fg = cv.bitwise_and(img2,img2,mask = mask) # Put logo in ROI dst = cv.add(img2_fg, img1_bg) img1[0:rows, 0:cols ] = dst cv.imshow(Result,img1) cv.waitKey(0) cv.destroyAllWindows()
Output
The masked images give following result −

Advertisements