当点位于图像上时,为什么要在图像平面外投影3d到2d

时间:2019-06-05 16:06:50

标签: perspectivecamera

考虑到虚幻引擎4中的3D世界坐标,我试图获取图像平面上的2D坐标。

世界坐标系为 x (从右向左递增), y (从外向内递增), z (从下到上增加)

我想要的图像坐标系是左下角的原点, x (从左到右递增), y (从下到上递增)

当相机位于

上时,我将相机的内部和外部设置如下
(-474.9739990234375, -67.140998840332031, 128.10400390625)

,其旋转角度为

(0, 0.80071002244949341, 99.797645568847656)

与世界坐标的原点有关。

import numpy as np

fov = 69.4
width = 1920.0
height = 1080.0
aspect_ratio = width / height
fx = width / (2 * np.tan(fov * np.pi / 2 / 180))
fy = height / (2 * np.tan(fov / aspect_ratio / 2 / 180 * np.pi))
intrinsic = [[-fx, 0, width / 2], [0, fy, height / 2], [0 , 0, 1]]

pitch = 0.80071002244949341 * np.pi / 180
yaw = 99.797645568847656 * np.pi / 180
roll = 0 * np.pi / 180

ry = [[np.cos(pitch), 0, np.sin(pitch)], [0, 1, 0], [-np.sin(pitch), 0, np.cos(pitch)]]
rx = [[1, 0, 0], [0, np.cos(roll), -np.sin(roll)], [0, np.sin(roll), np.cos(roll)]]
rz = [[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]
rotation_matrix = np.matmul(np.matmul(rx, ry), rz)

translation_matrix = np.array([[-474.9739990234375, -67.140998840332031, 128.10400390625]])

extrinsic = np.concatenate([rotation_matrix, translation_matrix.T], axis=1)
extrinsic = np.concatenate([extrinsic, np.array([[0, 0, 0, 1]])], axis=0)
extrinsic = np.linalg.inv(extrinsic)
extrinsic = extrinsic[:3]
print(extrinsic)

假设我在场景中也有一个对象,并且该对象也在相机视图中。 它的坐标是

[-582.88897705078125, 143.63600158691406, 88.338996887207031]

使用下面的代码,我应该获得3D点在图像平面上的投影。

world_coordinate = np.array([[-582.88897705078125, 143.63600158691406, 88.338996887207031, 1]])
camera_coordinate = np.dot(extrinsic, world_coordinate.T)
print(camera_coordinate)
camera_coordinate[0] *= 1
tmp = camera_coordinate[1].copy()
camera_coordinate[1] = camera_coordinate[2]
camera_coordinate[2] = tmp
print(camera_coordinate)
image_coordinate = np.dot(intrinsic, camera_coordinate)
image_coordinate /= image_coordinate[2]
print(image_coordinate)

但是我知道了

[[-3.52097326e+03]
 [-3.59180874e+02]
 [ 1.00000000e+00]]

哪个不在图像平面上。我该如何解决?

1 个答案:

答案 0 :(得分:0)

结果证明这是一个错误。实际的世界坐标系为 x (从外向内增加), y (从左向右增加), z (从从下到上)。

只需将yz平面的项目映射到图像空间即可。