【问题标题】:Perspective projection with homography not working单应性透视投影不起作用
【发布时间】:2018-12-09 07:42:46
【问题描述】:

我正在尝试用浅黄色多边形替换下图右侧的道路:

起源

需要(手动创建):

所以我正在考虑使用 Homography 来实现它(我知道添加一个填充的多边形也可以,但我可能想使用一些其他源图像而不是简单的黄色多边形,例如未来的广告图片)。 这是一个关于它的tutorial,我只是复制其中的代码并对像素进行了一些更改。我使用的源图片是这样的:

这篇文章中的第一张图片是我的目标图片。

这是我完成这项工作的代码:

import cv2
import numpy as np

# source image
source_img = cv2.imread('lightyellow.jpg')
# get four corners of the source (clock wise)
pts_source = np.array([[0,0], [20,0], [20,30],[0,30]])

# destination image
dst_img = cv2.imread('0.png')
# four corners in destination image (also clock wise):
pts_dst = np.array([[292,0], [415,0], [578,120],[415,189]])

# calculate homography
h, status = cv2.findHomography(pts_source, pts_dst)

# warp source image to destination based on homography
img_out = cv2.warpPerspective(source_img, h, (dst_img.shape[1], dst_img.shape[0]))

cv2.imshow('warped', img_out)
cv2.waitKey(0)

但是,我得到的是这样的:

这是完全错误的,但我不知道为什么。有人可以给我一些指导吗?

【问题讨论】:

    标签: python opencv projection perspective homography


    【解决方案1】:

    我终于用下面的代码做到了:

    import cv2
    import numpy as np
    
    # source image
    source_img = cv2.imread('lightyellow.jpg')
    size = source_img.shape
    # get four corners of the source (clock wise)
    pts_source = np.array(
                        [
                        [0,0],
                        [size[1] - 1, 0],
                        [size[1] - 1, size[0] -1],
                        [0, size[0] - 1 ]
                        ],dtype=float
                        )
    #pts_source = np.array([[310,0], [440,0], [589,151],[383,151]])
    
    # destination image
    dst_img = cv2.imread('0.png')
    # four corners in destination image (also clock wise):
    pts_dst = np.array([[292,0], [409,0], [577,191],[421,193]])
    
    # calculate homography
    h, status = cv2.findHomography(pts_source, pts_dst)
    
    # warp source image to destination based on homography
    temp = cv2.warpPerspective(source_img, h, (dst_img.shape[1], dst_img.shape[0]))
    
    # Black out polygonal area in destination image.
    cv2.fillConvexPoly(dst_img, pts_dst.astype(int), 0, 16)
    
    # Add warped source image to destination image.
    dst_img = dst_img + temp
    
    cv2.imshow('warpped', dst_img)
    cv2.waitKey(0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2014-07-06
      • 2018-05-03
      相关资源
      最近更新 更多