Arithmetic Operations on Images using Python OpenCV
In this article, you will learn about different arithmetic operations performed on images using Python OpenCV. We have performed several arithmetic operations on numbers. But, these arithmetic operations can also be performed on images, like addition, subtraction, multiplication, and division.
First, we need to install the OpenCV module. Here is the command to install the OpenCV module with the pip tool.
pip install opencv-contrib-python
The above command installs some other modules along with OpenCV. Those modules are generally used with OpenCV.
Addition of Images Python OpenCV
Python OpenCV provides the cv2.addWeighted() method to add two images. This method calculates the weighted sum of two arrays. It returns an image in the output, which is a combination of the corresponding pixel values of the input images. It has the following syntax-
cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])
src1- first input array,
alpha- weight of the first array elements,
src2- second input array of the same size and channel number as src1,
beta- weight of the second array elements,
gamma- scalar added to each sum,
dst- output array that has the same size and number of channels as the input arrays,
dtype- optional depth of the output array.
Example of Images Addition
Suppose we have the following two images-
import cv2
import numpy as np
img1 = cv2.imread('nature.jpg')
img2 = cv2.imread('ocean.jpg')
dst = cv2.addWeighted(img1, 0.7, img2, 0.6, 0)
cv2.imshow('Blended Image',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns the following output-
Subtraction of Images Python OpenCV
In Python OpenCV, the cv2.subtract() method is used to subtract two images. Both images should be of the same size and type.
import cv2
import numpy as np
img1 = cv2.imread('nature.jpg')
img2 = cv2.imread('ocean.jpg')
result_image = cv2.subtract(img1, img2)
cv2.imshow('Subtracted Image', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Multiplication of images Python OpenCV
The addition and subtraction of the images are generally used. It is also possible to multiply and divide the images, which are not generally used. In Python OpenCV, cv2.multiply() method is used to multiply two images. Suppose we have the following two images-
import cv2
import numpy as np
img1 = cv2.imread('nature.jpg')
img2 = cv2.imread('night.jpg')
result_image = cv2.multiply(img1, img2)
cv2.imshow('Multiply Image', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns the following output-
Related Articles
Detect Specific Color From Image using Python OpenCVPython OpenCV Histogram Equalization
Python OpenCV Erosion and Dilation
Adaptive Thresholding in Python OpenCV
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
OpenCV human detection
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