【问题标题】:Why does a 3D point appear to be behind the camera?为什么 3D 点似乎在相机后面?
【发布时间】:2018-02-25 07:40:01
【问题描述】:

我编写了一个简单的脚本,根据相机的内在函数和外在函数将 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 轴的相机,我会得到单位矩阵。

【问题讨论】:

  • 你能解释一下你是怎么知道你的相机指向负z轴的吗?可能位置 (0,0,0) 的摄像机具有单位旋转矩阵正在查看正 z 轴而不是负 z 轴。

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


【解决方案1】:

我认为混乱只在这一行:

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 的东西才会出现在镜头前。

【讨论】:

    【解决方案2】:
    # 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

    问候

    【讨论】:

    • 单位矩阵的转置就是单位矩阵。
    猜你喜欢
    • 2014-02-02
    • 2014-01-08
    • 2014-08-14
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多