π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
docs(geometry): keep the finite far depth of a zero disparity as depth_from_disparity's convention (#4272) (#6260)
Dmytro Mishkin0967a8e7e608
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.pyindex d55fc64ef..7067d6aa5 100644--- a/kornia/geometry/depth.py+++ b/kornia/geometry/depth.py@@ -769,19 +769,26 @@ def depth_from_disparity("""Compute depth from disparity.Convention:- - the depth is ``baseline * focal / disparity``, elementwise: ``baseline`` is the distance between the- two camera centres and ``focal`` the focal length in pixels, so a disparity of 2 with a baseline of- 0.5 and a focal length of 100 gives a depth of 25.+ - the depth is ``baseline * focal / (disparity + 1e-8)``, elementwise: ``baseline`` is the distance+ between the two camera centres and ``focal`` the focal length in pixels, so a disparity of 2 with a+ baseline of 0.5 and a focal length of 100 gives a depth of 25. The ``1e-8`` lowers the depth by a+ relative ``1e-8 / disparity``. Float32 rounds that away from a disparity of 0.25 up; below it the bias+ shows, ``1e-6`` relative (10 float32 ulps) at a disparity of 0.01.+ - a zero disparity (no stereo match, or a point at infinity) gives a finite far depth,+ ``baseline * focal / 1e-8`` (``5e9`` for baseline 0.5 and focal 100), not ``inf``. Such a point warps+ as a point at infinity, along the rotation-only homography, in+ :func:`~kornia.geometry.depth.warp_frame_depth`, whereas an ``inf`` depth would turn into ``NaN`` in+ :func:`~kornia.geometry.depth.depth_to_3d` wherever a ray component is zero. The far depth is a+ placeholder, not a measurement: mask ``disparity > 0`` to tell matched pixels apart.+ - in float16 the ``1e-8`` rounds to ``0`` and the far depth would overflow anyway, so a zero disparity+ gives ``inf``. A float32 far depth turns back into ``inf`` wherever the warp casts it to float16, as a+ float16 pose or float16 autocast does, so run the whole warp (depth, image, pose and camera matrix) in+ float32, outside float16 autocast, when zero disparities flow into it.- ``baseline`` and ``focal`` are each a scalar (Python number, 0-d or :math:`(1,)` tensor) shared by thewhole disparity, or a :math:`(B,)` tensor paired with ``disparity[b]``, which then needs a leadingbatch axis of size ``B`` (otherwise ``ShapeError``). The two may mix.- ``disparity`` is :math:`(*, H, W)` and the result has its shape. Its sign is not checked.- .. warning::- The divisor is ``disparity + 1e-8``, so a zero disparity (no stereo match) returns a large finite depth- set by the epsilon (``5e9`` for baseline ``0.5``, focal ``100``) rather than ``inf``, except where the- epsilon underflows: `#4272 <https://github.com/kornia/kornia/issues/4272>`_.-Args:disparity: Disparity tensor of shape :math:`(*, H, W)`.baseline: Distance between the two lenses, as an int, float, or tensor of shape ``()`` or ``(1,)``,diff --git a/tests/geometry/test_depth.py b/tests/geometry/test_depth.pyindex f8faa10f0..789bf15ec 100644--- a/tests/geometry/test_depth.py+++ b/tests/geometry/test_depth.py@@ -1147,11 +1147,10 @@ class TestDepthFromDisparity(BaseTester):# evaluate function gradientself.gradcheck(kornia.geometry.depth.depth_from_disparity, (disparity, baseline, focal))- def test_wart_zero_disparity_gives_a_finite_depth_4272(self, device, dtype):- # kornia#4272: depth_from_disparity computes ``baseline * focal / (disparity + 1e-8)``, so a zero- # disparity (the usual 'no match' fill) returns 5e9 for baseline 0.5 and focal 100, a function of the- # epsilon rather than inf. In float16 the 1e-8 rounds to 0 and the result is inf. Disparity 2 gives 25.- # Delete when #4272 is fixed.+ def test_convention_zero_disparity_gives_a_finite_far_depth_4272(self, device, dtype):+ # kornia#4272 (kept by ruling): depth_from_disparity computes ``baseline * focal / (disparity + 1e-8)``,+ # so a zero disparity (the usual 'no match' fill) gives the finite far depth 5e9 for baseline 0.5 and+ # focal 100, not inf. In float16 the 1e-8 rounds to 0 and the result is inf. Disparity 2 gives 25.disparity = torch.zeros(1, 1, 1, 1, device=device, dtype=dtype)depth = depth_from_disparity(disparity, 0.5, 100.0)if dtype == torch.float16:@@ -1164,6 +1163,33 @@ class TestDepthFromDisparity(BaseTester):torch.full((1, 1, 1, 1), 25.0, device=device, dtype=dtype),)+ def test_convention_zero_disparity_warps_as_a_point_at_infinity_4272(self, device, dtype):+ # kornia#4272 (kept by ruling): the far depth of a zero disparity sends warp_frame_depth along the+ # rotation-only homography K R K^-1, the limit for a point at infinity; an inf depth would give NaN.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("half-precision pixel coordinates round across the image border in this comparison")+ camera_matrix = torch.tensor(+ [[[100.0, 0.0, 4.0], [0.0, 100.0, 3.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype+ )+ angle = torch.tensor(0.01, device=device, dtype=dtype)+ src_trans_dst = _eye4(device, dtype).clone()+ src_trans_dst[0, 0, 0] = angle.cos()+ src_trans_dst[0, 0, 2] = angle.sin()+ src_trans_dst[0, 2, 0] = -angle.sin()+ src_trans_dst[0, 2, 2] = angle.cos()+ src_trans_dst[0, 0, 3] = 0.1+ image_src = torch.arange(48, device=device, dtype=dtype).view(1, 1, 6, 8)+ depth_dst = depth_from_disparity(torch.zeros(1, 1, 6, 8, device=device, dtype=dtype), 0.5, 100.0)++ warped = warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)++ assert torch.isfinite(warped).all()+ # Reference in CPU float64 (MPS has no float64).+ k64, r64 = camera_matrix.cpu().double(), src_trans_dst[:, :3, :3].cpu().double()+ h_inf = k64 @ r64 @ torch.linalg.inv(k64)+ expected = kornia.geometry.transform.warp_perspective(image_src.cpu().double(), torch.linalg.inv(h_inf), (6, 8))+ self.assert_close(warped, expected.to(device=device, dtype=dtype))+@pytest.mark.parametrize("disparity_shape", [(2, 1, 2, 3), (2, 2, 3)])@pytest.mark.parametrize("per_batch", ["baseline", "focal", "both"])def test_convention_per_batch_camera_parameters_4272(self, disparity_shape, per_batch, device, dtype):