OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
In the previous article, you learned about the different arithmetic operations on images using the Python OpenCV. In this article, you will learn to perform logical operations on images, like bitwise AND, OR, NOR, and XOR using Python OpenCV.
The concept of logical operators is very simple. We hope you have definitely performed logical operations on numbers that return some Boolean value. But, in the case of images, the logical operators are applied in a pixel-by-pixel way, i.e., the value of a pixel in the output image depends only on the values of the corresponding pixels in the input images. Hence, the input images must be of the same size and type.
Python cv2 bitwise AND
To perform bitwise AND logical operations, Python OpenCV provides the cv2.bitwise_and() method. It combines the corresponding pixels of two image buffers using a bitwise AND operation. It is commonly used for detecting differences in input images. It returns an image highlighting the target regions with a binary mask.
Suppose we have the following two images-
In the given example, we have applied the cv2.bitwise_and() method to perform a bitwise AND operation on the above images.
import cv2
img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_and = cv2.bitwise_and(img2, img1)
cv2.imshow("bit_and", bitwise_and)
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns the following output-
Python cv2 bitwise OR
Python OpenCV provides the cv2.bitwise_or() method to perform bitwise OR logical operation. It combines the corresponding pixels of two image buffers by a bitwise OR operation. The bitwise OR operator is useful for processing binary-valued images (0 or 1) to detect objects that have moved between frames. Here is an example-
import cv2
img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_or = cv2.bitwise_or(img2, img1)
cv2.imshow("bitwise_or", bitwise_or)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python cv2 bitwise XOR
Python OpenCV provides the cv2.bitwise_xor() method to perform bitwise XOR logical operation. It combines corresponding pixels of two image buffers by a bitwise XOR operation. It is also used for processing binary-valued images (0 or 1) to detect objects that have moved between frames.
import cv2
img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_xor = cv2.bitwise_xor(img2, img1)
cv2.imshow("bitwise_xor", bitwise_xor)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python cv2 bitwise NOT
Bitwise NOT or complement is a unary operation that performs logical negation on each bit. It forms the one's complement of the given binary value. It means the bits that are 1 become 0, and those that are 0 become 1. Python OpenCV provides the cv2.bitwise_not() method to perform a bitwise NOT operation on each pixel of the input image. It inverts the image representation.
import cv2
img2 = cv2.imread("nature.jpg")
bitwise_not = cv2.bitwise_not(img2)
cv2.imshow("bitwise_not", bitwise_not)
cv2.waitKey(0)
cv2.destroyAllWindows()
Similarly, when we perform bitwise NOT on the second image, we will get the following.
import cv2
img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_not = cv2.bitwise_not(img1)
cv2.imshow("bitwise_not", bitwise_not)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related Articles
How to capture a video in Python OpenCV and savePython overlay two images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
How to find the stop words in nltk Python
Eye Detection Program in Python OpenCV
Arithmetic Operations on Images using Python OpenCV
Python Tkinter Combobox
Python OpenCV Overview and Examples