Difference of adding two images using NumPy and OpenCV

This recipe explains what is the difference between adding two images using NumPy and using OpenCV. The main difference between NumPy and OpenCV addition is that NumPy addition is based on modulo operation while OpenCV uses saturated operation.

Recipe Objective: What is the difference between adding two images using NumPy and using OpenCV?

In this recipe, let us discuss the difference between adding two images using NumPy and using OpenCV.

Get Access to Plant Species Identification Project using Machine Learning

Step 1: Import libraries and read the images

The two images that we are using in this recipe are given below.

opencv logo

ProjectPro logo

import cv2
import numpy as np
image1 = cv2.imread('project.jpg')
image2=cv2.imread('OpenCV_Logo.jpg')

Step 2: Compare and alter the sizes

Images with different sizes can not be added, and hence we compare the sizes and resize one of the images.

print(f"The shape of image 1 and image 2 is {image1.shape} and {image2.shape} respectively")

Output:

The shape of image 1 and image 2 is (110, 335, 3) and (190, 340, 3) respectively

Hence we reshape the first image as the shape of the second image using the np.resize() function.

image1=np.resize(image1,image2.shape)

Step 3: Numpy addition vs OpenCV addition

We already know that the pixel arrays are stored in uint8 format. uint8 is nothing but an unsigned integer of 8 bits length. These generally hold values in the range of 0 to 255. The main difference between NumPy and OpenCV addition is that NumPy addition is based on modulo operation while OpenCV uses saturated operation. Let us break down the above statement with an example.

When we add two uint8 numbers using NumPy, we will get a modulus of the sum if the sum exceeds 255

For instance : 170 + 150 = 320 % 256 = 64

Here 320 is the actual sum, and since it is more significant than 255, 320 mod 256 is calculated, which gives 64

But when we add two uint8 numbers using the cv2.add() function, we would get the result as 255 if the sum exceeds 255.

For instance : 170 + 150 = 320 => 255

Here 320 is the actual sum, and 255 is the final result after saturation operation.

And hence, for this reason, it is always advisable to use cv2.add() function too add images over normal NumPy addition

numpy_addition = image1 + image2
cv2_addition = cv2.add(image1,image2)

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.