Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): divide point_line_distance by the weight of a homogeneous point (#4975)

εŒΊζ’“ηabd9ca317b65
diff --git a/changelog.d/4975.fixed.md b/changelog.d/4975.fixed.md
new file mode 100644
index 000000000..2ee9d3c01
--- /dev/null
+++ b/changelog.d/4975.fixed.md
@@ -0,0 +1,4 @@
+`point_line_distance` now divides by the weight of a homogeneous `(*, N, 3)` point, `|ax + by + cw| / (|w| |(a, b)|)`,
+and returns `inf` for a point at infinity (#4935). The weight was ignored, so a point with `w != 1` got the distance
+of `(x, y)` instead of `(x / w, y / w)`, and `left_to_right_epipolar_distance` and `right_to_left_epipolar_distance`
+inherited it for `(*, N, 3)` input. 2-D points and `w = 1` are unchanged.
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index 991960786..82c928db3 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -258,7 +258,8 @@ def point_line_distance(point: torch.Tensor, line: torch.Tensor, eps: float = 1e
r"""Return the distance from points to lines.
Args:
- point: (possibly homogeneous) points :math:`(*, N, 2 or 3)`.
+ point: points :math:`(*, N, 2)`, or homogeneous points :math:`(*, N, 3)` whose last coordinate is the
+ weight :math:`w`; a point at infinity (:math:`w = 0`) is at distance ``inf`` from every line.
line: lines coefficients :math:`(a, b, c)` with shape :math:`(*, N, 3)`, where :math:`ax + by + c = 0`.
eps: Small constant for safe sqrt.
@@ -278,13 +279,26 @@ def point_line_distance(point: torch.Tensor, line: torch.Tensor, eps: float = 1e
# Using in-place operations to improve performance
numerator = line[..., 0] * point[..., 0]
numerator += line[..., 1] * point[..., 1]
- numerator += line[..., 2]
+ if point.shape[-1] == 3:
+ numerator += line[..., 2] * point[..., 2]
+ else:
+ numerator += line[..., 2]
numerator.abs_()
# Avoid computing norm multiple times by saving its value
denom_norm = (line[..., 0].square() + line[..., 1].square()).sqrt()
- return numerator / (denom_norm + eps)
+ distance = numerator / (denom_norm + eps)
+ if point.shape[-1] == 3:
+ # (x, y, w) is the Euclidean point (x / w, y / w), so its distance is |ax + by + cw| / (|w| |(a, b)|); the
+ # weight used to be ignored, which is right only for w = 1 (#4935). A point at infinity (w = 0) is at
+ # distance inf; torch.where also differentiates the branch it does not select, so that division is kept
+ # finite.
+ w = point[..., 2].abs()
+ at_infinity = w == 0
+ safe_w = torch.where(at_infinity, torch.ones_like(w), w)
+ distance = torch.where(at_infinity, torch.full_like(distance, torch.inf), distance / safe_w)
+ return distance
def batched_dot_product(x: torch.Tensor, y: torch.Tensor, keepdim: bool = False) -> torch.Tensor:
diff --git a/tests/geometry/epipolar/test_epipolar_metrics.py b/tests/geometry/epipolar/test_epipolar_metrics.py
index ff44a0106..e93c66610 100644
--- a/tests/geometry/epipolar/test_epipolar_metrics.py
+++ b/tests/geometry/epipolar/test_epipolar_metrics.py
@@ -169,6 +169,17 @@ class TestLeftToRightEpipolarDistance(BaseTester):
self.gradcheck(epi.left_to_right_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))
+ def test_homogeneous_weight_4935(self, device, dtype):
+ # #4935: the measured point went to point_line_distance with its weight ignored, so (10, 14, 2) scored 6.08
+ # where the same point (5, 7) scored 13.72.
+ Fm = torch.tensor([[[0.0, -0.02, 0.3], [0.02, 0.0, -0.9], [-0.3, 0.9, 0.1]]], device=device, dtype=dtype)
+ pts1 = torch.tensor([[[10.0, 20.0]]], device=device, dtype=dtype)
+ pts2 = torch.tensor([[[5.0, 7.0]]], device=device, dtype=dtype)
+ pts2_weighted = torch.tensor([[[10.0, 14.0, 2.0]]], device=device, dtype=dtype)
+ expected = epi.left_to_right_epipolar_distance(pts1, pts2, Fm)
+ self.assert_close(expected, torch.tensor([[13.717871]], device=device, dtype=dtype))
+ self.assert_close(epi.left_to_right_epipolar_distance(pts1, pts2_weighted, Fm), expected)
+
class TestRightToLeftEpipolarDistance(BaseTester):
def test_smoke(self, device, dtype):
@@ -218,6 +229,16 @@ class TestRightToLeftEpipolarDistance(BaseTester):
self.gradcheck(epi.right_to_left_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))
+ def test_homogeneous_weight_4935(self, device, dtype):
+ # #4935: the measured point went to point_line_distance with its weight ignored, so (20, 40, 2) scored 29.54
+ # where the same point (10, 20) scored 11.89.
+ Fm = torch.tensor([[[0.0, -0.02, 0.3], [0.02, 0.0, -0.9], [-0.3, 0.9, 0.1]]], device=device, dtype=dtype)
+ pts1 = torch.tensor([[[10.0, 20.0]]], device=device, dtype=dtype)
+ pts1_weighted = torch.tensor([[[20.0, 40.0, 2.0]]], device=device, dtype=dtype)
+ pts2 = torch.tensor([[[5.0, 7.0]]], device=device, dtype=dtype)
+ expected = epi.right_to_left_epipolar_distance(pts1, pts2, Fm)
+ self.assert_close(epi.right_to_left_epipolar_distance(pts1_weighted, pts2, Fm), expected)
+
_HALF_PIXEL_F = (
"a pixel-unit F spans eight decades (entries down to ~1e-8): float16 flushes the small entries to zero and "
diff --git a/tests/geometry/test_linalg.py b/tests/geometry/test_linalg.py
index f94cc6364..9b11b3688 100644
--- a/tests/geometry/test_linalg.py
+++ b/tests/geometry/test_linalg.py
@@ -419,6 +419,26 @@ class TestPointsLinesDistances(BaseTester):
pts = torch.rand(2, 3, 2, device=device, requires_grad=True, dtype=torch.float64)
lines = torch.rand(2, 3, 3, device=device, requires_grad=True, dtype=torch.float64)
self.gradcheck(kgl.point_line_distance, (pts, lines))
+ # homogeneous points with weights away from 0 and 1
+ pts = (torch.rand(2, 3, 3, device=device, dtype=torch.float64) + 0.5).requires_grad_(True)
+ self.gradcheck(kgl.point_line_distance, (pts, lines))
+
+ def test_homogeneous_weight_4935(self, device, dtype):
+ # #4935: the third coordinate of a (*, N, 3) point was ignored, so the point (1.5, -0.7) was 2.68 from
+ # 3x + 4y + 10 = 0 when written as (3, -1.4, 2) and 1.66 as (-1.5, 0.7, -1), instead of 2.34. The homogeneous
+ # distance is |ax + by + cw| / (|w| |(a, b)|), and a point at infinity (w = 0) is at distance inf.
+ line = torch.tensor([[[3.0, 4.0, 10.0]]], device=device, dtype=dtype)
+ point = torch.tensor([[[1.5, -0.7]]], device=device, dtype=dtype)
+ expected = torch.tensor([[2.34]], device=device, dtype=dtype)
+ self.assert_close(kgl.point_line_distance(point, line), expected)
+ for w in (1.0, 2.0, -1.0, 0.25):
+ homogeneous = torch.cat((w * point, torch.full_like(point[..., :1], w)), dim=-1)
+ self.assert_close(kgl.point_line_distance(homogeneous, line), expected)
+ at_infinity = torch.tensor([[[1.5, -0.7, 0.0]]], device=device, dtype=dtype, requires_grad=True)
+ distance = kgl.point_line_distance(at_infinity, line)
+ assert bool(distance.isposinf().all()), distance
+ distance.sum().backward()
+ assert bool(torch.isfinite(at_infinity.grad).all()), at_infinity.grad
class TestEuclideanDistance(BaseTester):