How to Perform Bitwise XOR Operation on Images in OpenCV Python?
OpenCV is a popular open-source computer vision library. It is widely used for image processing, video processing, machine learning, and more. In this tutorial, we will learn how to perform a bitwise XOR operation on images using OpenCV Python.
A bitwise XOR operation is a logical operation that takes two input bits and outputs a single bit. The output is 1 if the two input bits are different, and 0 if they are the same. In image processing, we use bitwise XOR operation to combine two binary images to form a new image, where each pixel is the result of the XOR operation on the corresponding pixels of the two input images.
Prerequisites
Before we start, make sure you have installed OpenCV and NumPy libraries. If you haven’t yet, you can install them using pip:
pip install opencv-python
pip install numpy
Loading Images
Let’s first load two binary images that we will use for bitwise XOR operation. In this example, we have two images. The first image is a square with a white background and a black square in the center. The second image is similar to the first one, but the black square is shifted to the right:
import cv2
import numpy as np
# load the images
img1 = cv2.imread('image1.png', 0)
img2 = cv2.imread('image2.png', 0)
# check the size of the images
print("Image 1 size:", img1.shape)
print("Image 2 size:", img2.shape)
In the code above, we first import the necessary libraries. Then, we load the two images using the cv2.imread()
function. The second argument to the function specifies the color type of the image. A value of 0 indicates a binary image, which is what we have in this example. We then print the size of the two images to make sure they have the same dimensions.
Performing Bitwise XOR Operation
To perform a bitwise XOR operation on two images, we use the cv2.bitwise_xor()
function. The function takes two images as input and outputs a new image:
# perform bitwise XOR operation on the two images
result = cv2.bitwise_xor(img1, img2)
# display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the code above, we pass the two images img1
and img2
to the cv2.bitwise_xor()
function to perform the bitwise XOR operation. We then display the resulting image using the cv2.imshow()
function. The cv2.waitKey(0)
function waits for a key event, and cv2.destroyAllWindows()
closes all windows.
Visualizing the Result
Let’s visualize the result by displaying the original images and the resulting image side by side:
# create a new image by stacking the two input images horizontally
img_concat = np.hstack((img1, img2))
# create a new image by stacking the input images and the resulting image horizontally
result_concat = np.hstack((img_concat, result))
# display the images
cv2.imshow('Input and Result', result_concat)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the code above, we use the np.hstack()
function to stack the two input images horizontally and create a new image. We then stack the input images and the resulting image horizontally to create a new image using the same function. Finally, we display the resulting image using the cv2.imshow()
function.
Code Summary
Here’s the complete code:
import cv2
import numpy as np
# load the images
img1 = cv2.imread('image1.png', 0)
img2 = cv2.imread('image2.png', 0)
# check the size of the images
print("Image 1 size:", img1.shape)
print("Image 2 size:", img2.shape)
# perform bitwise XOR operation on the two images
result = cv2.bitwise_xor(img1, img2)
# display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# create a new image by stacking the two input images horizontally
img_concat = np.hstack((img1, img2))
# create a new image by stacking the input images and the resulting image horizontally
result_concat = np.hstack((img_concat, result))
# display the images
cv2.imshow('Input and Result', result_concat)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
In this tutorial, we have learned how to perform a bitwise XOR operation on images using OpenCV Python. We first loaded two binary images and then used thecv2.bitwise_xor()
function to perform the XOR operation on the two images. We then displayed the resulting image and visualized it by creating a new image that includes the input images and the result.
Bitwise XOR operation is just one of the many operations we can perform on images using OpenCV. With its powerful tools and functions, OpenCV provides endless possibilities for image processing and computer vision applications.