Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): promote dtype in PinholeCamera.scale_ for int64 height/width (#4371)

li-lizheff204fb9c6c0
diff --git a/changelog.d/4371.fixed.md b/changelog.d/4371.fixed.md
new file mode 100644
index 000000000..8e148877c
--- /dev/null
+++ b/changelog.d/4371.fixed.md
@@ -0,0 +1 @@
+`PinholeCamera.scale_` now promotes an integer `height` / `width` with a floating-point scale factor to floating point, matching `scale`, instead of raising `RuntimeError`.
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index 1ff311f2c..79742776b 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -56,11 +56,9 @@ class PinholeCamera:
.. warning::
:meth:`scale` and :meth:`scale_` rescale the principal point as ``cx' = s * cx`` β€” the half-pixel rule β€”
which disagrees with the integer pixel centres above; it is tracked as a coordinated repair in
- `#4263 <https://github.com/kornia/kornia/issues/4263>`_. The in-place :meth:`scale_` failure on an
- integer ``height`` / ``width`` with a floating-point scale factor is
- `#4265 <https://github.com/kornia/kornia/issues/4265>`_, the direct projection limitation for
- :math:`(B, N, 4, 4)` camera storage `#4266 <https://github.com/kornia/kornia/issues/4266>`_. The behaviour
- described here is documented as it is; the issues above track the repairs.
+ `#4263 <https://github.com/kornia/kornia/issues/4263>`_. The direct projection limitation for
+ :math:`(B, N, 4, 4)` camera storage is `#4266 <https://github.com/kornia/kornia/issues/4266>`_. The
+ behaviour described here is documented as it is; the issues above track the repairs.
Args:
intrinsics: torch.Tensor with shape :math:`(B, 4, 4)`
@@ -316,7 +314,7 @@ class PinholeCamera:
- the new camera owns its parameter storage, so writing ``tx`` / ``ty`` / ``tz`` on the returned
camera leaves the source where it was.
- with a floating-point ``scale_factor``, an integer ``height`` / ``width`` is promoted to floating
- point, unlike :meth:`scale_`. An integer factor preserves the integer image-size dtype.
+ point, as :meth:`scale_` does. An integer factor preserves the integer image-size dtype.
See :doc:`camera and world conventions </get-started/camera-conventions>` for image resizing and
the matching intrinsics scaling convention.
@@ -352,20 +350,15 @@ class PinholeCamera:
Convention:
- applies the same rescaling as :meth:`scale` in place and returns ``self``. The camera owns its
parameter storage, so the tensors passed to the constructor are not modified.
- - with a floating-point ``scale_factor``, writing back into an integer ``height`` / ``width`` raises
- :class:`RuntimeError` where :meth:`scale` promotes it to floating point. An integer factor
- succeeds. The focal lengths and principal point have already been scaled when the error is
- raised: the camera is left partially scaled.
- If ``height`` is integer, both image dimensions are unchanged; if only ``width`` is integer,
- ``height`` has already been scaled too.
+ - with a floating-point ``scale_factor``, an integer ``height`` / ``width`` is promoted to floating
+ point, just as :meth:`scale` does. An integer factor preserves the integer image-size dtype.
See :doc:`camera and world conventions </get-started/camera-conventions>` for image resizing and
the matching intrinsics scaling convention.
.. warning::
- The failure on an integer image size with a floating-point scale factor is tracked in
- `#4265 <https://github.com/kornia/kornia/issues/4265>`_, and the principal-point rule shared with
- :meth:`scale` in `#4263 <https://github.com/kornia/kornia/issues/4263>`_.
+ The principal-point rule shared with :meth:`scale` is tracked in
+ `#4263 <https://github.com/kornia/kornia/issues/4263>`_.
Args:
scale_factor: a torch.Tensor with the scale factor. It has
@@ -382,8 +375,8 @@ class PinholeCamera:
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
+ self.height = self.height * scale_factor
+ self.width = self.width * scale_factor
return self
def project(self, point_3d: torch.Tensor) -> torch.Tensor:
diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.py
index 71e5ec1c0..af013e4e8 100644
--- a/tests/geometry/camera/test_pinhole.py
+++ b/tests/geometry/camera/test_pinhole.py
@@ -811,13 +811,11 @@ class TestPinholeCamera(BaseTester):
self.assert_close(height, height_before, atol=0.0, rtol=0.0)
self.assert_close(width, width_before, atol=0.0, rtol=0.0)
- def test_wart_scale_inplace_rejects_integer_image_size_4265(self, device, dtype):
- # Wart pin for kornia#4265: the constructor accepts int64 height/width --
- # that is what the class docstring's own example builds -- and a floating factor promotes them, but the
- # in-place twin scale_() writes the float result back into the int64 storage and raises.
- # Snippet used to generate expected: cam.scale_(0.5) on an int64 height executed 2026-09-05 (torch 2.14.0,
- # every dtype) -> RuntimeError("result type Float can't be cast to the desired output type Long").
- # Pins the CURRENT behavior; NOT a contract; delete when #4265 is repaired.
+ def test_scale_inplace_promotes_integer_image_size_4265(self, device, dtype):
+ # Regression for kornia#4265: the constructor accepts int64 height/width --
+ # that is what the class docstring's own example builds. A floating factor now
+ # promotes int64 to float in both scale() and scale_() after this repair (#4371).
+ # Integer factor preserves int64. Pins the repaired contract after #4371.
K = _k44(device, dtype)
cam = kornia.geometry.camera.PinholeCamera(
K,
@@ -836,14 +834,20 @@ class TestPinholeCamera(BaseTester):
self.assert_close(inplace.width, scaled.width)
self.assert_close(inplace.intrinsics, scaled.intrinsics)
assert cam.scale(torch.tensor([0.5], device=device, dtype=dtype)).height.is_floating_point()
- with pytest.raises(RuntimeError, match="can't be cast to the desired output type"):
- cam.scale_(0.5)
+ # scale_(0.5) now promotes int64 height/width to float instead of raising (#4371).
+ # Both the plain Python float (the issue's own call shape) and a tensor factor promote.
+ inplace = cam.scale_(0.5)
+ assert inplace is cam
+ assert cam.height.is_floating_point()
+ # int64 height/width * Python float 0.5 promotes to the default floating dtype
+ # (e.g. float32), not necessarily the camera's intrinsics dtype. Construct the
+ # expected values from the promoted tensors rather than the fixture dtype.
+ self.assert_close(cam.height, cam.height.new_tensor([3.0]), atol=0.0, rtol=0.0)
+ self.assert_close(cam.width, cam.width.new_tensor([4.0]), atol=0.0, rtol=0.0)
expected_K = _k44(device, dtype)
expected_K[:, :2, :3] *= 0.5
self.assert_close(K, _k44(device, dtype), atol=0.0, rtol=0.0)
self.assert_close(cam.intrinsics, expected_K, atol=0.0, rtol=0.0)
- self.assert_close(cam.height, torch.tensor([6], device=device))
- self.assert_close(cam.width, torch.tensor([8], device=device))
@pytest.mark.parametrize("shape", [(4, 4), (1, 3, 3), (1, 3, 4), (1, 5, 5), (1, 2, 3, 3), (1, 1, 1, 4, 4)])
@pytest.mark.parametrize("parameter", ["intrinsics", "extrinsics"])