对具有已知内在参数的给定图像进行“鸟瞰”变换

问题描述 投票:0回答:1

我正在尝试对给定图像进行鸟瞰变换。 使用

cv2.calibrateCamera
cv2.solvePnPRansac
,我知道相机的内在参数。

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

  1. Python 使用新的相机位置创建图像
  2. 摄像机标定opencv python的鸟瞰透视变换
  3. OpenCV:通过平移和旋转获取透视矩阵

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

该图像的相机矩阵:

旋转矢量:

翻译向量:

我使用的最新代码是这样的:

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

示例图片:

输出只是黑色:

所需的输出可能如下所示(但棋盘不好,图像取自第二个链接):

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

python opencv image-processing camera-calibration homography
1个回答
0
投票

看看这个页面,你就会找到答案。 也许你的T不好。 https://gist.github.com/anujonthemove/7b35b7c1e05f01dd11d74d94784c1e58

其实我不明白为什么这个T要设置为500。

© www.soinside.com 2019 - 2024. All rights reserved.