使用多个摄像头进行位置跟踪,将2D位置转换为单个3D点

时间:2017-04-09 04:42:15

标签: python opencv homography

首先,我对OpenCV的知识很少。

我的头顶上有一个红外灯塔。使用一些网络摄像头,我能够获得每个摄像头的信标的(x,y)位置。如果我把这些网络摄像头放在我的房间里,我希望能够在我房间的真实3D空间中跟踪我的(x,y,z)位置。

Heres an illustration for what I am trying to do

我正在尝试复制此视频:https://youtu.be/LpB0AteUH7A。该视频是开源的,所以我查看了他的代码并尝试修改它以便为我工作。但是,我发现这些函数返回的值没有多大意义(对于所有函数都是0,或者是负值,有时它只是错误,我不明白为什么)。

根据我的理解,我需要先校准软件才能获得我的位置。校准将涉及将信标正好移动1米并告诉软件将这些点保存1米,然后再做几个点和位置。我已经有了相机的失真/系数矩阵。

我使用的是python和opencv,到目前为止,我尝试使用solvepnp,triangulatePoints,perspectiveTransform但我没有矩阵数学理解知道这些函数在做什么。

def calculate3DPosition():

    world3DPoints1 = np.array([[[0],[0],[70]],[[100],[0],[70]],[[200],[0],[70]]], dtype=np.float32)
    world3DPoints2 = np.array([[[0],[0],[70]],[[100],[0],[70]],[[200],[0],[70]]], dtype=np.float32)

    Point2D1 = np.array([[[168], [335]], [[315], [240]], [[430], [166]]], dtype=np.float32)
    Point2D2 = np.array([[[400], [288]], [[300], [187]], [[225], [125]]], dtype=np.float32)

    projection_matrix1, position1 = calculateProjectionMatrix(Point2D1, world3DPoints1)
    projection_matrix2, position2 = calculateProjectionMatrix(Point2D2, world3DPoints2)

    Point2D1 = np.array([[[168],[315],[430]],[[400],[300],[225]]], dtype=np.float32)
    Point2D2 = np.array([[[400],[300],[225]],[[288],[187],[125]]], dtype=np.float32)

    triangulationOutput = cv2.triangulatePoints(np.eye(4)[:3], projection_matrix2, Point2D1, Point2D2)

    mypoint1 = np.array([triangulationOutput[0], triangulationOutput[1], triangulationOutput[2]])
    mypoint1 = mypoint1.reshape(-1, 3)
    mypoint1 = np.array([mypoint1])
    P_24x4 = np.resize(projection_matrix1, (4,4))
    P_24x4[3,0] = 0
    P_24x4[3,1] = 0
    P_24x4[3,2] = 0
    P_24x4[3,3] = 1

    projected = cv2.perspectiveTransform(mypoint1, P_24x4)
    output = triangulationOutput[:-1]/triangulationOutput[-1]
    return output

def calculateProjectionMatrix(listPoint2D, listPoint3D):
    camera_matrix = np.float64([[247.9139798, 0., 155.30251177], [0., 248.19822494, 100.74688813], [0.,  0., 1.]])
    distortion_coefficient = np.float64([-0.45977769,  0.29782977, -0.00162724,  0.00046035, -0.16351777])
    projection_matrix = np.array([], dtype=np.float32).reshape(0,3,4)

    ret, rvec, tvec = cv2.solvePnP(listPoint3D, listPoint2D, camera_matrix, distortion_coefficient)
    rotM_cam = cv2.Rodrigues(rvec)[0]

    cameraPosition = -np.matrix(rotM_cam).T * np.matrix(tvec)

    camMatrix = np.append(cv2.Rodrigues(rvec)[0], tvec, 1)
    projectionMatrix = np.dot(camera_matrix, camMatrix)

    return projectionMatrix, cameraPosition

print(calculate3DPosition())

因为我使用他的代码,我不知道它的作用,但我知道它对他有用。如果你能指出我正确的方向,请帮助。

0 个答案:

没有答案
相关问题