Skip to content

Commit 677b0bd

Browse files
SiddhantRanadefacebook-github-bot
authored andcommitted
SfMPerspectiveCameras projection matrix bug fix (#148)
Summary: Fixed a bug in creating the projection matrix for the SfMPerspectiveCameras class. Also fixed the corresponding test in test_cameras.py. The p0x, p0y are 2D coordinates for the principal point in the NDCS, and therefore should be added AFTER the perspective z divide. I.e. we expect <a href="https://www.codecogs.com/eqnedit.php?latex=\begin{bmatrix}&space;x\&space;y\&space;z&space;\end{bmatrix}_{\text{NDCS}}&space;=&space;\begin{bmatrix}&space;f_xX/Z&space;&plus;&space;p_x\&space;f_yY/Z&space;&plus;&space;p_y\&space;1&space;/&space;Z\&space;\end{bmatrix}&space;=&space;\text{normalize}\left(&space;\begin{bmatrix}&space;f_xX&space;&plus;&space;p_xZ\&space;f_yY&space;&plus;&space;p_yZ\&space;1\&space;Z&space;\end{bmatrix}&space;\right)" target="_blank"><img src="https://latex.codecogs.com/gif.latex?\begin{bmatrix}&space;x\&space;y\&space;z&space;\end{bmatrix}_{\text{NDCS}}&space;=&space;\begin{bmatrix}&space;f_xX/Z&space;&plus;&space;p_x\&space;f_yY/Z&space;&plus;&space;p_y\&space;1&space;/&space;Z\&space;\end{bmatrix}&space;=&space;\text{normalize}\left(&space;\begin{bmatrix}&space;f_xX&space;&plus;&space;p_xZ\&space;f_yY&space;&plus;&space;p_yZ\&space;1\&space;Z&space;\end{bmatrix}&space;\right)" title="\begin{bmatrix} x\ y\ z \end{bmatrix}_{\text{NDCS}} = \begin{bmatrix} f_xX/Z + p_x\ f_yY/Z + p_y\ 1 / Z\ \end{bmatrix} = \text{normalize}\left( \begin{bmatrix} f_xX + p_xZ\ f_yY + p_yZ\ 1\ Z \end{bmatrix} \right)" /></a> The current behavior is <a href="https://www.codecogs.com/eqnedit.php?latex=\begin{bmatrix}&space;x\&space;y\&space;z&space;\end{bmatrix}_{\text{NDCS}}&space;=&space;\begin{bmatrix}&space;f_xX/Z&space;&plus;&space;p_x/Z\&space;f_yY/Z&space;&plus;&space;p_y/Z\&space;1&space;/&space;Z\&space;\end{bmatrix}&space;=&space;\text{normalize}\left(&space;\begin{bmatrix}&space;f_xX&space;&plus;&space;p_x\&space;f_yY&space;&plus;&space;p_y\&space;1\&space;Z&space;\end{bmatrix}&space;\right)" target="_blank"><img src="https://latex.codecogs.com/gif.latex?\begin{bmatrix}&space;x\&space;y\&space;z&space;\end{bmatrix}_{\text{NDCS}}&space;=&space;\begin{bmatrix}&space;f_xX/Z&space;&plus;&space;p_x/Z\&space;f_yY/Z&space;&plus;&space;p_y/Z\&space;1&space;/&space;Z\&space;\end{bmatrix}&space;=&space;\text{normalize}\left(&space;\begin{bmatrix}&space;f_xX&space;&plus;&space;p_x\&space;f_yY&space;&plus;&space;p_y\&space;1\&space;Z&space;\end{bmatrix}&space;\right)" title="\begin{bmatrix} x\ y\ z \end{bmatrix}_{\text{NDCS}} = \begin{bmatrix} f_xX/Z + p_x/Z\ f_yY/Z + p_y/Z\ 1 / Z\ \end{bmatrix} = \text{normalize}\left( \begin{bmatrix} f_xX + p_x\ f_yY + p_y\ 1\ Z \end{bmatrix} \right)" /></a> which is incorrect. Pull Request resolved: #148 Reviewed By: gkioxari Differential Revision: D21039003 Pulled By: davnov134 fbshipit-source-id: 3e19ac22adbcc39b731ae14052a72fd4ddda2af5
1 parent b2b0c5a commit 677b0bd

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

pytorch3d/renderer/cameras.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ def get_projection_transform(self, **kwargs) -> Transform3d:
505505
py = principal_point[:,1]
506506
507507
P = [
508-
[fx, 0, 0, px],
509-
[0, fy, 0, py],
508+
[fx, 0, px, 0],
509+
[0, fy, py, 0],
510510
[0, 0, 0, 1],
511511
[0, 0, 1, 0],
512512
]
@@ -800,8 +800,8 @@ def _get_sfm_calibration_matrix(
800800
]
801801
else:
802802
K = [
803-
[fx, 0, 0, px],
804-
[0, fy, 0, py],
803+
[fx, 0, px, 0],
804+
[0, fy, py, 0],
805805
[0, 0, 0, 1],
806806
[0, 0, 1, 0],
807807
]
@@ -827,12 +827,14 @@ def _get_sfm_calibration_matrix(
827827
K = fx.new_zeros(N, 4, 4)
828828
K[:, 0, 0] = fx
829829
K[:, 1, 1] = fy
830-
K[:, 0, 3] = px
831-
K[:, 1, 3] = py
832830
if orthographic:
831+
K[:, 0, 3] = px
832+
K[:, 1, 3] = py
833833
K[:, 2, 2] = 1.0
834834
K[:, 3, 3] = 1.0
835835
else:
836+
K[:, 0, 2] = px
837+
K[:, 1, 2] = py
836838
K[:, 3, 2] = 1.0
837839
K[:, 2, 3] = 1.0
838840

tests/test_cameras.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def sfm_perspective_project_naive(points, fx=1.0, fy=1.0, p0x=0.0, p0y=0.0):
8181
(N, V, 3) tensor of projected points.
8282
"""
8383
z = points[:, :, 2]
84-
x = (points[:, :, 0] * fx + p0x) / z
85-
y = (points[:, :, 1] * fy + p0y) / z
84+
x = (points[:, :, 0] * fx) / z + p0x
85+
y = (points[:, :, 1] * fy) / z + p0y
8686
points = torch.stack((x, y, 1.0 / z), dim=2)
8787
return points
8888

0 commit comments

Comments
 (0)