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

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

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.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.