2017-09-16 30 views
6

我寫了一個簡單的腳本,將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軸,而不是負面的。 –

回答

3

我覺得困惑是唯一在這一行:

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的東西纔會在相機的前面。

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

我認爲:

| 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沒錯,我糾正了我的答案。 –

相關問題