Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement normal_transform_pixel and test

edgarriba7f25fded6b4d
diff --git a/test/test_utils.py b/test/test_utils.py
index 819a57c4b..e7f7f3086 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -4,6 +4,8 @@ import numpy as np
import torch
import torchgeometry as tgm
+import utils # test utils
+
class Tester(unittest.TestCase):
@@ -34,6 +36,29 @@ class Tester(unittest.TestCase):
self.assertTrue(tuple(
grid[0, height - 1, width - 1].numpy()) == (width - 1, height - 1))
+ def test_normalize_pixel_grid(self):
+ # generate input data
+ batch_size = 1
+ height, width = 2, 4
+
+ # create points grid
+ grid_norm = tgm.create_meshgrid(height, width,
+ normalized_coordinates=True)
+ grid_norm = torch.unsqueeze(grid_norm, dim=0)
+ grid_pix = tgm.create_meshgrid(height, width,
+ normalized_coordinates=False)
+ grid_pix = torch.unsqueeze(grid_pix, dim=0)
+
+ # grid from pixel space to normalized
+ norm_trans_pix = tgm.normal_transform_pixel(height, width) # 1x3x3
+ pix_trans_norm = tgm.inverse(norm_trans_pix) # 1x3x3
+ # transform grids
+ grid_pix_to_norm = tgm.transform_points(norm_trans_pix, grid_pix)
+ grid_norm_to_pix = tgm.transform_points(pix_trans_norm, grid_norm)
+ self.assertTrue(utils.check_equal_torch(grid_pix, grid_norm_to_pix))
+ self.assertTrue(utils.check_equal_torch(grid_norm, grid_pix_to_norm))
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/torchgeometry/imgwarp.py b/torchgeometry/imgwarp.py
index 9bfe3c460..41cbb6318 100644
--- a/torchgeometry/imgwarp.py
+++ b/torchgeometry/imgwarp.py
@@ -9,6 +9,7 @@ __all__ = [
"warp_perspective",
"get_perspective_transform",
"get_rotation_matrix2d",
+ "normal_transform_pixel",
]
@@ -36,6 +37,14 @@ def center_transform(transform, height, width):
torch.matmul(transform, origin_mat_center))
+def normal_transform_pixel(height, width):
+ return torch.tensor([[
+ [2. / (width - 1), 0., -1.],
+ [0., 2. / (height - 1), -1.],
+ [0., 0., 1.]]
+]) # 1x3x3
+
+
def normalize_transform_to_pix(transform, height, width):
assert len(transform.shape) == 3, transform.shape
normal_trans_pix = torch.tensor([[