Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add warp_perspective

edgarriba24e6e8ed4617
diff --git a/test/test_imgwarp.py b/test/test_imgwarp.py
new file mode 100644
index 000000000..7261313a8
--- /dev/null
+++ b/test/test_imgwarp.py
@@ -0,0 +1,62 @@
+import unittest
+
+import torch
+import torchgeometry as tgm
+from torch.autograd import gradcheck
+
+import utils # test utils
+
+
+class Tester(unittest.TestCase):
+
+ def test_warp_perspective(self):
+ # generate input data
+ batch_size = 1
+ height, width = 64, 64
+ alpha = tgm.pi / 2 # 90 deg rotation
+
+ # create data patch
+ patch = torch.rand(batch_size, 1, height, width)
+
+ # create transformation (rotation)
+ M = torch.tensor([[
+ [torch.cos(alpha), -torch.sin(alpha), 0.],
+ [torch.sin(alpha), torch.cos(alpha), 0.],
+ [ 0., 0., 1.],
+ ]]) # Bx3x3
+
+ # apply transformation and inverse
+ _, _, h, w = patch.shape
+ patch_warped = tgm.warp_perspective(patch, M, dsize=(height, width))
+ patch_warped_inv = tgm.warp_perspective(patch_warped, tgm.inverse(M),
+ dsize=(height, width))
+
+ res = utils.check_equal_torch(patch, patch_warped_inv)
+ self.assertTrue(res)
+
+ def test_warp_perspective_gradcheck(self):
+ # generate input data
+ batch_size = 1
+ height, width = 16, 32
+ alpha = tgm.pi / 2 # 90 deg rotation
+
+ # create data patch
+ patch = torch.rand(batch_size, 1, height, width)
+ patch = utils.tensor_to_gradcheck_var(patch) # to var
+
+ # create transformation (rotation)
+ M = torch.tensor([[
+ [torch.cos(alpha), -torch.sin(alpha), 0.],
+ [torch.sin(alpha), torch.cos(alpha), 0.],
+ [ 0., 0., 1.],
+ ]]) # Bx3x3
+ M = utils.tensor_to_gradcheck_var(M, requires_grad=False) # to var
+
+ # evaluate function gradient
+ res = gradcheck(tgm.warp_perspective, (patch, M, (height, width,)),
+ raise_exception=True)
+ self.assertTrue(res)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.py
index 3f6bfd871..f0da61ed2 100644
--- a/torchgeometry/__init__.py
+++ b/torchgeometry/__init__.py
@@ -3,6 +3,7 @@ from .depth_warper import DepthWarper, depth_warp
from .pinhole import *
from .conversions import *
from .utils import *
+from .imgwarp import *
__version__ = '0.1.0' # the current version of the lib
diff --git a/torchgeometry/imgwarp.py b/torchgeometry/imgwarp.py
new file mode 100644
index 000000000..84db4ae3c
--- /dev/null
+++ b/torchgeometry/imgwarp.py
@@ -0,0 +1,48 @@
+import torch
+
+from .homography_warper import homography_warp
+
+
+__all__ = [
+ "warp_perspective",
+]
+
+
+def warp_perspective(src, M, dsize, flags='bilinear', border_mode=None,
+ border_value=0):
+ """Applies a perspective transformation to an image.
+
+ The function warpPerspective transforms the source image using the specified matrix:
+
+ .. math::
+ dst(x, y) = src \left(\\frac{M_{11} x + M_{12} y + M_{33}}{M_{31} x + M_{32} y + M_{33}} , \\frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}}\\right)
+
+ Args:
+ src (Tensor): input image.
+ M (Tensor): transformation matrix.
+ dsize (tuple): size of the output image (height, width).
+
+ Returns:
+ Tensor: the warped input image.
+
+ Shape:
+ - Input: :math:`(B, C, H, W)` and :math:`(B, 3, 3)`
+ - Output: :math:`(B, C, H, W)`
+
+ .. note::
+ See a working example `here <../../../examples/warp_perspective.ipynb>`_.
+ """
+ if not torch.is_tensor(src):
+ raise TypeError("Input src type is not a torch.Tensor. Got {}"
+ .format(type(src)))
+ if not torch.is_tensor(M):
+ raise TypeError("Input M type is not a torch.Tensor. Got {}"
+ .format(type(M)))
+ if not len(src.shape) == 4:
+ raise ValueError("Input src must be a BxCxHxW tensor. Got {}"
+ .format(src.shape))
+ if not (len(M.shape) == 3 or M.shape[-2:] == (3, 3)):
+ raise ValueError("Input M must be a Bx3x3 tensor. Got {}"
+ .format(src.shape))
+ # warp and return
+ return homography_warp(src, M, dsize)