Saturday 20 March 2021

opencv 18 blending image with pyramids




stacked image

blended image


image pyramid (same image with different resolutions)

Laplacian pyramid (low resolution image + edge detection at different resolution) 
#main.py
import numpy as np
import cv2

mountain1 = cv2.imread('assets/mountain1.jpg')
mountain1 = cv2.resize(mountain1, (1200, 800))
#print(mountain1.shape)
mountain2 = cv2.imread('assets/mountain2.jpg')
mountain2 = cv2.resize(mountain2, (1200, 800))
#print(mountain2.shape)

mountain_stack = np.hstack((mountain1[:, :700], mountain2[:, 700:]))

#generate Gaussian pyramid for mountain1
mountain1_copy = mountain1.copy()
mountain1_pyramid = [mountain1_copy]
for i in range(6):
    #decrease image resolution
    mountain1_copy = cv2.pyrDown(mountain1_copy)
    mountain1_pyramid.append(mountain1_copy)

#generate Gaussian pyramid for mountain2
mountain2_copy = mountain2.copy()
mountain2_pyramid = [mountain2_copy]
for i in range(6):
    #decrease image resolution
    mountain2_copy = cv2.pyrDown(mountain2_copy)
    mountain2_pyramid.append(mountain2_copy)

#generate Laplacina Pyramid for mountain1
mountain1_copy = mountain1_pyramid[5]
lp_mountain1 = [mountain1_copy]
for i in range(5, 0, -1):
    resolution_up = cv2.pyrUp(mountain1_pyramid[i])
    shape = mountain1_pyramid[i-1].shape
    resolution_up = cv2.resize(resolution_up, (shape[1], shape[0]))
    laplacian = cv2.subtract(mountain1_pyramid[i-1], resolution_up)
    lp_mountain1.append(laplacian)

#generate Laplacina Pyramid for mountain2
mountain2_copy = mountain2_pyramid[5]
lp_mountain2 = [mountain2_copy]
for i in range(5, 0, -1):
    resolution_up = cv2.pyrUp(mountain2_pyramid[i])
    shape = mountain2_pyramid[i - 1].shape
    resolution_up = cv2.resize(resolution_up, (shape[1], shape[0]))
    laplacian = cv2.subtract(mountain2_pyramid[i-1], resolution_up)
    lp_mountain2.append(laplacian)

#stack laplacian images
lp_pyramid = []
for mount1_lap, mount2_lap in zip(lp_mountain1, lp_mountain2):
    row, col, ch = mount1_lap.shape
    #print(mount1_lap.shape)
    lp_stack = np.hstack((mount1_lap[:, :int(col*7/12)], mount2_lap[:, int(col*7/12):]))
    #print(lp_stack.shape)
    lp_pyramid.append(lp_stack)

#reconstruct stacked image
#reconstruct[0] has a low resolution stacked image
#reconstruct[1] is generated by adding scaled up reconstruct[0] and corresponding laplacian edges
#reconstruct[2] is generated by adding scaled up reconstruct[1] and corresponding laplacian edges
reconstruct = lp_pyramid[0]
for i in range(1, 6):
    reconstruct = cv2.pyrUp(reconstruct)
    #print(reconstruct.shape)
    #print(lp_pyramid[i].shape)
    shape = lp_pyramid[i].shape
    reconstruct = cv2.resize(reconstruct, (shape[1], shape[0]))
    reconstruct = cv2.add(lp_pyramid[i], reconstruct)

cv2.imshow('mountain1', mountain1)
cv2.imshow('mountain2', mountain2)
cv2.imshow('mountain stack', mountain_stack)

"""
for i in range(6):
    image_name = 'mountain2 pyramid' + str(i)
    cv2.imshow(image_name, mountain2_pyramid[i])

for i in range(5, -1, -1):
    image_name = 'mountain laplacian ' + str(i)
    cv2.imshow(image_name, lp_mountain1[i])
"""

cv2.imshow('blended image', reconstruct)

cv2.waitKey(0)
cv2.destroyAllWindows()

reference:

No comments:

Post a Comment