Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

added normalize boolean for remap (#921)

Praneeta2080cd6fa52
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 990b4e03b..dc644e881 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -438,7 +438,7 @@ def get_rotation_matrix2d(
def remap(tensor: torch.Tensor, map_x: torch.Tensor, map_y: torch.Tensor,
mode: str = 'bilinear', padding_mode: str = 'zeros',
- align_corners: Optional[bool] = None) -> torch.Tensor:
+ align_corners: Optional[bool] = None, normalized_coordinates: bool = False) -> torch.Tensor:
r"""Applies a generic geometrical transformation to a tensor.
The function remap transforms the source tensor using the specified map:
@@ -458,6 +458,8 @@ def remap(tensor: torch.Tensor, map_x: torch.Tensor, map_y: torch.Tensor,
padding_mode (str): padding mode for outside grid values
'zeros' | 'border' | 'reflection'. Default: 'zeros'.
align_corners (bool, optional): mode for grid_generation. Default: None.
+ normalized_coordinates (bool): whether the input coordinates are
+ normalised in the range of [-1, 1]. Default: False
Returns:
torch.Tensor: the warped tensor with same shape as the input grid maps.
@@ -493,11 +495,13 @@ def remap(tensor: torch.Tensor, map_x: torch.Tensor, map_y: torch.Tensor,
# grid_sample need the grid between -1/1
map_xy: torch.Tensor = torch.stack([map_x, map_y], dim=-1)
- map_xy_norm: torch.Tensor = normalize_pixel_coordinates(
- map_xy, height, width)
+
+ # normalize coordinates if not already normalized
+ if not normalized_coordinates:
+ map_xy = normalize_pixel_coordinates(map_xy, height, width)
# simulate broadcasting since grid_sample does not support it
- map_xy_norm = map_xy_norm.expand(batch_size, -1, -1, -1)
+ map_xy_norm: torch.Tensor = map_xy.expand(batch_size, -1, -1, -1)
# warp ans return
tensor_warped: torch.Tensor = F.grid_sample(
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index f8b53d4d9..05f1a37af 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -449,6 +449,28 @@ class TestRemap:
input_warped = kornia.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
assert_allclose(input_warped, expected, rtol=1e-4, atol=1e-4)
+ def test_normalized_coordinates(self, device, dtype):
+ height, width = 3, 4
+ normalized_coordinates = True
+ inp = torch.tensor([[[
+ [1., 1., 1., 1.],
+ [1., 1., 1., 1.],
+ [1., 1., 1., 1.],
+ ]]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
+ expected = torch.tensor([[[
+ [1., 1., 1., 1.],
+ [1., 1., 1., 1.],
+ [1., 1., 1., 1.],
+ ]]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
+
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=normalized_coordinates, device=device).to(dtype)
+
+ # Normalized input coordinates
+ input_warped = kornia.remap(inp, grid[..., 0], grid[..., 1], align_corners=True,
+ normalized_coordinates=normalized_coordinates)
+ assert_allclose(input_warped, expected, rtol=1e-4, atol=1e-4)
+
def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
img = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)