OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
In the previous article, you will learn about the different arithmetic operations on images using 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 returns 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.
OpenCV Bitwise AND
Python OpenCV provides cv2.bitwise_and() method to perform bitwise AND logical operation. It combines corresponding pixels of two image buffers by a bitwise AND operation. It commonly used for detecting differences in the 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 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 -

OpenCV Bitwise OR
Python OpenCV provides cv2.bitwise_or() method to perform bitwise OR logical operation. It combines corresponding pixels of two image buffers by a bitwise OR operation. The bitwise OR operator are useful for processing binary-valued images (0 or 1) to detect objects which have moved between frames. Here is the 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()

OpenCV Bitwise XOR
Python OpenCV provides 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 which 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()

OpenCV Bitwise NOT
Bitwise NOT or complement is a unary operation that performs logical negation on each bit. It is forming the ones' 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 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 OpenCV Overlaying or Blending 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
Python OpenCV Overview and Examples