🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
test(geometry): re-enable the skipped `get_perspective_transform3d` test (#6310)
Debanjan Sarkard2f861d01581
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.pyindex bf528bbb4..9400f88cc 100644--- a/kornia/geometry/transform/imgwarp.py+++ b/kornia/geometry/transform/imgwarp.py@@ -1635,7 +1635,7 @@ def get_projective_transform(center: torch.Tensor, angles: torch.Tensor, scales:def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.Tensor:- r"""Calculate a 3d perspective transform from four pairs of the corresponding points.+ r"""Calculate a 3d perspective transform from five of eight pairs of corresponding box corners.The function calculates the matrix of a perspective transform so that:@@ -1694,12 +1694,15 @@ def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.TConvention:- points: ``(x, y, z)``, pixel centers, origin at the top-left of the first depth slice; shape :math:`(B, 8, 3)`+ - the 15 unknowns are solved exactly from points 0, 1, 2, 5 and 7, the corners ``000``, ``100``, ``110``,+ ``101`` and ``011`` in :func:`~kornia.geometry.bbox.bbox_generator3d` order; points 3, 4 and 6 are not+ used, so they map onto their ``dst`` only when all eight pairs are related by one homography- returns the source→destination **pixel** homography :math:`(B, 4, 4)`Args:- src: coordinates of quadrangle vertices in the source image with shape :math:`(B, 8, 3)`.- dst: coordinates of the corresponding quadrangle vertices in- the destination image with shape :math:`(B, 8, 3)`.+ src: coordinates of the box corners in the source volume with shape :math:`(B, 8, 3)`.+ dst: coordinates of the corresponding box corners in+ the destination volume with shape :math:`(B, 8, 3)`.Returns:the perspective transformation with shape :math:`(B, 4, 4)`.@@ -1729,9 +1732,7 @@ def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.Tf"with the same dtype (Got {src.dtype}, {dst.dtype}).")- # we build matrix A by using only 4 point correspondence. The linear- # system is solved with the least square method, so here- # we could even pass more correspondence+ # we build the square matrix A from 5 point correspondences, 3 equations each, and solve it exactlyp = []# 000, 100, 110, 101, 011diff --git a/tests/geometry/transform/test_imgwarp3d.py b/tests/geometry/transform/test_imgwarp3d.pyindex 933c101e4..2762d1233 100644--- a/tests/geometry/transform/test_imgwarp3d.py+++ b/tests/geometry/transform/test_imgwarp3d.py@@ -948,31 +948,44 @@ class TestGetRotationMatrix3d(BaseTester):class TestPerspectiveTransform3D(BaseTester):- @pytest.mark.skip("Not working")+ # Box corners in bbox_generator3d order: 000, 100, 110, 010, 001, 101, 111, 011.+ _corners = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 1))+@pytest.mark.parametrize("batch_size", [1, 2, 5])def test_get_perspective_transform3d(self, batch_size, device, dtype):- # generate input data- # d_max, h_max, w_max = 16, 64, 32 # height, width- # d = torch.ceil(d_max * torch.rand(batch_size, device=device, dtype=dtype))- # h = torch.ceil(h_max * torch.rand(batch_size, device=device, dtype=dtype))- # w = torch.ceil(w_max * torch.rand(batch_size, device=device, dtype=dtype))-- norm = torch.rand(batch_size, 8, 3, device=device, dtype=dtype)- points_src = torch.rand_like(norm, device=device, dtype=dtype)- points_dst = points_src + norm-- # compute transform from source to target- dst_homo_src = kornia.geometry.transform.get_perspective_transform3d(points_src, points_dst)-- # TODO: get_perspective_transform3d seems to be correct since it would result in the- # expected output for cropping volumes. Not sure what is going on here.- self.assert_close(- kornia.geometry.linalg.transform_points(dst_homo_src, points_src), points_dst, rtol=1e-4, atol=1e-4- )+ # Eight random point pairs are in general not related by any 3D homography (15 degrees of freedom against+ # 24 constraints), which is why this test used to fail. The destination points are the source corners+ # mapped by known homographies instead, built in float64 and then cast.+ torch.manual_seed(0)+ sizes = torch.tensor([7.0, 5.0, 3.0], dtype=torch.float64) + torch.rand(batch_size, 1, 3, dtype=torch.float64)+ offsets = torch.rand(batch_size, 1, 3, dtype=torch.float64)+ points_src = torch.tensor(self._corners, dtype=torch.float64) * sizes + offsets+ dst_homo_src = torch.eye(4, dtype=torch.float64) + 0.1 * torch.randn(batch_size, 4, 4, dtype=torch.float64)+ dst_homo_src[:, 3, :3] *= 0.1+ dst_homo_src = dst_homo_src / dst_homo_src[:, 3:, 3:]+ points_dst = kornia.geometry.linalg.transform_points(dst_homo_src, points_src)+ points_src = points_src.to(device, dtype)+ points_dst = points_dst.to(device, dtype)++ out = kornia.geometry.transform.get_perspective_transform3d(points_src, points_dst)++ self.assert_close(out, dst_homo_src.to(device, dtype), low_tolerance=True)+ self.assert_close(kornia.geometry.linalg.transform_points(out, points_src), points_dst, low_tolerance=True)# compute gradient checkself.gradcheck(kornia.geometry.transform.get_perspective_transform3d, (points_src, points_dst), fast_mode=False)+ def test_convention_uses_corners_0_1_2_5_7(self, device, dtype):+ # The 15 unknowns are solved exactly from corners 0, 1, 2, 5 and 7 (000, 100, 110, 101, 011); the+ # destinations of corners 3, 4 and 6 do not enter the solve.+ points_src = torch.tensor(self._corners, device=device, dtype=dtype)[None] * 4+ points_dst = points_src * 2 + 1+ moved_dst = points_dst.clone()+ moved_dst[:, [3, 4, 6]] += 5+ expected = kornia.geometry.transform.get_perspective_transform3d(points_src, points_dst)+ out = kornia.geometry.transform.get_perspective_transform3d(points_src, moved_dst)+ self.assert_close(out, expected)+@pytest.mark.parametrize("batch_size", [1, 2])def test_get_perspective_transform3d_2(self, batch_size, device, dtype):torch.manual_seed(0)