为什么 3D 点似乎在相机后面?

Why does a 3D point appear to be behind the camera?

我写了一个简单的脚本来将 3D 点投影到基于相机内部和外部的图像中。但是当我在原点有一个相机指向 z 轴下方并且 3D 指向 z 轴下方更远时,它似乎在相机后面而不是在它前面。这是我的脚本,我已经检查了很多次。

import numpy as np

def project(point, P):
    Hp = P.dot(point)

    if Hp[-1] < 0:
        print 'Point is behind camera'

    Hp = Hp / Hp[-1]
    print Hp[0][0], 'x', Hp[1][0]
    return Hp[0][0], Hp[1][0]

if __name__ == '__main__':

    # Rc and C are the camera orientation and location in world coordinates
    # Camera posed at origin pointed down the negative z-axis
    Rc = np.eye(3)
    C  = np.array([0, 0, 0])

    # Camera extrinsics
    R = Rc.T
    t = -R.dot(C).reshape(3, 1)

    # The camera projection matrix is then:
    # P = K [ R | t] which projects 3D world points 
    # to 2D homogenous image coordinates.

    # Example intrinsics dont really matter ...

    K = np.array([
        [2000, 0,  2000],
        [0,  2000, 1500],
        [0,  0,  1],
    ])


    # Sample point in front of camera
    # i.e. further down the negative x-axis
    # should project into center of image
    point = np.array([[0, 0, -10, 1]]).T

    # Project point into the camera
    P = K.dot(np.hstack((R, t)))

    # But when projecting it appears to be behind the camera?
    project(point,P)

我唯一能想到的是,识别旋转矩阵不对应于相机指向负z轴,向上矢量指向正y轴方向。但是我不明白为什么会这样,例如我从像 gluLookAt 这样的函数构造了 Rc,并在原点给它一个相机,指向负 z 轴,我会得到单位矩阵。

我认为混淆仅在于这一行:

if Hp[-1] < 0:
    print 'Point is behind camera'

因为这些公式假设正 Z 轴进入屏幕,所以实际上具有正 Z 值的点将在相机后面:

if Hp[-1] > 0:
    print 'Point is behind camera'

我似乎记得这个选择是任意的,以使 3D 表示与我们的 2D 预想很好地配合:如果你假设你的相机在 -Z 方向看,那么当 Y 为正时,负 X 将在左边点起来。在这种情况下,只有负 Z 的东西才会出现在镜头前。

# Rc and C are the camera orientation and location in world coordinates
# Camera posed at origin pointed down the negative z-axis
Rc = np.eye(3)
C  = np.array([0, 0, 0])
# Camera extrinsics
R = Rc.T
t = -R.dot(C).reshape(3, 1)

首先你进行了单位矩阵的转置。 你有相机旋转矩阵:

| 1, 0, 0| 
| 0, 1, 0|
| 0, 0, 1|

不会使坐标space指向负Z。您应该根据角度或翻转制作旋转矩阵,例如:

|1, 0,  0| 
|0, 1,  0|
|0, 0, -1|

我认为这会解决您的问题。 这是检查计算矩阵是否正确的便捷工具:

http://www.andre-gaschler.com/rotationconverter/

再说一句:你计算的是R.t()(转置),R是单位矩阵。这对模型没有影响。转置矩阵同理,变换后仍为0

https://www.quora.com/What-is-an-inverse-identity-matrix

此致