我正在尝试使用opencv-python包进行单应性透视转换。
我有一个背景和前景图像,并希望执行透视变换并在给定四(x,y)坐标的背景图像上缝合前景图像,如下所示:
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)
使用cv2.findhomography()完成透视转换后,我使用适当的蒙版掩盖前景和背景图像并将它们添加到一起,如下所示:
def showFinal(src1,src2):
grayed = cv2.cvtColor(src2,cv2.COLOR_BGR2GRAY)
_,grayed = cv2.threshold(灰色,0,255,cv2.THRESH_BINARY)
grayedInv = cv2.bitwise_not(灰色)
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)
问题
问题是查找结果是错误的,因为变换的前景图像没有缝合在我用于查找单应性的四个坐标内。
任何人都可以指导我为什么会发生这种错误?
预期产出
实际产出