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

Why project 3d to 2d out of image plane while the point lies on the image

我正在尝试在给定 Unreal engine 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]]

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

原来这是一个错误。实际世界坐标系为x(从屏幕外向内递增),y(从左向右递增), z(从下往上递增).

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