In this Python OpenCV article i want to show you doing of Arithmetic Operations On Images in OpenCV, so first of all let’s talk about Arithmetic Operations in OpenCV.
What is Arithmetic Operations in Python OpenCV?
Arithmetic operations in OpenCV is a mathematical operations performed on pixel values of images or matrices. These operations can be used to manipulate image data in different ways, such as addition, subtraction, multiplication and division.
In OpenCV, arithmetic operations are often used for tasks like image blending, brightness adjustment, contrast enhancement and image composition. They allows you to perform pixel-wise computations efficiently, and it enables you to do image processing and computer vision tasks.
Common arithmetic operations in Python OpenCV
- Addition: Adding pixel values of two images or a constant value to all pixels.
- Subtraction: Subtracting pixel values of one image from another or a constant value from all pixels.
- Multiplication: Multiplying pixel values of two images or a constant value to all pixels.
- Division: Dividing pixel values of one image by another or a constant value from all pixels.
This is the complete source code for this article
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 29 30 31 32 33 34 35 36 37 |
import cv2 import matplotlib.pyplot as plt # Load the images img1 = cv2.imread("lena.tif") img2 = cv2.imread("woman.tif") # Perform pixel-wise addition and subtraction add = img1 + img2 subtraction = img1 - img2 # Perform addition using OpenCV's add() function add1 = cv2.add(img1, img2) # Perform subtraction using OpenCV's subtract() function sub2 = cv2.subtract(img1, img2) # Display the images using Matplotlib plt.figure(figsize=(10, 5)) plt.subplot(2, 2, 1) plt.imshow(cv2.cvtColor(add, cv2.COLOR_BGR2RGB)) plt.title('Addition') plt.subplot(2, 2, 2) plt.imshow(cv2.cvtColor(subtraction, cv2.COLOR_BGR2RGB)) plt.title('Subtraction') plt.subplot(2, 2, 3) plt.imshow(cv2.cvtColor(add1, cv2.COLOR_BGR2RGB)) plt.title('Addition (OpenCV)') plt.subplot(2, 2, 4) plt.imshow(cv2.cvtColor(sub2, cv2.COLOR_BGR2RGB)) plt.title('Subtraction (OpenCV)') plt.show() |
In this code:
- We load the images using cv2.imread().
- We perform pixel-wise addition and subtraction as before.
- We convert the images from BGR to RGB format using cv2.cvtColor() because Matplotlib expects RGB images.
- We use plt.subplot() to create a grid of subplots.
- We use plt.imshow() to display the images in the subplots.
- Finally, we call plt.show() to display the figure containing the images.
If you run the code this will be the result
FAQs:
What are arithmetic operations on images in OpenCV?
Arithmetic operations on images in OpenCV involve performing mathematical operations between corresponding pixel values of two images or between a constant and all pixel values of an image. These operations include addition, subtraction, multiplication, and division.
How to perform an arithmetic operation on an image?
For performing an arithmetic operation on an image in OpenCV, you can use either simple arithmetic operators (+, -, *, /) or specific OpenCV functions such as cv2.add() and cv2.subtract().
Subscribe and Get Free Video Courses & Articles in your Email