已知内在参数的给定图像上的“鸟瞰”变换

2024-10-02 08:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对给定的图像进行鸟瞰视图转换。 使用cv2.calibrateCameracv2.solvePnPRansac,我知道相机的内在参数

我发现很多帖子都提出了很多不同的答案,但没有一个对我有用。我尝试过的一些帖子示例:

  1. Python create image with new camera position
  2. Bird's eye view perspective transformation from camera calibration opencv python
  3. OpenCV: get perspective matrix from translation & rotation

它们都不起作用,一些产生黑色图像,而另一些产生奇怪的扭曲,毫无意义。我知道我计算的内在参数是正确的,因为我可以将模型重新投影到原始图像点上

此图像的相机矩阵:

camera matrix

旋转矢量:

rotation vector

翻译向量:

enter image description here

我使用的最新代码如下:

def get_birds_eye_view(image, camera_matrix, rotation_vec, translation_vec):

    dst = np.zeros_like(image)

    dx, dy, dz = translation_vec

    A1= np.matrix([[1, 0, -image.shape[1]/2],
                    [0, 1, -image.shape[0]/2],
                    [0, 0, 0   ],
                    [0, 0, 1   ]])

    T = np.matrix([[1,0,0,dx],
                    [0,1,0,dy],
                    [0,0,1,dz],
                    [0,0,0,1]])

    rotation_matrix = cv2.Rodrigues(rotation_vec)[0]

    rotation_matrix = np.c_[ rotation_matrix, np.zeros(3)]
    rotation_matrix = np.r_[ rotation_matrix, [[0, 0, 0, 1]] ]
    camera_matrix = np.c_[ camera_matrix, np.zeros(3)]

    invers_camera_matrix = np.zeros((4,3))
    invers_camera_matrix[:3,:3] = np.linalg.inv(camera_matrix[:3,:3])
    invers_camera_matrix[-1,:] = [0, 0, 1]

    H = np.linalg.multi_dot([camera_matrix, rotation_matrix, T, invers_camera_matrix])

    warped_image = cv2.warpPerspective(image, H, (image.shape[1], image.shape[0]), dst, cv2.INTER_NEAREST, cv2.BORDER_CONSTANT, 0)

    return warped_image

示例图像:

enter image description here

输出为黑色: enter image description here

所需的输出可能如下所示(但对于坏的而不是棋盘,从第二个链接获取的图像):

enter image description here

如果缺少任何信息,请评论,我将提供数据


Tags: 图像image示例参数npzeroscv2translation

热门问题