Image Blending Example with OpenCV in Python

    Image blending defines the process of combining two or more images to generate a new image with smooth transitions between them. It is commonly used in various applications such as creating panorama images, image morphing, and special effects in computer graphics.

    The purpose of image blending is to merge
seamlessly the content of multiple images while maintaining the visual coherence and smoothness. This can be achieved by considering the pixel values of the overlapping regions in the images and blending them together based on a specific blending technique or algorithm.

    In this tutorial, you'll briefly learn how to combine images by using OpenCV functions in Python. The tutorial covers:

  1. Simple blending
  2. Weighted blending
  3. Mask blending
  4. Source code listing

    We'll start by loading the required libraries.


import cv2
from matplotlib import pyplot as plt 
 
 
   The following code shows how to load images, resize them and display in graph.
 

file1 = "forest.jpg" 
file2 = "birds.jpg" 
 
 
# Load the two images
image1 = cv2.imread(file1)
image2 = cv2.imread(file2)
 
# Resize the images to the same size
image1 = cv2.resize(image1, (image2.shape[1], image2.shape[0]))
 
# Display the the images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB))
plt.title('Image1')
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB))
plt.title('Image2')
plt.tight_layout()
plt.show() 

 

 


Simple blending
 
    In simple blending, we combine images by using cv2.add() function. The cv2.add() function performs pixel-wise addition of two images or an image and a scalar value. It adds the corresponding pixel values of the input images, resulting in a new image with the same size. The following shows how to add images and display the output image.
 
  
# Perform simple blending
blended = cv2.add(image1, image2)

# Display the blended image

plt.imshow(cv2.cvtColor(blended, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show() 
   


 
Weighted blending

    In weighted blending, we set alpha and beta weights for image1 and image2 respectively. We can use cv2.addWeighted() function of OpenCV library for blending images. It helps to blend two images together using different weights for each image, resulting in a weighted combination. Below code shows how to perform weighted blending and display the blended image.

 
# Set the weight values
alpha = 0.7
beta = 0.3

# Perform weighted blending
blended = cv2.addWeighted(image1, alpha, image2, beta, 0)

plt.imshow(cv2.cvtColor(blended, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show()
  


Mask blending

    In this method, we first create mask based on image2, and apply the mask for both images. Finally, we combine image to create blended image. Below code shows how to blend by masking and display the output image.

 
# Create a binary mask based on image2's transparency (assume white pixels are opaque)
mask = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(mask, 1, 255, cv2.THRESH_BINARY)

# Apply the mask to images and invert the mask
foreground = cv2.bitwise_and(image2, image2, mask=mask)
background = cv2.bitwise_and(image1, image1, mask=mask)

# Combine the foreground and background
result = cv2.add(foreground, background)

plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show()
 

    In this tutorial, you've briefly learned how to perform image blending with OpenCV functions in Python. The full source code is listed below. 

 
Source code listing
 
  
import cv2
from matplotlib import pyplot as plt


file1 = "forest.jpg" 
file2 = "birds.jpg"  
 
# Load the two images
image1 = cv2.imread(file1)
image2 = cv2.imread(file2)  
 
# Resize the images to the same size
image1 = cv2.resize(image1, (image2.shape[1], image2.shape[0])) 
 
# Display the the images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB))
plt.title('Image1')
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB))
plt.title('Image2')
plt.tight_layout()
plt.show()
 
 
# Simple blending
# Perform simple blending
blended = cv2.add(image1, image2)

# Display the blended image
plt.imshow(cv2.cvtColor(blended, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show() 


# Weighted blending
# Set the weight values
alpha = 0.7
beta = 0.3

# Perform weighted blending
blended = cv2.addWeighted(image1, alpha, image2, beta, 0)

plt.imshow(cv2.cvtColor(blended, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show()
 

# Mask blending
# Create a binary mask based on image2's transparency (assume white pixels are opaque)
mask = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(mask, 1, 255, cv2.THRESH_BINARY)

# Apply the mask to images and invert the mask
foreground = cv2.bitwise_and(image2, image2, mask=mask)
background = cv2.bitwise_and(image1, image1, mask=mask)

# Combine the foreground and background
result = cv2.add(foreground, background)

plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.title("Blended Image")
plt.show()
       
 
 

 

No comments:

Post a Comment