cv2.warpPerspective 有点偏离?

问题描述

总的来说,我对 OpenCV 和图像处理比较陌生。有很多关于使用点扭曲图像以使其“自上而下”的教程。对于我的项目,我有一些计算机显示器的图像,我试图让我选择的图像看起来实际上显示在其中一个显示器上。它几乎有效,但转换似乎有点偏差。

我仔细检查了这些点以确保它们是正确的,所以我认为这与我的矩阵有关。这是我的 Python 代码:

def normalizeImage(image,monitorID,monitorPts,monitors):
    # This function will resize any image to the correct 
    # height and width to prepare it for being reshaped
    rows,cols,_ = image.shape
    pts1 = np.array([[0,0],[0,cols],[rows,0]],dtype=np.float32)
    pts2 = monitorPts[monitorID]
    matrix = cv2.getPerspectiveTransform(pts1,pts2) # I think my issue is here!!<<<<<<<<<<
    # matrix equals:
    # | .497 .072  27|
    # |-.045 .271 204|
    # |    0    0   1|
    dst = cv2.warpPerspective(image,matrix,(monitors.shape[1],monitors.shape[0]))
    cv2.imshow("dst",dst)
    return dst

    
def combineImages(monitors,image): # display shape of all three images for comparison
    dst = cv2.add(image,monitors)
    return dst


def main():
    monitors = cv2.imread("images/monitors.jpg")
    image0 = cv2.imread("images/goldglasses.png")
    monitorID = 0 # Which monitor are we adding an image to?
    # monitors.shape should be (694,1431,3)
    print("Monitors:")
    print(monitors.shape)
    # Point locations for all three monitors. We're only worried about number 0 for now.
    monitorPts = (np.array([[27,204],[84,411],[418,301],[393,129]],dtype=np.float32),np.array([[436,96],[453,325],[847,319],[850,93]],np.array([[911,93],[878,318],[1259,426],[1327,150]],dtype=np.float32))
    image0 = normalizeImage(image0,monitors)
    monitors = combineImages(monitors,image0)
    cv2.imshow("monitors",monitors)
    cv2.waitKey(0)

if __name__ == "__main__":
    # execute only if run as a script
    main()

我认为这可能与矩阵转换完成的顺序有关,但我不确定。至少,我 99% 确定问题出在“normalizeImage”函数中。

这是两张源图片:

金眼镜.png:

enter image description here

监视器.jpg:

enter image description here

此外,这是我目前得到的结果:

newMonitors.png:

enter image description here

请注意,这家伙完全不适合显示器。任何帮助表示赞赏。如果您有任何澄清问题,请随时提出。也非常感谢我的方法的任何可能更容易的替代方法。我真的很难理解其中一些函数的大量文档,所以如果有更好的方法来完成所有这些,我不会感到惊讶。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)