How to add two images using OpenCV

This recipe helps you add two images using OpenCV

Recipe Objective: How to add two images using OpenCV?

Let us try to add two images using OpenCV

Access Face Recognition Project Code using Facenet in Python

Step 1: Import the libraries and read the image

The images that we are using for this recipe are as follows.

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

To add two images, the shape of those two images must be equal. Let us compare and see the shapes of the two 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

Since the two images are of different sizes, let us convert any image to the form of another using np.resize() function. It takes the array to be reshaped and the shape to which the given array is to be reshaped as two input parameters

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

Step 3: Add images

Now let us add these to images using the cv2.add() function, which takes the two images to be added as input.

added_images = cv2.add(image1,image2)

We have successfully added the two images.

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

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

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.