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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

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

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.