I'm trying to stitch two pre-warped images together seamlessly using multi-band blending. I have two input images (that have already been warped) and one mask. However, when I apply MBB, the area surrounding the seams glow brighter and as a result, they become more visible which is the opposite of the objective here. I have absolutely no idea what I'm doing wrong.
To better explain the problem, here are the images and the output:
Target:
Source:
Mask:
And once I blend the source image into the target, this is what I get:
Here's my code for reference:
import cv2 as cv2
import numpy as np
import sys
def blend(A, B, m, canvas, num_levels=6):
trimmer = cv2.bitwise_or(canvas, m) # to trim the blurry edges around the image after blending
m[m == 255] = 1
GA = A.copy()
GB = B.copy()
GM = m.copy()
gpA = [GA]
gpB = [GB]
gpM = [GM]
for i in range(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 range(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 = la + lb
LS.append(ls)
ls_ = LS[0]
for i in range(1, num_levels):
size = (LS[i].shape[1], LS[i].shape[0])
ls_ = cv2.add(cv2.pyrUp(ls_, dstsize=size), np.float32(LS[i]))
ls_[ls_ > 255] = 255; ls_[ls_ < 0] = 0
ls_ = ls_.astype(np.uint8)
cv2.imwrite("trimmer.jpg", trimmer)
ls_ = cv2.bitwise_and(ls_, trimmer)
return ls_
Canvas to pass to the function (basically the mask for the target/mosaic):
Mask for the source/new image:
I'm also open to exploring other ways to blend the two images seamlessly, in case MBB is not the most suitable method to achieve my goal. Please help.