Skip to content

OpenCV camera to PyTorch3D PerspectiveCameras #522

@pengsongyou

Description

@pengsongyou

Dear PyTorch3D team,

First of all, thanks so much for releasing this amazing library!

I have some camera intrinsic and extrinsic parameters from OpenCV, and I try to convert them to PyTorch3D PerspectiveCameras. I have been carefully following this amazing page. However, the calculated pixels in the screen coordinate system in PyTorch3D are always not correct. I provide my code snippet below:

# Given a projection matrix, obtain K, R, t
K, R, t = cv2.decomposeProjectionMatrix(P)[:3]
K = K / K[2, 2]
t = t[:3] / t[3]

# NOTE: I have verified p_camera = K @ (R @ p_world - R @ t) 
# is the p_world in camera coordinate system
# p_pix = p_camera[:2] / p_camera[2] are the pixels in screen coordinate system between [0, W-1] and [0, H-1]

pose = np.eye(4, dtype=np.float32)
pose[:3, :3] = R
pose[:3, 3] = -R @ t

T1 = torch.tensor([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]],
           dtype=torch.float32) # assume OpenCV is X-right, Y-down, Z-in
T2 = torch.tensor([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
           dtype=torch.float32) # assume OpenCV is X-right, Y-down, Z-out

# transform the pose from OpenCV to PyTorch3D (X-left, Y-up, Z-out)
T = T1 # or T2
pose = (T @ torch.tensor(pose, dtype=torch.float32) @ T)
R = pose[:3, :3].unsqueeze(0)
t = pose[:3, 3].unsqueeze(0)

# build focal length and principle points from K
focal = torch.tensor((torch.tensor(K[0, 0]), torch.tensor(K[1, 1]))).unsqueeze(0)
principle = torch.tensor((torch.tensor(K[0, 2]), torch.tensor(K[1, 2]))).unsqueeze(0)

img_size = (rgb.shape[1], rgb.shape[0]) # (Width, Height)

camera = PerspectiveCameras(R=R, T=t, focal_length=focal, principal_point=principle, image_size=(img_size,))
p_pix_p3d = camera.transform_points_screen(p_world.float(), (img_size,))
p_pix_p3d = p_pix_p3d[:2] 

In my case, p_pix_p3d is always different from GT pixel p_pix, no matter if I use T1 or T2 as the transformation matrix. I am wondering if someone can kindly guide me on this? Thanks so much in advance for the help!

Best,
Songyou

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions