warpPerspective overlapping one image over another on the background

So I want to stich two images in panorama style. So here is what I planned: Create a white image (ie : color = 255, 255, 255) that is twice as wide as image1 Copy img1 to the (top-)left of this result image, which means an identity transform in terms of pixel coordinates Copy img2 to the result image, by warping it with the inverse of the estimated homography (which maps dst_pts to src_pts)

However, the final result not only set the background to black, but also overlapped second image over first. What am I doing wrong?

The code is below

def panorama(img1, img2, H, size):
   I = np.linalg.inv(H)
   img = np.zeros(size,dtype=np.uint8)
   img.fill(255)
   img = cv2.warpPerspective(img1, np.identity(3), size, img, borderMode=cv2.BORDER_TRANSPARENT)
   img = cv2.warpPerspective(img2, I, size, img, flags=cv2.WARP_INVERSE_MAP+cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
   return img

size = (2 * image1.shape[0],image1.shape[1])
cv2.imshow([panorama(image1, image2, H, size)],["Panorama"])
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum