So the setting is given a coordinate system shown below where the z
-axis is pointing out of the screen (towards you), the camera focal length is 270
pixels and image resolution is 640x480
, then we have an object somewhere in 3D space, and two drones d1
and d2
taking two observations at two different viewpoints, where is d1
is at (6, 3, 2)
and the corresponding image coordinate of the object is (320, 280)
, and that of d2
is (9.5, 4.5, 3)
and (160, 408)
, also the heading of d1
is -20
degrees from the y
-axis and that of d2
is +30
degrees from y
-axis, the goal is to determine (x, y, z)
where the object is at, the drones are hovering over the xy
plane
Click Here for Image Illustration
Given the information, by letting d1
be the reference frame, we can have the camera intrinsics K = [[270, 0, 320], [0, 270, 240], [0, 0, 1]]
, the transformation is rotate +50
degrees with z
-axis as the rotation axis, and translation t = [3.5, 1.5, 1]
, therefore my code
import numpy as np
import cv2
def pixel2cam(pt, K):
u = (pt[0] - K[0][2]) / K[0][0]
v = (pt[1] - K[1][2]) / K[1][1]
return np.array([u, v], dtype=np.float32)
def triangulate(points_1, points_2, K, R, t):
cam_pts_1 = pixel2cam(points_1, K).reshape(2, 1)
cam_pts_2 = pixel2cam(points_2, K).reshape(2, 1)
T1 = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0]], dtype=np.float32)
T2 = np.hstack((R, t))
X = cv2.triangulatePoints(T1, T2, cam_pts_1, cam_pts_2)
X /= X[3]
return X
K = np.array([[270, 0, 320], [0, 270, 240], [0, 0, 1]], dtype=np.float32)
# rotate +50 degrees along z axis
R = np.array([[0.643, -0.766, 0], [0.766, 0.643, 0], [0, 0, 1]], dtype=np.float32)
t = np.array([[3.5], [1.5], [1]], dtype=np.float)
pt_1 = (320, 280)
pt_2 = (160, 408)
X = triangulate(pt_1, pt_2, K, R, t)
and that gives you a homogeneous coordinate X = [[-2.4155867], [ -5.1455526], [-12.032189], [1.]])
where z
is negative, so my question is
- Did I correctly formulate
R
andt
here? - If yes, then could the error be due to a different camera coordinate system used here than that in
OpenCV
?
Any help is appreaciated!