Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement pinhole.scale(), pinhole.scale_() and add tests

edgarriba1631cdd61c2e
diff --git a/test/test_pinhole.py b/test/test_pinhole.py
index 3b34a47b7..f7ed217d9 100644
--- a/test/test_pinhole.py
+++ b/test/test_pinhole.py
@@ -117,6 +117,64 @@ class TestPinholeCamera:
assert pinholes.rotation_matrix.shape == (batch_size, 2, 3, 3)
assert pinholes.translation_vector.shape == (batch_size, 2, 3, 1)
+ def test_pinhole_camera_scale(self):
+ batch_size = 2
+ height, width = 4, 6
+ fx, fy, cx, cy = 1, 2, width / 2, height / 2
+ tx, ty, tz = 1, 2, 3
+ scale_val = 2.0
+
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz)
+ height = torch.ones(batch_size) * height
+ width = torch.ones(batch_size) * width
+ scale_factor = torch.ones(batch_size) * scale_val
+
+ pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width)
+ pinhole_scale = pinhole.scale(scale_factor)
+
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 0, 0],
+ pinhole.intrinsics[..., 0, 0] * scale_val) # fx
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 1, 1],
+ pinhole.intrinsics[..., 1, 1] * scale_val) # fy
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 0, 2],
+ pinhole.intrinsics[..., 0, 2] * scale_val) # cx
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 1, 2],
+ pinhole.intrinsics[..., 1, 2] * scale_val) # cy
+ assert utils.check_equal_torch(pinhole_scale.height,
+ pinhole.height * scale_val) # image_height
+ assert utils.check_equal_torch(pinhole_scale.width,
+ pinhole.width * scale_val) # image_width
+
+ def test_pinhole_camera_scale_inplace(self):
+ batch_size = 2
+ height, width = 4, 6
+ fx, fy, cx, cy = 1, 2, width / 2, height / 2
+ tx, ty, tz = 1, 2, 3
+ scale_val = 2.0
+
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz)
+ height = torch.ones(batch_size) * height
+ width = torch.ones(batch_size) * width
+ scale_factor = torch.ones(batch_size) * scale_val
+
+ pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width)
+ pinhole_scale = pinhole.clone()
+ pinhole_scale.scale_(scale_factor)
+
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 0, 0],
+ pinhole.intrinsics[..., 0, 0] * scale_val) # fx
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 1, 1],
+ pinhole.intrinsics[..., 1, 1] * scale_val) # fy
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 0, 2],
+ pinhole.intrinsics[..., 0, 2] * scale_val) # cx
+ assert utils.check_equal_torch(pinhole_scale.intrinsics[..., 1, 2],
+ pinhole.intrinsics[..., 1, 2] * scale_val) # cy
+ assert utils.check_equal_torch(pinhole_scale.height,
+ pinhole.height * scale_val) # image_height
+ assert utils.check_equal_torch(pinhole_scale.width,
+ pinhole.width * scale_val) # image_width
@pytest.mark.parametrize("device_type", TEST_DEVICES)
@pytest.mark.parametrize("batch_size", [1, 2, 5, 6])
diff --git a/torchgeometry/core/pinhole.py b/torchgeometry/core/pinhole.py
index 8ce6d5a00..286d0fe40 100644
--- a/torchgeometry/core/pinhole.py
+++ b/torchgeometry/core/pinhole.py
@@ -210,8 +210,8 @@ class PinholeCamera:
def clone(self) -> 'PinholeCamera':
r"""Returns a deep copy of the current object instance."""
- height: torch.Tensor = self.height
- width: torch.Tensor = self.width
+ height: torch.Tensor = self.height.clone()
+ width: torch.Tensor = self.width.clone()
intrinsics: torch.Tensor = self.intrinsics.clone()
extrinsics: torch.Tensor = self.extrinsics.clone()
return PinholeCamera(intrinsics, extrinsics, height, width)
@@ -224,6 +224,49 @@ class PinholeCamera:
"""
return self.intrinsics.inverse()
+ def scale(self, scale_factor) -> 'PinholeCamera':
+ r"""Scales the pinhole model.
+
+ Args:
+ scale_factor (torch.Tensor): a tensor with the scale factor. It has
+ to be broadcastable with class members. The expected shape is
+ :math:`(B)` or :math:`(1)`.
+
+ Returns:
+ PinholeCamera: the camera model with scaled parameters.
+ """
+ # scale the intrinsic parameters
+ intrinsics: torch.Tensor = self.intrinsics.clone()
+ intrinsics[..., 0, 0] *= scale_factor
+ intrinsics[..., 1, 1] *= scale_factor
+ intrinsics[..., 0, 2] *= scale_factor
+ intrinsics[..., 1, 2] *= scale_factor
+ # scale the image height/width
+ height: torch.Tensor = scale_factor * self.height.clone()
+ width: torch.Tensor = scale_factor * self.width.clone()
+ return PinholeCamera(intrinsics, self.extrinsics, height, width)
+
+ def scale_(self, scale_factor) -> 'PinholeCamera':
+ r"""Scales the pinhole model in-place.
+
+ Args:
+ scale_factor (torch.Tensor): a tensor with the scale factor. It has
+ to be broadcastable with class members. The expected shape is
+ :math:`(B)` or :math:`(1)`.
+
+ Returns:
+ PinholeCamera: the camera model with scaled parameters.
+ """
+ # scale the intrinsic parameters
+ self.intrinsics[..., 0, 0] *= scale_factor
+ self.intrinsics[..., 1, 1] *= scale_factor
+ self.intrinsics[..., 0, 2] *= scale_factor
+ self.intrinsics[..., 1, 2] *= scale_factor
+ # scale the image height/width
+ self.height *= scale_factor
+ self.width *= scale_factor
+ return self
+
# NOTE: just for test. Decide if we keep it.
@classmethod
def from_parameters(self, fx, fy, cx, cy, height, width, tx, ty, tz):