Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

import pinhole_matrix, inv_pinhole_matrix, scale_pinhole, homography_i_H_ref

edgarriba764b9f5783ab
diff --git a/test/test_functional.py b/test/test_functional.py
index f789650af..733d44f31 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -186,5 +186,54 @@ class Tester(unittest.TestCase):
raise_exception=True)
self.assertTrue(res)
+ def test_homography_i_H_ref(self):
+ # generate input data
+ image_height, image_width = 32., 32.
+ cx, cy = image_width / 2, image_height / 2
+ fx, fy = 1., 1.
+ rx, ry, rz = 0., 0., 0.
+ tx, ty, tz = 0., 0., 0.
+ offset_x = 10. # we will apply a 10units offset to `i` camera
+
+ pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, \
+ image_height, image_width, rx, ry, rx, tx, ty, tz)
+
+ pinhole_i = utils.create_pinhole(fx, fy, cx, cy, \
+ image_height, image_width, rx, ry, rx, tx + offset_x, ty, tz)
+
+ # compute homography from ref to i
+ i_H_ref = dgm.homography_i_H_ref(pinhole_i, pinhole_ref)
+ i_H_ref_inv = dgm.inverse(i_H_ref)
+
+ # compute homography from i to ref
+ ref_H_i = dgm.homography_i_H_ref(pinhole_ref, pinhole_i)
+
+ res = utils.check_equal_torch(i_H_ref_inv, ref_H_i)
+ self.assertTrue(res)
+
+ @unittest.skip("Jacobian mismatch for output 0 with respect to input 0")
+ def test_homography_i_H_ref_gradcheck(self):
+ # generate input data
+ image_height, image_width = 32., 32.
+ cx, cy = image_width / 2, image_height / 2
+ fx, fy = 1., 1.
+ rx, ry, rz = 0., 0., 0.
+ tx, ty, tz = 0., 0., 0.
+ offset_x = 10. # we will apply a 10units offset to `i` camera
+
+ pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, \
+ image_height, image_width, rx, ry, rx, tx, ty, tz)
+ pinhole_ref = utils.tensor_to_gradcheck_var(pinhole_ref) # to var
+
+ pinhole_i = utils.create_pinhole(fx, fy, cx, cy, \
+ image_height, image_width, rx, ry, rx, tx + offset_x, ty, tz)
+ pinhole_i = utils.tensor_to_gradcheck_var(pinhole_ref) # to var
+
+ # evaluate function gradient
+ res = gradcheck(dgm.homography_i_H_ref, (pinhole_i, pinhole_ref,),
+ raise_exception=True)
+ self.assertTrue(res)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/test/utils.py b/test/utils.py
index cb680ff60..f6abbb64a 100644
--- a/test/utils.py
+++ b/test/utils.py
@@ -5,6 +5,13 @@ import numpy as np
# test utilites
+def create_pinhole(fx, fy, cx, cy, height, width, rx, ry, rz, tx, ty, tz):
+ """Creates pinhole model encoded to a torch.Tensor.
+ """
+ return torch.Tensor([
+ [fx, fy, cx, cy, height, width, rx, ry, rz, tx, ty, tz]])
+
+
def create_checkerboard(h, w, nw):
"""Creates a synthetic checkerd board of shape HxW and window size `nw`.
"""
@@ -47,9 +54,8 @@ def compute_patch_error(x, y, h, w):
return torch.abs(x - y)[..., h // 4:-h // 4, w // 4:-w // 4].mean()
-def check_equal_torch(a, b):
- dtype_val = np.finfo(a.numpy().dtype).eps
- return (torch.norm(a - b) <= (a.numel() * dtype_val)).item()
+def check_equal_torch(a, b, eps=1e-4):
+ return (torch.norm(a - b) <= (a.numel() * eps)).item()
def check_equal_numpy(a, b):
diff --git a/torchgeometry/functional.py b/torchgeometry/functional.py
index 22af230bc..bf2e88835 100644
--- a/torchgeometry/functional.py
+++ b/torchgeometry/functional.py
@@ -1,5 +1,7 @@
import torch
+from .transforms import rtvec_to_pose
+
__all__ = [
"pi",
@@ -10,6 +12,10 @@ __all__ = [
"transform_points",
"inverse",
"inverse_pose",
+ "pinhole_matrix",
+ "inv_pinhole_matrix",
+ "scale_pinhole",
+ "homography_i_H_ref",
]
@@ -154,3 +160,60 @@ def inverse_pose(pose):
pose_inv = torch.squeeze(pose_inv, dim=0)
return pose_inv
+
+
+def pinhole_matrix(pinhole):
+ assert len(pinhole.shape) == 2 and pinhole.shape[1] == 12, pinhole.shape
+ # unpack pinhole values
+ fx, fy, cx, cy = torch.chunk(pinhole[..., :4], 4, dim=1) # Nx1
+ # create output container
+ k = torch.eye(4, device=pinhole.device, dtype=pinhole.dtype)
+ k = k.view(1, 4, 4).repeat(pinhole.shape[0], 1, 1) # Nx4x4
+ # fill output with pinhole values
+ k[..., 0, 0] = fx; k[..., 0, 2] = cx
+ k[..., 1, 1] = fy; k[..., 1, 2] = cy
+ return k
+
+
+def inv_pinhole_matrix(pinhole, eps=1e-6):
+ assert len(pinhole.shape) == 2 and pinhole.shape[1] == 12, pinhole.shape
+ # unpack pinhole values
+ fx, fy, cx, cy = torch.chunk(pinhole[..., :4], 4, dim=1) # Nx1
+ # create output container
+ k = torch.eye(4, device=pinhole.device, dtype=pinhole.dtype)
+ k = k.view(1, 4, 4).repeat(pinhole.shape[0], 1, 1) # Nx4x4
+ # fill output with inverse values
+ k[..., 0, 0] = 1. / (fx + eps); k[..., 0, 2] = -1. * cx / (fx + eps)
+ k[..., 1, 1] = 1. / (fy + eps); k[..., 1, 2] = -1. * cy / (fy + eps)
+ return k
+
+
+def scale_pinhole(pinhole, scale):
+ """Scales the pinhole matrix from a pinhole model.
+ """
+ assert len(pinhole) == 2 and pinhole.shape[1] == 12, pinhole.shape
+ assert len(scale) == 2 and scale.shape[1] == 1, scale.shape
+ pinhole_scaled = pinhole.clone()
+ pinhole_scaled[..., :6] = pinhole[..., :6] * scale
+ return pinhole_scaled
+
+
+def get_optical_pose_base(pinhole):
+ assert len(pinhole.shape) == 2 and pinhole.shape[1] == 12, pinhole.shape
+ optical_pose_parent = pinhole[..., 6:]
+ return rtvec_to_pose(optical_pose_parent)
+
+
+def homography_i_H_ref(pinhole_i, pinhole_ref):
+ """
+ Homography from pinhole_ref to pinhole_i
+ :returns matrix (4x4) that converts depth points from pinhole_ref to
+ pinhole_i
+ """
+ assert len(pinhole_i.shape) == 2 and pinhole_i.shape[1] == 12, pinhole.shape
+ assert pinhole_i.shape == pinhole_ref.shape, pinhole_ref.shape
+ i_pose_base = get_optical_pose_base(pinhole_i)
+ ref_pose_base = get_optical_pose_base(pinhole_ref)
+ i_pose_ref = torch.matmul(i_pose_base, inverse_pose(ref_pose_base))
+ return torch.matmul(pinhole_matrix(pinhole_i), \
+ torch.matmul(i_pose_ref, inv_pinhole_matrix(pinhole_ref)))