Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement denormalize_coordinates

edgarribade6e4ec6760d
diff --git a/docs/source/geometry.conversions.rst b/docs/source/geometry.conversions.rst
index 403576fd9..0a60d0ab2 100644
--- a/docs/source/geometry.conversions.rst
+++ b/docs/source/geometry.conversions.rst
@@ -14,3 +14,4 @@ kornia.geometry.conversions
.. autofunction:: angle_axis_to_quaternion
.. autofunction:: rtvec_to_pose
.. autofunction:: normalize_pixel_coordinates
+.. autofunction:: denormalize_pixel_coordinates
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 47ae1fed9..26065729f 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -15,6 +15,7 @@ __all__ = [
"angle_axis_to_quaternion",
"rtvec_to_pose",
"normalize_pixel_coordinates",
+ "denormalize_pixel_coordinates",
]
EPS = 1e-6
@@ -441,7 +442,7 @@ def normalize_pixel_coordinates(
height (int): the maximum height in the y-axis.
Return:
- torch.Tensor: the nornmalized pixel coordinates.
+ torch.Tensor: the normalized pixel coordinates.
"""
if pixel_coordinates.shape[-1] != 2:
raise ValueError("Input pixel_coordinates must be of shape (*, 2). "
@@ -459,5 +460,36 @@ def normalize_pixel_coordinates(
return pixel_coordinates_norm
+def denormalize_pixel_coordinates(
+ pixel_coordinates: torch.Tensor,
+ height: int,
+ width: int) -> torch.Tensor:
+ r"""Denormalize pixel coordinates.
+
+ The input is assumed to be -1 if on extreme left, 1 if on
+ extreme right (x = w-1).
+
+ Args:
+ pixel_coordinate (torch.Tensor): the normalized grid coordinates.
+ Shape can be :math:`(*, 2)`.
+ width (int): the maximum width in the x-axis.
+ height (int): the maximum height in the y-axis.
+
+ Return:
+ torch.Tensor: the denormalized pixel coordinates.
+ """
+ if pixel_coordinates.shape[-1] != 2:
+ raise ValueError("Input pixel_coordinates must be of shape (*, 2). "
+ "Got {}".format(pixel_coordinates.shape))
+ # compute normalization factor
+ hw: torch.Tensor = torch.stack([
+ torch.tensor(width), torch.tensor(height)
+ ]).to(pixel_coordinates.device).to(pixel_coordinates.dtype)
+
+ factor: torch.Tensor = torch.tensor(2.) / (hw - torch.tensor(1.))
+
+ return torch.tensor(1.) / factor * (pixel_coordinates + 1)
+
+
# TODO: add below funtionalities
# - pose_to_rtvec
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index 5f13e103f..5415eab2d 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -1,14 +1,14 @@
import pytest
import numpy as np
+import kornia
+from kornia.testing import tensor_to_gradcheck_var, create_eye_batch
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import kornia as kornia
-from kornia.testing import tensor_to_gradcheck_var, create_eye_batch
-from ..common import device_type
-
# based on:
# https://github.com/ceres-solver/ceres-solver/blob/master/internal/ceres/rotation_test.cc#L271
@@ -345,7 +345,7 @@ class TestNormalizePixelCoordinates:
assert_allclose(actual, expected)
-'''class TestDenormalizePixelCoordinates:
+class TestDenormalizePixelCoordinates:
def test_tensor_bhw2(self):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
@@ -362,14 +362,14 @@ class TestNormalizePixelCoordinates:
def test_list(self):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False)
+ height, width, normalized_coordinates=True)
grid = grid.contiguous().view(-1, 2)
expected = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True)
+ height, width, normalized_coordinates=False)
expected = expected.contiguous().view(-1, 2)
- grid_norm = kornia.normalize_pixel_coordinates(
+ grid_norm = kornia.denormalize_pixel_coordinates(
grid, height, width)
assert_allclose(grid_norm, expected)
@@ -378,38 +378,13 @@ class TestNormalizePixelCoordinates:
@torch.jit.script
def op_script(input: torch.Tensor, height: int,
width: int) -> torch.Tensor:
- return kornia.normalize_pixel_coordinates(input, height, width)
+ return kornia.denormalize_pixel_coordinates(input, height, width)
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False)
+ height, width, normalized_coordinates=True)
actual = op_script(grid, height, width)
- expected = kornia.normalize_pixel_coordinates(
+ expected = kornia.denormalize_pixel_coordinates(
grid, height, width)
assert_allclose(actual, expected)
-
- def test_jit_trace(self):
- @torch.jit.script
- def op_script(input, height, width):
- return kornia.normalize_pixel_coordinates(input, height, width)
- # 1. Trace op
- height, width = 3, 4
- grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False)
- op_traced = torch.jit.trace(
- op_script,
- (grid, torch.tensor(height), torch.tensor(width),))
-
- # 2. Generate new input
- height, width = 2, 5
- grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).repeat(2, 1, 1, 1)
-
- # 3. Evaluate
- actual = op_traced(
- grid, torch.tensor(height), torch.tensor(width))
- expected = kornia.normalize_pixel_coordinates(
- grid, height, width)
-
- assert_allclose(actual, expected)'''