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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

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.