Welcome to this tutorial on arithmetic operations on images using OpenCV-Python! In this tutorial, we will cover addition, subtraction, and bitwise operations. By the end of this tutorial, you will have a solid understanding of how to manipulate images using these basic operations.
Image addition is the process of adding pixel values to two images. In OpenCV, the function cv2.add() is used for this purpose.
Open the Python Shell
Open Python Shell in terminal
python3
Load two images
Utilize .imread to read targeted images.
import cv2 as cv2
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
Add the images
Use .add() to aggregate two images that were read in the previous step.
result = cv2.add(img1, img2)
Save the result
Write the result out.
cv2.imwrite('Image_Addition.jpg', result)
Example
The generated image Image_Addition.jpg looks like:
Image Blending
This is also image addition, but different weights are given to images in order to give a feeling of blending or transparency. Images are added as per the equation below:
cv.addWeighted() applies this equation to the image.
Add the images
Use .addWeighted() to apply the equation to images that were read in the previous step.
result = cv2.addWeighted(img1,0.7,img2,0.3,0)
Save the result
Write the result out.
cv2.imwrite('Image_Blending.jpg', result)
Example
The generated image Image_Blending.jpg looks like:
Image Subtraction
Image subtraction is used to find the difference between two images. It subtracts the pixel values of one image from another. In OpenCV, you can use the cv2.subtract() function.
Subtract the images
Use .subtract() to subtract the pixel values of image1 from image2.
result = cv2.subtract(img1, img2)
Save the result
Write the result out.
cv2.imwrite('Image_Subtraction.jpg', result)
Example
The generated image Image_Subtraction.jpg looks like:
Bitwise Operations
Bitwise operations are used to manipulate the individual bits of pixel values. OpenCV provides functions like cv2.bitwise_and(), cv2.bitwise_or(), cv2.bitwise_xor(), and cv2.bitwise_not() for performing bitwise operations.
Let's see an example of each operation:
Bitwise AND
Use .bitwise_and() to compute bit-wise AND of bits of pixel values in two images.
bitwise_and = cv2.bitwise_and(img1, img2)
Bitwise OR
Use .bitwise_or() to compute bit-wise OR of bits of pixel values in two images.
bitwise_or = cv2.bitwise_or(img1, img2)
Bitwise XOR
Use .bitwise_xor() to compute bit-wise XOR of bits of pixel values in two images.
bitwise_xor = cv2.bitwise_xor(img1, img2)
Bitwise NOT
Use .bitwise_not() to compute bit-wise NOT of bits of pixel values in two images.
Congratulations! You have now learned how to perform arithmetic operations on images using OpenCV-Python. You have covered image addition, subtraction, and bitwise operations. These basic operations can be very useful for image processing tasks, such as blending images, detecting changes, and masking specific areas of an image.
Keep practicing and experimenting with different images to get a better understanding of how these operations work. Good luck, and happy coding!
We use cookies for a number of reasons, such as keeping the website reliable and secure, to improve your experience on our website and to see how you interact with it. By accepting, you agree to our use of such cookies. Privacy Policy