numpy进行快速坐标透视变换

发布于 2022-11-12  244 次阅读


对于一个透视变换矩阵,可以将其写作一个向量的形式,那么对于一个坐标(i,j),可以通过以下方式进行透视变换:

def axisTransform(i: int, j: int, perMat: np.array) -> Tuple[float]:
    """使用变换矩阵映射坐标

    Args:
        i (int): 行号
        j (int): 列号
        perMat (np.array): 变换矩阵

    Returns:
        Tuple[float]: 以浮点数形式返回变换后的 (i, j)
    """
    a = i * perMat[0] + j * perMat[1] + perMat[2]
    b = i * perMat[3] + j * perMat[4] + perMat[5]
    c = i * perMat[6] + j * perMat[7] + perMat[8]
    return a / c, b / c

以上方式即使使用numba进行加速后,在进行多点运算时仍然需要消耗很多时间,可以直接将上述过程写作矩阵操作的形式,如下:

def axisTransform(i: int, j: int, perMat: np.array) -> Tuple[float]:
    """使用变换矩阵映射坐标

    Args:
        i (int): 行号
        j (int): 列号
        perMat (np.array): 变换矩阵

    Returns:
        Tuple[float]: 以浮点数形式返回变换后的 (i, j)
    """
    x = np.array([i,j,1])
    y = np.array(perMat).reshape(3, 3)
    t = np.dot(x,y.T)
    return t[0] / t[2], t[1] / t[2]
「雪霁融雾月,冰消凝夜雨」
最后更新于 2022-11-12