OpenCV is a Computer Vision library that helps you manipulate images and videos in a way of doing different processing techniques over them.

In this post, we are going to show you, how can you blend images, in order to create one image from a few others. Before reading this post about blend images, here we have another one about barcode detection using OpenCV.

The Image Blending Problem

In this post, we are going to use two pictures, and we are going to blend them into one picture. We are going to use Gaussian and Laplacian pyramids in order to resize the images.

An image pyramid is a collection of images, which arise from one source i.e. one original image. In order to create a pyramid, we need to downsample the source image until some desired stopping point is reached.

There are two types of image pyramids: Gaussian pyramid (Used to downsample images) and Laplacian pyramid (Used to reconstruct an upsampled image from an image lower in the pyramid (with less resolution)).

In our example, we are going to use both of these types of pyramids. With the Gaussian, we are going to downsample our images so later we can reconstruct them in order to lose some of their resolutions and their colors look more similar one to others.

 

Blend Images Using OPENCV

Image 1: Apple That We Will Use In This Image Blending Problem

 

How To Blend Image Using OpenCV, Gaussian and Laplacian Pyramid

Image 2: Orange That We Are Going To Use To Blend Images

 

import cv2
import numpy as np,sys

A = cv2.imread(‘apple.png’)
B = cv2.imread(‘orange.png’)

With the code above we read the images that we are going to use.

# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpA.append(G)

Then we generate the Gaussian pyramid for the image with the apple.

# generate Gaussian pyramid for B
G = B.copy()
gpB = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpB.append(G)

We generate a Gaussian pyramid for the image with the orange.

# generate Laplacian Pyramid for A

lpA = [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i-
1],GE)
    lpA.append(L)

# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i-
1],GE)
    lpB.append(L)

We generate a Laplacian pyramid for both of the images.

# Now add left and right halves of images in each level
LS = []
for la,lb in zip(lpA,lpB):
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,
0:int(cols/2)], lb[:,int(cols/2):]))
    LS.append(ls)


We add the halves of the images.
# now reconstruct
ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])
cv2.imshow(‘RESULT’,ls_)

Now we reconstruct the image, in order to get the result.

 

Results of Image Blending Using OpenCV, Gaussian and Laplacian Pyramid

Image 3: The Result Of The Image Blending

 

We can also blend images by just directly connecting the two images, with the code below.

# image with direct connecting each half
real = np.hstack((A[:,:int(cols/2)],B[:,int(cols/2):]))

 

Results of Direct Image Blending Using OpenCV, Gaussian and Laplacian Pyramid

Image 4: Result Of Direct Blending

 

As we can see the result of the direct blending is not nearly precise as the result we get by applying Gaussian and Laplacian pyramid.

If you want to see a video tutorial of this problem, we suggest you check this YouTube video.

Conclusion

This post is an example of how can you manipulate images by creating one image you like from a few others, by using programming code instead of some graphics editors like Photoshop or Gimp.

OpenCV is a very popular and powerful Computer Vision library, and we have other posts where we showcase examples of image processing using this library.

We hope that we spark a little interest in you so you will be learning more about Computer Vision since this field is one of the fastest-growing fields in the Artificial Intelligence discipline.

Like with every post we do, we encourage you to continue learning, trying and creating.

Facebook Comments