Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Open CV
  • scikit-image
  • pycairo
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Analysis
  • ML Math
  • Machine Learning
  • NLP
  • Deep Learning
  • Deep Learning Interview Questions
  • ML Projects
  • ML Interview Questions
  • 100 Days of Machine Learning
Open In App
Next Article:
Image Blending Using OpenCV
Next article icon

Image Blending Using OpenCV

Last Updated : 06 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Image blending is used in computer vision that allows us to combine two or more images over each other. For example in Augmented Reality (AR) this technique is used by AR Glasses to blend virtual objects over our reality. It is also used in creating panoramas, creating special effects and Photography.

Blending of Images Using OpenCV

To blend images using OpenCV, we will use weights over images to blend them. Here’s how we do it:

Python
import cv2 import matplotlib.pyplot as plt import requests from PIL import Image from io import BytesIO import numpy as np   # Image URLs image1_url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmKTFI_7r2TKR0sknfK_7GJX8sHItxbf-zh_jOJIBde2-L69K29IAzFLrD&s" image2_url = "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQLVI5oqw867VkKFwDFtPaTGx2iRyLI2xth6m96yXdszR9vcfGi"  response_image1 = requests.get(image1_url) response_image2 = requests.get(image2_url) img1 = Image.open(BytesIO(response_image1.content)) img2 = Image.open(BytesIO(response_image2.content))  image1 = cv2.cvtColor(np.array(img1), cv2.COLOR_RGB2BGR) image2 = cv2.cvtColor(np.array(img2), cv2.COLOR_RGB2BGR)  image1 = cv2.resize(image1, (500, 500)) image2 = cv2.resize(image2, (500, 500))  # Set blending weights alpha = 0.5   beta = 0.5    blended_image = cv2.addWeighted(image1, alpha, image2, beta, 0) plt.figure(figsize=(15, 5))  plt.subplot(1, 3, 1) plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)) plt.title('Image 1') plt.axis('off')  plt.subplot(1, 3, 2) plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)) plt.title('Image 2') plt.axis('off')  plt.subplot(1, 3, 3) plt.imshow(cv2.cvtColor(blended_image, cv2.COLOR_BGR2RGB)) plt.title('Blended Image') plt.axis('off')  plt.show() 

Output:

Figure_10
Demonstration of Blended image

Here Alpha and Beta refers to value (weights) for Image 1 and Image 2 respectfully. For above example, we have set the value of Alpha and Beta as 0.5.

Now lets see how different value of Alpha and Beta affects Blended Image.

Python
import cv2 import matplotlib.pyplot as plt import requests from PIL import Image from io import BytesIO import numpy as np   # Image URLs image1_url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmKTFI_7r2TKR0sknfK_7GJX8sHItxbf-zh_jOJIBde2-L69K29IAzFLrD&s" image2_url = "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQLVI5oqw867VkKFwDFtPaTGx2iRyLI2xth6m96yXdszR9vcfGi"  response_image1 = requests.get(image1_url) response_image2 = requests.get(image2_url) img1 = Image.open(BytesIO(response_image1.content)) img2 = Image.open(BytesIO(response_image2.content))  image1 = cv2.cvtColor(np.array(img1), cv2.COLOR_RGB2BGR) image2 = cv2.cvtColor(np.array(img2), cv2.COLOR_RGB2BGR)  image1 = cv2.resize(image1, (500, 500)) image2 = cv2.resize(image2, (500, 500))  # Changing the values of alpha and beta  alpha = 0.8   beta = 1-alpha  # beta=0.2  blended_image = cv2.addWeighted(image1, alpha, image2, beta, 0) plt.figure(figsize=(15, 5))  plt.subplot(1, 3, 1) plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)) plt.title('Image 1') plt.axis('off')  plt.subplot(1, 3, 2) plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)) plt.title('Image 2') plt.axis('off')  plt.subplot(1, 3, 3) plt.imshow(cv2.cvtColor(blended_image, cv2.COLOR_BGR2RGB)) plt.title('Blended Image') plt.axis('off')  plt.show() 

Output:

Figure_11
Changing the blended image due to change of values of alpha and beta

Now when we increased weights of Image 1 i.e Alpha value to 0.8 we can see prominence of Image 1 in Blended Image. Weights of Alpha and Beta combined should not exceed 1.

Image blending with OpenCV is a easy to use technique used in Computer Vision. By experimenting with different weights and images, we can achieve different results.


Next Article
Image Blending Using OpenCV

S

sahilgupta03
Improve
Article Tags :
  • Computer Vision
  • AI-ML-DS
  • Python-OpenCV
  • AI-ML-DS With Python

Similar Reads

    Live Webcam Drawing using OpenCV
    Let us see how to draw the movement of objects captured by the webcam using OpenCV. Our program takes the video input from the webcam and tracks the objects we are moving. After identifying the objects, it will make contours precisely. After that, it will print all your drawing on the output screen.
    3 min read
    Python | Grayscaling of Images using OpenCV
    Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white.Importance of grayscaling Dimension reduction: For example, In RGB images there are three color channels and three dimensions whi
    5 min read
    Color Identification in Images using Python - OpenCV
    An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Don’t you
    3 min read
    Reading an image in OpenCV using Python
    Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste
    6 min read
    Drawing a cross on an image with OpenCV
    In this article, we are going to discuss how to draw a cross on an image using OpenCV-Python. We can draw an overlay of two lines one above another to make a cross on an image.   To draw a line on OpenCV, the below function is used. Syntax: cv2.line(image, starting Point, ending Point, color, thickn
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences