Open In App

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

Similar Reads