π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
making `compute_correspond_epilines` work with fundamental and point of volumetric tensors (#1585)
nitaifingerhut39a77539c580
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex 44b9c7b9b..6b0e181b7 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -168,10 +168,10 @@ def convert_points_to_homogeneous(points: torch.Tensor) -> torch.Tensor:r"""Function that converts points from Euclidean to homogeneous space.Args:- points: the points to be transformed with shape :math:`(B, N, D)`.+ points: the points to be transformed with shape :math:`(*, N, D)`.Returns:- the points in homogeneous coordinates :math:`(B, N, D+1)`.+ the points in homogeneous coordinates :math:`(*, N, D+1)`.Examples:>>> input = torch.tensor([[0., 0.]])diff --git a/kornia/geometry/epipolar/fundamental.py b/kornia/geometry/epipolar/fundamental.pyindex a797487d5..bf272055f 100644--- a/kornia/geometry/epipolar/fundamental.py+++ b/kornia/geometry/epipolar/fundamental.py@@ -126,31 +126,32 @@ def compute_correspond_epilines(points: torch.Tensor, F_mat: torch.Tensor) -> tor"""Compute the corresponding epipolar line for a given set of points.Args:- points: tensor containing the set of points to project in the shape of :math:`(B, N, 2)`.- F_mat: the fundamental to use for projection the points in the shape of :math:`(B, 3, 3)`.+ points: tensor containing the set of points to project in the shape of :math:`(*, N, 2)`.+ F_mat: the fundamental to use for projection the points in the shape of :math:`(*, 3, 3)`.Returns:- a tensor with shape :math:`(B, N, 3)` containing a vector of the epipolar+ a tensor with shape :math:`(*, N, 3)` containing a vector of the epipolarlines corresponding to the points to the other image. Each line is described as:math:`ax + by + c = 0` and encoding the vectors as :math:`(a, b, c)`."""- if not (len(points.shape) == 3 and points.shape[2] == 2):+ if not (len(points.shape) >= 2 and points.shape[-1] == 2):raise AssertionError(points.shape)- if not (len(F_mat.shape) == 3 and F_mat.shape[-2:] == (3, 3)):+ if not (len(F_mat.shape) >= 2 and F_mat.shape[-2:] == (3, 3)):raise AssertionError(F_mat.shape)points_h: torch.Tensor = convert_points_to_homogeneous(points)# project points and retrieve lines components- a, b, c = torch.chunk(F_mat @ points_h.permute(0, 2, 1), dim=1, chunks=3)+ points_h = torch.transpose(points_h, dim0=-2, dim1=-1)+ a, b, c = torch.chunk(F_mat @ points_h, dim=-2, chunks=3)# compute normal and compose equation linenu: torch.Tensor = a * a + b * bnu = torch.where(nu > 0.0, 1.0 / torch.sqrt(nu), torch.ones_like(nu))- line = torch.cat([a * nu, b * nu, c * nu], dim=1) # Bx3xN- return line.permute(0, 2, 1) # BxNx3+ line = torch.cat([a * nu, b * nu, c * nu], dim=-2) # *x3xN+ return torch.transpose(line, dim0=-2, dim1=-1) # *xNx3def fundamental_from_essential(E_mat: torch.Tensor, K1: torch.Tensor, K2: torch.Tensor) -> torch.Tensor:diff --git a/test/geometry/epipolar/test_fundamental.py b/test/geometry/epipolar/test_fundamental.pyindex 0cb183794..fa78823bd 100644--- a/test/geometry/epipolar/test_fundamental.py+++ b/test/geometry/epipolar/test_fundamental.py@@ -173,7 +173,7 @@ class TestFindFundamental:assert gradcheck(epi.find_fundamental, (points1, points2, weights), raise_exception=True)-class TestComputeCorrespondEpilimes:+class TestComputeCorrespondEpilines:def test_smoke(self, device, dtype):point = torch.rand(1, 1, 2, device=device, dtype=dtype)F_mat = torch.rand(1, 3, 3, device=device, dtype=dtype)@@ -188,6 +188,22 @@ class TestComputeCorrespondEpilimes:lines = epi.compute_correspond_epilines(point, F_mat)assert lines.shape == (B, N, 3)+ @pytest.mark.parametrize(+ "batch_size, num_frames, num_points",+ [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)],+ )+ def test_volumetric(self, batch_size, num_frames, num_points, device, dtype):+ B, T, N = batch_size, num_frames, num_points+ point = torch.rand(B, T, N, 2, device=device, dtype=dtype)+ F_mat = torch.rand(B, T, 3, 3, device=device, dtype=dtype)++ lines_T_hops = torch.zeros(B, T, N, 3, device=device, dtype=dtype)+ for i in range(T):+ lines_T_hops[:, i, ...] = epi.compute_correspond_epilines(point[:, i, ...], F_mat[:, i, ...])+ lines_one_hop = epi.compute_correspond_epilines(point, F_mat)++ assert_close(lines_T_hops, lines_one_hop, atol=1e-8, rtol=1e-8)+def test_opencv(self, device, dtype):point = torch.rand(1, 2, 2, device=device, dtype=dtype)F_mat = torch.rand(1, 3, 3, device=device, dtype=dtype)