简体   繁体   English

如何将坐标系从世界转换为相机(3D 到 2D 转换)?

[英]How can I convert a coordinate system from world to camera (3D to 2D conversion)?

I am working on a project involving 3D world to 2D film conversion, and I am having trouble obtaining the correct output.我正在从事一个涉及 3D 世界到 2D 电影转换的项目,我无法获得正确的 output。

I am using the information as located on https://www.scratchapixel.com/lessons/3d-basic-rendering/computing-pixel-coordinates-of-3d-point/mathematics-computing-2d-coordinates-of-3d-points to figure out the code, and this is part of what I have so far:我正在使用位于https://www.scratchapixel.com/lessons/3d-basic-rendering/computing-pixel-coordinates-of-3d-point/mathematics-computing-2d-coordinates-of-3d-上的信息指出找出代码,这是我到目前为止的一部分:

# Camera's Focal Point f is known, and predefined.

# Define camera's position in terms of world coordinates (Fixed)
cx = 281.771;
cy = 6109.4;
cz = 1141.63;

# Each of the world's axes is described in terms of camera coordinates

r11, r21, r31 = 0.0026, -0.9999, 0.0038; #World X-Axis in Cam Coords
r12, r22, r32 = -0.0043, -0.0038, -0.9998; #World Y-Axis in Cam Coords
r13, r23, r33 = -0.9999, 0.0157, 0.0042; #World Z-Axis in Cam Coords


r_matrix = np.array([[r11, r21, r31, 0],
                [r12, r22, r32, 0],
                [r13, r23, r33, 0],
                [cx, cy, cz, 1]]); #Rotation Matrix

# Inverse of this since we are doing world-to-local
global r_mat

r_mat = inv(r_matrix); # This will also be passed to function since it is fixed for each camera
print(r_mat);
r_mat = r_mat.astype(float)

world = np.array([])
world = np.array([U, V, W, 1.0]); #World Coordinate Matrix (x,y,z,1)
world = world.astype(float)
local = world.dot(r_mat)
print(local)
localx = local[0]
localy = local[1]
localz = local[2]

camx = -1 * f * (localx/localz)
camy = -1 * f * (localy/localz)

This gives me incorrect values for the 2D coordinates.这给了我不正确的二维坐标值。 Could someone please please help me out with this?有人可以帮我解决这个问题吗? It would be a huge help!这将是一个巨大的帮助!

(Extra: Does the orientation of the camera-axes and the world-axes have to be the same? Or does the rotation matrix take care of this?) (额外:相机轴和世界轴的方向是否必须相同?还是旋转矩阵会处理这个问题?)

first of all i would like to have commented instead but my reputation does not allow this at the moment.首先,我想发表评论,但我的声誉目前不允许这样做。

So you got your camera at some coordniantes in the 3D space and a point in 3D space which you want to project into the 2D space of the camera.因此,您的相机位于 3D 空间中的一些坐标处,以及 3D 空间中的一个点,您希望将其投影到相机的 2D 空间中。

You need to describe the point in camera coordinates which corespondes to a translation and a rotation, which can be described in a 4x4 matrix.您需要在相机坐标中描述与平移和旋转相对应的点,可以用 4x4 矩阵来描述。 The extra dimension is needed to make wokring with the matrix easier.需要额外的维度才能更轻松地使用矩阵。

Lets define some variables让我们定义一些变量

pc = point in camera coordinates (3D) pc = 相机坐标中的点 (3D)

pw = point in wolrd coordinates (3D) pw = 世界坐标中的点 (3D)

Cwc = rotation of the camera ( matrix which describes the rotation of the camera) Cwc = 摄像机旋转(描述摄像机旋转的矩阵)

Tw = describes the translation Tw = 描述翻译

Twc = combination of translation and rotation Twc = 平移和旋转的组合

Therefore pc =Twc * pw so you now have described the point in the camera coordinates.因此 pc =Twc * pw 所以您现在已经描述了相机坐标中的点。 Now you need to project the 3D point into the 2D camera plain.现在您需要将 3D 点投影到 2D 相机平面中。

p2d = point in camera coordinates (2D) p2d = 相机坐标中的点 (2D)

Z = projection matrix (involves focal length) Z = 投影矩阵(涉及焦距)

p2d = Z*pc p2d = Z*pc

For example:例如:

p2d = [[f, 0, 0, 0], [0, f, 0, 0], [0, 0, 1, 0]]*[pc1, pc2, pc3, 1]

Thats it for the theory, what is missing is the actual calculation of the Cwc, the matrix which describes the rotation in of the camera plain.理论就是这样,缺少的是 Cwc 的实际计算,该矩阵描述了相机平面的旋转。 Unfortunatley i dont know how to do this.不幸的是,我不知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM