How to blend images using OpenCV

This recipe helps you blend images using OpenCV

Recipe Objective: How to blend images using OpenCV?

In this recipe, let us understand how to blend two images using OpenCV

Deep Learning Project for Text Detection in Images using Python

Step 1: Import the 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 the sizes of the images

Let us compare the size of the two images. We can blend two images only if they are in the same shape.

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

As we understand from the above output, the shapes of the two images are not the same. Hence we resize the second image as of the size of the first image using the cv2.resize() function. This function takes in the following parameters

  • src: Image which is to be resized
  • dsize: The size to which it should be resized. This is in the form of (width, height) tuple

image2_resized = cv2.resize(image2, (image1.shape[1],image1.shape[0]))
print(f"The shape of image 1 and image 2 (after resizing) is {image1.shape} and {image2_resized.shape} respectively")

Output:

    The shape of image 1 and image 2 (after resizing) is (110, 335, 3) and (110, 335, 3) respectively

The images are now in the same shape

Step 3: Blending the two images

The blending of two images is nothing but adding two images by giving a specific weight value to each image so that it gives a feeling of one image overlapping the other. This can be achieved using the cv2.addWeighted() function, which works based on the following equation

blended_image = α*img1 + β*img2 + γ

where α is the weight value of the first image, β is the weight of the second image, and γ is a scalar added to each sum.

The weight values generally range between 0 and 1

The cv2.addWeighted() function takes the following parameters

  • src1: First image
  • alpha: weight of the first image
  • src2: Second image
  • beta: Weight of the second image
  • gamma: scalar value

Let us consider gamma to be 0 in this example

blended_image = cv2.addWeighted(src1=image1,alpha=0.5,src2=image2_resized,beta=0.5,gamma=0) cv2.imshow('Blended Image',blended_image)
cv2.waitKey(0)

Output:

blended.jpg

And tada! We have successfully blended the two images!

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 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.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

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.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

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