The Wayback Machine - https://web.archive.org/web/20240807153754/https://www.geeksforgeeks.org/arithmetic-operations-on-images-using-opencv-set-1-addition-and-subtraction/
Open In App

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

Last Updated : 04 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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. 
 

Addition of 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 imports 
import cv2 
import numpy as np 
    
# path to input images are specified and  
# images are loaded with imread command 
image1 = cv2.imread('input1.jpg') 
image2 = cv2.imread('input2.jpg')
  
# cv2.addWeighted is applied over the
# image inputs with applied parameters
weightedSum = cv2.addWeighted(image1, 0.5, image2, 0.4, 0)
  
# the window showing output image
# with the weighted sum 
cv2.imshow('Weighted Image', weightedSum)
  
# De-allocate any associated memory usage  
if cv2.waitKey(0) & 0xff == 27: 
    cv2.destroyAllWindows() 


Output: 

 

Subtraction of Image:

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 imports 
import cv2 
import numpy as np 
    
# path to input images are specified and  
# images are loaded with imread command 
image1 = cv2.imread('input1.jpg') 
image2 = cv2.imread('input2.jpg')
  
# cv2.subtract is applied over the
# image inputs with applied parameters
sub = cv2.subtract(image1, image2)
  
# the window showing output image
# with the subtracted image 
cv2.imshow('Subtracted Image', sub)
  
# De-allocate any associated memory usage  
if cv2.waitKey(0) & 0xff == 27: 
    cv2.destroyAllWindows() 


Output: 



Previous Article
Next Article

Similar Reads

Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Addition and Blending of images using OpenCV in Python
When we talk about images, we know its all about the matrix either binary image(0, 1), gray scale image(0-255) or RGB image(255 255 255). So additions of the image is adding the numbers of two matrices. In OpenCV, we have a command cv2.add() to add the images. Below is code for Addition of two images using OpenCV : # Python program for adding # ima
2 min read
Addition and Subtraction on TimeDelta objects using Pandas - Python
TimeDelta module is used to represent the time in the pandas module and can be used in various ways. Performing operations like addition and subtraction are very important for every language but performing these tasks on dates and time can be very valuable. Operations on TimeDelta dataframe or series - 1) Addition - df['Result'] = df['TimeDelta1']
2 min read
Perform addition and subtraction using CherryPy
CherryPy also known as a web application library is a Python web framework that provides a friendly interface to the HTTP protocol for Python developers. It allows developers to build web applications the same way as in traditional object-oriented Python programs. Thereby, resulting in smaller source code developed in no time. This framework is mai
2 min read
Arithmetic operations using OpenCV | Python
Prerequisite: Arithmetic Operations on Images using OpenCV | Basics We can perform different Arithmetic operations on images e.g. Addition, Subtraction, etc. This is possible because images are actually stored as arrays (3 Dimensional for RGB images and 1 dimensional for the grayscale images). Importance of Arithmetic Operations on images: Image Bl
2 min read
Python | Background subtraction using OpenCV
Background Subtraction has several use cases in everyday life, It is being used for object segmentation, security enhancement, pedestrian tracking, counting the number of visitors, number of vehicles in traffic etc. It is able to learn and identify the foreground mask.As the name suggests, it is able to subtract or eliminate the background portion
2 min read
Background subtraction - OpenCV
Background subtraction is a way of eliminating the background from image. To achieve this we extract the moving foreground from the static background. Background Subtraction has several use cases in everyday life, It is being used for object segmentation, security enhancement, pedestrian tracking, counting the number of visitors, number of vehicles
2 min read
Python OpenCV - Background Subtraction
Background Subtraction is one of the major Image Processing tasks. It is used in various Image Processing applications like Image Segmentation, Object Detection, etc. OpenCV provides us 3 types of Background Subtraction algorithms:- BackgroundSubtractorMOG BackgroundSubtractorMOG2 BackgroundSubtractorGMG Normally, we can perform background Subtract
2 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
Practice Tags :
three90RightbarBannerImg