I am trying to perform perspective transformation with homography using the opencv-python package.
I have a background and foreground image and would like to perform perspective transform and stitch the foreground image on the background image given four (x, y) coordinates, as follow:
bgImg = cv2.imread(BACK_PATH, cv2.IMREAD_COLOR)
fgImg = cv2.imread(FORE_PATH, cv2.IMREAD_COLOR)
bgHeight, bgWidth, dpt = bgImg.shape
origImageCoords = np.array([(0, 0),
(0, bgHeight),
(bgWidth, bgHeight),
(bgWidth, 0)])
stitchingCoords = []
def transformPerspective():
y0 = 285
y1 = 400
x0 = 447
x1 = 600
stitchingCoords.append((x0, y0))
stitchingCoords.append((x0, y1))
stitchingCoords.append((x1, y1))
stitchingCoords.append((x1, y0))
homography = cv2.findHomography(origImageCoords, np.array(stitchingCoords))
dst_corners = cv2.warpPerspective(src=fgImg, M=homography[0], dsize=(bgWidth, bgHeight))
showFinal(bgImg, dst_corners)
After the perspective transformation is done using cv2.findhomography()
, I mask the foreground and background images using appropriate masks and add them together, as follow:
def showFinal(src1, src2):
grayed = cv2.cvtColor(src2, cv2.COLOR_BGR2GRAY)
_, grayed = cv2.threshold(grayed, 0, 255, cv2.THRESH_BINARY)
grayedInv = cv2.bitwise_not(grayed)
src1final = cv2.bitwise_and(src1, src1, mask=grayedInv)
src2final = cv2.bitwise_and(src2, src2, mask=grayed)
finalImage = cv2.add(src1final, src2final)
cv2.namedWindow("output", cv2.WINDOW_AUTOSIZE)
cv2.imshow("output", finalImage)
Problem
The problem is the that the find result is wrong because the transformed-foreground image is not stitched inside the four coordinates that I used for finding the homography.
Could anyone guide me as to why this error is occurring?