Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed recovery of metric depth for orthographic cameras #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pyrender/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ def get_projection_matrix(self, width=None, height=None):
"""
pass

@abc.abstractmethod
def inverse_transform_depth_ndc(self, depth_ndc):
"""
Unproject the depth in Normalized Device Coordinates
to metric values in eye (camera) reference frame.
:param
depth_ndc: np.array np.float32
depth in [-1, 1] in normalized device coordinates
:return:
depth: np.array np.float32
depth in metric units in eye (camera) reference frame.
"""
pass


class PerspectiveCamera(Camera):

Expand Down Expand Up @@ -205,6 +219,19 @@ def get_projection_matrix(self, width=None, height=None):

return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
(self.zfar + self.znear - depth_img[noninf] *
(self.zfar - self.znear)))
depth_img[inf_inds] = 0.0
return depth_img


class OrthographicCamera(Camera):
"""A perspective camera for perspective projection.
Expand Down Expand Up @@ -309,6 +336,17 @@ def get_projection_matrix(self, width=None, height=None):
P[3][3] = 1.0
return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = (depth_img[noninf] * (self.zfar - self.znear) + self.zfar + self.znear) / 2.0
depth_img[inf_inds] = 0.0
return depth_img


class IntrinsicsCamera(Camera):
"""A perspective camera with custom intrinsics.
Expand Down Expand Up @@ -432,6 +470,19 @@ def get_projection_matrix(self, width, height):

return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
(self.zfar + self.znear - depth_img[noninf] *
(self.zfar - self.znear)))
depth_img[inf_inds] = 0.0
return depth_img


__all__ = ['Camera', 'PerspectiveCamera', 'OrthographicCamera',
'IntrinsicsCamera']
12 changes: 1 addition & 11 deletions pyrender/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,18 +1117,8 @@ def _read_main_framebuffer(self, scene, flags):
depth_im = np.frombuffer(depth_buf, dtype=np.float32)
depth_im = depth_im.reshape((height, width))
depth_im = np.flip(depth_im, axis=0)
inf_inds = (depth_im == 1.0)
depth_im = 2.0 * depth_im - 1.0
z_near = scene.main_camera_node.camera.znear
z_far = scene.main_camera_node.camera.zfar
noninf = np.logical_not(inf_inds)
if z_far is None:
depth_im[noninf] = 2 * z_near / (1.0 - depth_im[noninf])
else:
depth_im[noninf] = ((2.0 * z_near * z_far) /
(z_far + z_near - depth_im[noninf] *
(z_far - z_near)))
depth_im[inf_inds] = 0.0
depth_im = scene.main_camera_node.camera.inverse_transform_depth_ndc(depth_im)

# Resize for macos if needed
if sys.platform == 'darwin':
Expand Down