About Lesson
In this Python OpenCV lesson we are going to learn about Python OpenCV Bitwise Operators, bitwise operations include AND, OR, NOT and XOR operations these operations are very useful when we want to extract any part of the image.
This is the complete code for this lesson.
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 |
import cv2 import numpy as np 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) and_bitwise = cv2.bitwise_and(img1, img2) cv2.imshow("Bitwise AND", and_bitwise) or_bitwise = cv2.bitwise_or(img1, img2) cv2.imshow("Bitwise OR" , or_bitwise) xor_bitwise = cv2.bitwise_xor(img1, img2) cv2.imshow("XOR Operations", xor_bitwise) cv2.waitKey(0) cv2.destroyAllWindows() |
In the above code we have used different bitwise operations like bitwise and, bitwise or and xor operations.
Run the complete code and this is the result.