使用cv2.findhomography()进行透视变换会导致坐标错误

2024-09-28 13:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用opencvpython包执行单应性透视转换。在

我有一个背景和前景图像,并希望执行透视变换,并在给定四个(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()完成透视变换后,我使用适当的遮罩遮罩前景和背景图像,并将它们添加到一起,如下所示:

^{pr2}$

问题

问题是,查找结果是错误的,因为转换后的前景图像没有缝合到我用来查找单应性的四个坐标内。在

有人能告诉我为什么会发生这个错误吗?在

预期产量

enter image description here

实际产量

enter image description here


Tags: 图像cv2背景x1appendimread前景y1

热门问题