Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction) - GeeksforGeeks

13 Dec, 2021
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images. These operations can be helpful in enhancing the properties of the input images. The Image arithmetics are important for analyzing the input image properties. The operated images can be further used as an enhanced input image, and many more operations can be applied for clarifying, thresholding, dilating etc of the image.  
We can add two images by using function cv2.add(). This directly adds up image pixels in the two images.  
Syntax: cv2.add(img1, img2)
But adding the pixels is not an ideal situation. So, we use cv2.addweighted(). Remember, both images should be of equal size and depth.  
Syntax: cv2.addWeighted(img1, wt1, img2, wt2, gammaValue)Parameters: img1: First Input Image array(Single-channel, 8-bit or floating-point) wt1: Weight of the first input image elements to be applied to the final image img2: Second Input Image array(Single-channel, 8-bit or floating-point) wt2: Weight of the second input image elements to be applied to the final image gammaValue: Measurement of light 
Images used as Input:Input Image1: 
Input Image2: 
Below is the code:  
Python3
# Python program to illustrate# arithmetic operation of# addition of two images   # organizing importsimport cv2import numpy as np   # path to input images are specified and # images are loaded with imread commandimage1 = cv2.imread('input1.jpg')image2 = cv2.imread('input2.jpg') # cv2.addWeighted is applied over the# image inputs with applied parametersweightedSum = cv2.addWeighted(image1, 0.5, image2, 0.4, 0) # the window showing output image# with the weighted sumcv2.imshow('Weighted Image', weightedSum) # De-allocate any associated memory usage if cv2.waitKey(0) & 0xff == 27:    cv2.destroyAllWindows()
Output: 
 
Just like addition, we can subtract the pixel values in two images and merge them with the help of cv2.subtract(). The images should be of equal size and depth.  
Syntax:  cv2.subtract(src1, src2)
Images used as Input: Input Image1: 
Input Image2: 
Below is the code: 
Python3
# Python program to illustrate# arithmetic operation of# subtraction of pixels of two images # organizing importsimport cv2import numpy as np   # path to input images are specified and # images are loaded with imread commandimage1 = cv2.imread('input1.jpg')image2 = cv2.imread('input2.jpg') # cv2.subtract is applied over the# image inputs with applied parameterssub = cv2.subtract(image1, image2) # the window showing output image# with the subtracted imagecv2.imshow('Subtracted Image', sub) # De-allocate any associated memory usage if cv2.waitKey(0) & 0xff == 27:    cv2.destroyAllWindows()
Output: 
anikakapoor
Image-Processing
OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()