π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added denormalize homography function (#1061)
Dmytro Mishkineac733c235ca
diff --git a/docs/source/geometry.warp.rst b/docs/source/geometry.warp.rstindex 05baebe93..4b4bc471f 100644--- a/docs/source/geometry.warp.rst+++ b/docs/source/geometry.warp.rst@@ -13,5 +13,7 @@ kornia.geometry.warp.. autofunction:: depth_warp.. autofunction:: warp_grid.. autofunction:: normalize_homography+.. autofunction:: denormalize_homography+.. autofunction:: normalize_homography3d.. autofunction:: normal_transform_pixel.. autofunction:: normal_transform_pixel3ddiff --git a/kornia/geometry/transform/homography_warper.py b/kornia/geometry/transform/homography_warper.pyindex 3ea7912e3..081928f23 100644--- a/kornia/geometry/transform/homography_warper.py+++ b/kornia/geometry/transform/homography_warper.py@@ -16,6 +16,7 @@ __all__ = ["warp_grid","warp_grid3d","normalize_homography",+ "denormalize_homography","normalize_homography3d","normal_transform_pixel","normal_transform_pixel3d",@@ -400,6 +401,41 @@ def normalize_homography(return dst_norm_trans_src_norm+def denormalize_homography(+ dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]+) -> torch.Tensor:+ r"""De-normalize a given homography in pixels from [-1, 1] to actual height and width.++ Args:+ dst_pix_trans_src_pix (torch.Tensor): homography/ies from source to destination to be+ denormalized. :math:`(B, 3, 3)`+ dsize_src (tuple): size of the source image (height, width).+ dsize_dst (tuple): size of the destination image (height, width).++ Returns:+ torch.Tensor: the denormalized homography of shape :math:`(B, 3, 3)`.+ """+ check_is_tensor(dst_pix_trans_src_pix)++ if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):+ raise ValueError(+ "Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {}".format(dst_pix_trans_src_pix.shape)+ )++ # source and destination sizes+ src_h, src_w = dsize_src+ dst_h, dst_w = dsize_dst++ # compute the transformation pixel/norm for src/dst+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)++ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)+ dst_denorm_trans_dst_pix = _torch_inverse_cast(dst_norm_trans_dst_pix)+ # compute chain transformations+ dst_norm_trans_src_norm: torch.Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)+ return dst_norm_trans_src_norm++def normalize_homography3d(dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]) -> torch.Tensor:diff --git a/test/geometry/transform/test_homography_warper.py b/test/geometry/transform/test_homography_warper.pyindex f98cbf6c0..536cd68d5 100644--- a/test/geometry/transform/test_homography_warper.py+++ b/test/geometry/transform/test_homography_warper.py@@ -37,9 +37,6 @@ class TestHomographyWarper:norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width))assert (norm_homo == dst_homo_src).all()- norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width))- assert (norm_homo == dst_homo_src).all()-# change output scalenorm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height * 2, width // 2))res = torch.tensor(@@ -47,6 +44,25 @@ class TestHomographyWarper:).repeat(batch_size, 1, 1)assert_allclose(norm_homo, res, atol=1e-4, rtol=1e-4)+ @pytest.mark.parametrize("batch_size", [1, 3])+ def test_denormalize_homography_identity(self, batch_size, device, dtype):+ # create input data+ height, width = 2, 5+ dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)++ res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)+ assert (kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) == res).all()++ denorm_homo = kornia.denormalize_homography(dst_homo_src, (height, width), (height, width))+ assert (denorm_homo == dst_homo_src).all()++ # change output scale+ denorm_homo = kornia.denormalize_homography(dst_homo_src, (height, width), (height * 2, width // 2))+ res = torch.tensor([[[0.25, 0.0, 0.0], [0.0, 3.0, 0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).repeat(+ batch_size, 1, 1+ )+ assert_allclose(denorm_homo, res, atol=1e-4, rtol=1e-4)+@pytest.mark.parametrize("batch_size", [1, 3])def test_normalize_homography_general(self, batch_size, device, dtype):# create input data@@ -62,6 +78,39 @@ class TestHomographyWarper:res = torch.tensor([[[0.5, 0.0, 0.0], [0.0, 2.0, 5.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)assert (norm_homo == res).all()+ @pytest.mark.parametrize("batch_size", [1, 3])+ def test_denormalize_homography_general(self, batch_size, device, dtype):+ # create input data+ height, width = 2, 5+ dst_homo_src = torch.eye(3, device=device, dtype=dtype)+ dst_homo_src[..., 0, 0] = 0.5+ dst_homo_src[..., 1, 1] = 2.0+ dst_homo_src[..., 0, 2] = 1.0+ dst_homo_src[..., 1, 2] = 2.0+ dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)++ denorm_homo = kornia.denormalize_homography(dst_homo_src, (height, width), (height, width))+ res = torch.tensor([[[0.5, 0.0, 3.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)+ assert (denorm_homo == res).all()++ @pytest.mark.parametrize("batch_size", [1, 3])+ def test_consistency(self, batch_size, device, dtype):+ # create input data+ height, width = 2, 5+ dst_homo_src = torch.eye(3, device=device, dtype=dtype)+ dst_homo_src[..., 0, 0] = 0.5+ dst_homo_src[..., 1, 1] = 2.0+ dst_homo_src[..., 0, 2] = 1.0+ dst_homo_src[..., 1, 2] = 2.0+ dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)++ denorm_homo = kornia.denormalize_homography(dst_homo_src, (height, width), (height, width))+ norm_denorm_homo = kornia.normalize_homography(denorm_homo, (height, width), (height, width))+ assert (dst_homo_src == norm_denorm_homo).all()+ norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width))+ denorm_norm_homo = kornia.denormalize_homography(norm_homo, (height, width), (height, width))+ assert (dst_homo_src == denorm_norm_homo).all()+@pytest.mark.parametrize("offset", [1, 3, 7])@pytest.mark.parametrize("shape", [(4, 5), (2, 6), (4, 3), (5, 7)])def test_warp_grid_translation(self, shape, offset, device, dtype):