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

时间:2017-09-16 06:46:24

标签: computer-vision camera-calibration projection-matrix

我写了一个简单的脚本,将3D点投影到基于相机内在函数和外显子的图像上。但是当我在原点指向z轴并且沿z轴进一步向下指向3D时,它看起来是在摄像机后面而不是在它前面。这是我的剧本,我已经多次检查过了。

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轴的相机,我会得到身份基质

2 个答案:

答案 0 :(得分:3)

我认为混淆只在这一行:

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的东西才会出现在相机前面。

答案 1 :(得分:-1)

# 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|

这不会使坐标空间指向负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

此致