Skip to content

Commit 8fa438c

Browse files
Ruilong Lifacebook-github-bot
Ruilong Li
authored andcommitted
Fix camera conversion between opencv and pytorch3d
Summary: For non square image, the NDC space in pytorch3d is not square [-1, 1]. Instead, it is [-1, 1] for the smallest side, and [-u, u] for the largest side, where u > 1. This behavior is followed by the pytorch3d renderer. See the function `get_ndc_to_screen_transform` for a example. Without this fix, the rendering result is not correct using the converted pytorch3d-camera from a opencv-camera on non square images. This fix also helps the `transform_points_screen` function delivers consistent results with opencv projection for the converted pytorch3d-camera. Reviewed By: classner Differential Revision: D31366775 fbshipit-source-id: 8858ae7b5cf5c0a4af5a2af40a1358b2fe4cf74b
1 parent 815a93c commit 8fa438c

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

pytorch3d/renderer/camera_conversions.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ def _cameras_from_opencv_projection(
2828
# Retype the image_size correctly and flip to width, height.
2929
image_size_wh = image_size.to(R).flip(dims=(1,))
3030

31+
# Screen to NDC conversion:
32+
# For non square images, we scale the points such that smallest side
33+
# has range [-1, 1] and the largest side has range [-u, u], with u > 1.
34+
# This convention is consistent with the PyTorch3D renderer, as well as
35+
# the transformation function `get_ndc_to_screen_transform`.
36+
scale = (image_size_wh.to(R).min(dim=1, keepdim=True)[0] - 1) / 2.0
37+
scale = scale.expand(-1, 2)
38+
c0 = (image_size_wh - 1) / 2.0
39+
3140
# Get the PyTorch3D focal length and principal point.
32-
focal_pytorch3d = focal_length / (0.5 * image_size_wh)
33-
p0_pytorch3d = -(principal_point / (0.5 * image_size_wh) - 1)
41+
focal_pytorch3d = focal_length / scale
42+
p0_pytorch3d = -(principal_point - c0) / scale
3443

3544
# For R, T we flip x, y axes (opencv screen space has an opposite
3645
# orientation of screen axes).
@@ -45,6 +54,7 @@ def _cameras_from_opencv_projection(
4554
T=T_pytorch3d,
4655
focal_length=focal_pytorch3d,
4756
principal_point=p0_pytorch3d,
57+
image_size=image_size,
4858
)
4959

5060

@@ -64,8 +74,13 @@ def _opencv_from_cameras_projection(
6474
# Retype the image_size correctly and flip to width, height.
6575
image_size_wh = image_size.to(R).flip(dims=(1,))
6676

67-
principal_point = (-p0_pytorch3d + 1.0) * (0.5 * image_size_wh) # pyre-ignore
68-
focal_length = focal_pytorch3d * (0.5 * image_size_wh)
77+
# NDC to screen conversion.
78+
scale = (image_size_wh.to(R).min(dim=1, keepdim=True)[0] - 1) / 2.0
79+
scale = scale.expand(-1, 2)
80+
c0 = (image_size_wh - 1) / 2.0
81+
82+
principal_point = -p0_pytorch3d * scale + c0
83+
focal_length = focal_pytorch3d * scale
6984

7085
camera_matrix = torch.zeros_like(R)
7186
camera_matrix[:, :2, 2] = principal_point

tests/test_camera_conversions.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
DATA_DIR = get_tests_dir() / "data"
2525

2626

27-
def _coords_opencv_screen_to_pytorch3d_ndc(xy_opencv, image_size):
28-
"""
29-
Converts the OpenCV screen coordinates `xy_opencv` to PyTorch3D NDC coordinates.
30-
"""
31-
xy_pytorch3d = -(2.0 * xy_opencv / image_size.flip(dims=(1,))[:, None] - 1.0)
32-
return xy_pytorch3d
33-
34-
3527
def cv2_project_points(pts, rvec, tvec, camera_matrix):
3628
"""
3729
Reproduces the `cv2.projectPoints` function from OpenCV using PyTorch.
@@ -145,18 +137,13 @@ def test_opencv_conversion(self):
145137
R, tvec, camera_matrix, image_size
146138
)
147139

148-
# project the 3D points with converted cameras
149-
pts_proj_pytorch3d = cameras_opencv_to_pytorch3d.transform_points(pts)[..., :2]
150-
151-
# convert the opencv-projected points to pytorch3d screen coords
152-
pts_proj_opencv_in_pytorch3d_screen = _coords_opencv_screen_to_pytorch3d_ndc(
153-
pts_proj_opencv, image_size
154-
)
140+
# project the 3D points with converted cameras to screen space.
141+
pts_proj_pytorch3d_screen = cameras_opencv_to_pytorch3d.transform_points_screen(
142+
pts
143+
)[..., :2]
155144

156145
# compare to the cached projected points
157-
self.assertClose(
158-
pts_proj_opencv_in_pytorch3d_screen, pts_proj_pytorch3d, atol=1e-5
159-
)
146+
self.assertClose(pts_proj_opencv, pts_proj_pytorch3d_screen, atol=1e-5)
160147

161148
# Check the inverse.
162149
R_i, tvec_i, camera_matrix_i = opencv_from_cameras_projection(

0 commit comments

Comments
 (0)