I am trying to blend two images, given a mask, using the following script:
import cv2
import numpy as np
def pyramid_blend(A, B, m, num_levels):
GA = A.copy()
GB = B.copy()
GM = m.copy()
gpA = [GA]
gpB = [GB]
gpM = [GM]
for i in xrange(num_levels):
GA = cv2.pyrDown(GA)
GB = cv2.pyrDown(GB)
GM = cv2.pyrDown(GM)
gpA.append(np.float32(GA))
gpB.append(np.float32(GB))
gpM.append(np.float32(GM))
lpA = [gpA[num_levels - 1]]
lpB = [gpB[num_levels - 1]]
gpMr = [gpM[num_levels - 1]]
for i in xrange(num_levels - 1, 0, -1):
size = (gpA[i - 1].shape[1], gpA[i - 1].shape[0])
LA = np.subtract(gpA[i - 1], cv2.pyrUp(gpA[i], dstsize=size))
LB = np.subtract(gpB[i - 1], cv2.pyrUp(gpB[i], dstsize=size))
lpA.append(LA)
lpB.append(LB)
gpMr.append(gpM[i - 1])
LS = []
for la, lb, gm in zip(lpA, lpB, gpMr):
ls = la * gm + lb * (1.0 - gm)
LS.append(ls)
ls_ = LS[0]
for i in xrange(1, num_levels):
size = (LS[i].shape[1], LS[i].shape[0])
ls_ = cv2.add(cv2.pyrUp(ls_, dstsize=size), np.float32(LS[i]))
return ls_
if __name__ == '__main__':
A = cv2.imread('./black.jpg')
B = cv2.imread('./white.jpg')
m = cv2.imread('./mask.jpg')
lpb = pyramid_blend(A, B, m, 6)
What i did:
- Find the Gaussian Pyramids of the images.
- From Gaussian Pyramids, find their Laplacian Pyramids
- Join the left half and right half of images in each levels of Laplacian Pyramids using mask.
- From this joint image pyramids, reconstruct the original image.
The images are used -
The result i get -
For some reason, and i dont understand why, the colors of the result image are completely off.