有没有一种方法可以根据孩子的透视变换自动将透视变换应用于父对象?

时间:2019-06-13 06:15:48

标签: python opencv

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

explanatory image

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

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

我希望还可以基于子矩形转换(如虚线轮廓)为父对象获得正确的透视图。

1 个答案:

答案 0 :(得分:0)

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

相关问题