【问题标题】:Is there a way to apply perspective transformation automatically to parent based on perspective transformation of the child?有没有办法根据孩子的透视变换自动将透视变换应用于父级?
【发布时间】:2019-10-27 16:27:19
【问题描述】:

我正在编辑需要透视变换的图像。我有兴趣基于子透视变换对整个图像/容器应用/发出校正。

我可以找到仅转换感兴趣区域(黑色子矩形)的示例。

def four_point_transform(image, pts):

    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype = "float32")

    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    return warped

我希望基于子矩形变换也为父对象获得正确的透视,如虚线轮廓。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    使用 cv2.getPerspectiveTransform 返回的值先变换外层图像。根据结果​​,您可能不需要转换内部黑色矩形,因为它将与父图像一起转换。

    【讨论】:

    • 它可以纠正图像,但我只得到了一半的图像。我已经从:python warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)); # to; warped = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0])); 查看图片(仅供参考):result unexpected
    • 尝试打印 image.shape 以查看是否传递了正确的高度和宽度。另外,图像是黑色矩形还是父图像?
    • "image" 参数是整个图像/图片,包含所有图形(矩形),大小为 1600x1200px)。当我打印形状时,是图像尺寸。我相信我需要在“dst”变量中的某个地方工作。
    猜你喜欢
    • 2010-09-25
    • 2022-12-19
    • 2012-07-06
    • 2018-03-02
    • 2012-04-26
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多