etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Python OpenCV Overlaying or Blending Two Images

In this article, you will learn how to overlay or blend two images, one over another, using Python OpenCV.

OpenCV is a free, open source library that is used for computer vision. It provides good support for machine learning, face recognition, deep learning, etc. In the previous articles, we have used the OpenCV to perform different operations on images. This article is also important.

The blending of images means mixing two images. Overlaying an image over another refers to the method involved with copying the image information of one image over the other. The output image is a combination of the corresponding pixel values of the input images. The blending of one image over another is generally used in a variety of computer graphics and image processing applications.





Python OpenCV provides the cv2.addWeighted() method to achieve this goal. This method calculates the weighted sum of two arrays. It returns an image in the output, which is a combination of the corresponding pixel values of the input images. It has the following syntax-

cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])

src1: first input array,
alpha: weight of the first array elements,
src2: second input array with the same size and channel number as src1,
beta: weight of the second array elements,
gamma: scalar added to each sum,
dst: output array that has the same size and number of channels as the input arrays,
dtype: optional depth of the output array.





Code Explanation

These are the steps taken to overlay one image over another in Python OpenCV. First, we will load both images using the imread() method.

img1 = cv2.imread('forest.png')
img2 = cv2.imread('pigeon.png')

Next, we will blend the image using the cv2.addWeighted() method. Here, we have taken 0.5 weights of the first input image and 0.7 weights of the second input image.

dst = cv2.addWeighted(img1,0.5,img2,0.7,0)

The cv2.imshow() method is used to display an image in a window. It accepts 'window name' in the first parameter and 'image name' in the second parameter.

cv2.imshow('Blended Image',dst)

The given line waits for a keyboard button press using the cv2.waitKey() method.

cv2.waitKey(0)

Next, use the cv2.destroyAllWindows() method to exit the window and destroy all windows.

cv2.destroyAllWindows()




Complete Code: OpenCV Overlay or Blend Two Images

Here is the complete code for overlaying and blending two images using the Python OpenCV.

import cv2
import numpy as np

img1 = cv2.imread('forest.png')
img2 = cv2.imread('pigeon.png')
dst = cv2.addWeighted(img1, 0.5, img2, 0.7, 0)

img_arr = np.hstack((img1, img2))
cv2.imshow('Input Images',img_arr)
cv2.imshow('Blended Image',dst)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

The above code returns two windows, one window contains input images and the second contains a blended image.

OpenCV Image Blending
OpenCV Image Blending

Reference : https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html





Related Articles

Human Body Detection Program In Python OpenCV
Detect Specific Color From Image using Python OpenCV
Capture a video in Python OpenCV and save
Arithmetic Operations on Images using Python OpenCV
Adaptive Thresholding in Python OpenCV
Python OpenCV Erosion and Dilation
Bitwise and OpenCV
OpenCV color detection
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display mongodb data in html page




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to get current directory, filename and code line number in PHP
-----------------
How to add multiple custom markers on google map
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple pagination in PHP with MySQL
-----------------
How to generate QR Code in PHP
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
PHP MYSQL Advanced Search Feature
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP Server Side Form Validation
-----------------
jQuery File upload progress bar with file size validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
To check whether a year is a leap year or not in php
-----------------
Php file based authentication
-----------------
Simple File Upload Script in PHP
-----------------
Simple PHP File Cache
-----------------
PHP User Authentication by IP Address
-----------------
Calculate the distance between two locations using PHP
-----------------
PHP Secure User Registration with Login/logout
-----------------
Polling system using PHP, Ajax and MySql
-----------------
How to print specific part of a web page in javascript
-----------------
Detect Mobile Devices in PHP
-----------------
Simple Show Hide Menu Navigation
-----------------
Simple way to send SMTP mail using Node.js
-----------------
SQL Injection Prevention Techniques
-----------------
Get Visitor\'s location and TimeZone
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Google Street View API Example
-----------------
PHP Sending HTML form data to an Email
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
CSS Simple Menu Navigation Bar
-----------------
Date Timestamp Formats in PHP
-----------------
PHP Programming Error Types
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
How to add google map on your website and display address on click marker
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
PHP Getting Document of Remote Address
-----------------
How to display PDF file in web page from Database in PHP
-----------------
File Upload Validation in PHP
-----------------
PHP FTP Connection and File Handling
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.