为什么3D点出现在相机后面?

问题描述:

我写了一个简单的脚本,将3D点投影到基于相机内部函数和外部函数的图像上。但是当我在原点指向Z轴的相机和3D指向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轴我会得到单位矩阵。

+0

你能解释一下我怎么知道你的相机指向负z轴?也许一个相机在位置(0,0,0)与身份旋转矩阵正在看加Z轴,而不是负面的。 –

我觉得困惑是唯一在这一行:

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

,因为这些公式假设正Z轴进入画面,所以实际上是一个积极的Z值点将是背后的摄像头:

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

我似乎记得这个选择是任意的,使3D表现与我们的2D预想打得好:如果你认为你的相机正在-Z方向,则负X将是向左时,积极的Y点。在这种情况下,只有负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| 

我认为:

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

不使坐标负Z.你应该让空间中的点根据角度或翻转,例如旋转矩阵将解决您的问题。 下面是检查方便的工具,如果计算的矩阵是正确的:

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

还有一个备注:您计算保留时间()(转),R为单位矩阵。这对模型没有任何影响。转置矩阵也是如此,转换后仍然等于0。

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

问候

+1

单位矩阵的转置是单位矩阵。 – MondKin

+0

@MondKin没错,我纠正了我的答案。 –