Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

transform boxes

dvd423fdc4811fab7
diff --git a/docs/source/geometry.linalg.rst b/docs/source/geometry.linalg.rst
index 2e24e51e6..d9bd98b8d 100644
--- a/docs/source/geometry.linalg.rst
+++ b/docs/source/geometry.linalg.rst
@@ -8,3 +8,4 @@ kornia.geometry.linalg
.. autofunction:: compose_transformations
.. autofunction:: inverse_transformation
.. autofunction:: transform_points
+.. autofunction:: transform_boxes
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index 0e10f36e4..8b4898044 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -11,6 +11,7 @@ __all__ = [
"relative_transformation",
"inverse_transformation",
"transform_points",
+ "transform_boxes",
"perspective_transform_lafs",
]
@@ -213,6 +214,53 @@ def transform_points(trans_01: torch.Tensor,
return points_0
+def transform_boxes(trans_mat: torch.Tensor, boxes: torch.Tensor, mode: str = "xyxy") -> torch.Tensor:
+
+ r""" Function that applies a transformation matrix to a box or batch of boxes. Boxes must
+ be a tensor of the shape (4, ) or a batch of boxes (B, 4) and trans_mat must be a (3, 3)
+ transformation matrix
+
+ Args:
+ trans_mat (torch.Tensor): The transformation matrix to be applied
+ boxes (torch.Tensor): The boxes to be transformed
+ mode (str): The format in which the boxes are provided. If set to 'xyxy' the boxes
+ are assumed to be in the format (xmin, ymin, xmax, ymax). If set to 'xywh'
+ the boxes are assumed to be in the format (xmin, ymin, width, height).
+ Default: 'xyxy'
+ Returns:
+ torch.Tensor: The set of transformed points in the specified mode
+
+
+ """
+
+ if not torch.is_tensor(boxes):
+ raise TypeError(f"Boxes type is not a torch.Tensor. Got {type(boxes)}")
+
+ if not torch.is_tensor(trans_mat):
+ raise TypeError(f"Tranformation matrix type is not a torch.Tensor. Got {type(trans_mat)}")
+
+ if not isinstance(mode, str):
+ raise TypeError(f"Mode must be a string. Got {type(mode)}")
+
+ if mode not in ("xyxy", "xywh"):
+ raise ValueError(f"Mode must be one of 'xyxy', 'xywh'. Got {mode}")
+
+ boxes = boxes.unsqueeze(0)
+
+ # convert box to format xyxy
+ if mode == "xywh":
+ boxes[..., -2] = boxes[..., 0] + boxes[..., -2] # x + w
+ boxes[..., -1] = boxes[..., 1] + boxes[..., -1] # y + h
+
+ transformed_boxes: torch.Tensor = kornia.transform_points(trans_mat, boxes.view(-1, 2, 2)).view(-1, 4)
+
+ if mode == 'xywh':
+ transformed_boxes[:, 2] = transformed_boxes[:, 2] - transformed_boxes[:, 0]
+ transformed_boxes[:, 3] = transformed_boxes[:, 3] - transformed_boxes[:, 1]
+
+ return transformed_boxes
+
+
def perspective_transform_lafs(trans_01: torch.Tensor,
lafs_1: torch.Tensor) -> torch.Tensor:
r"""Function that applies perspective transformations to a set of local affine frames (LAFs).
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index d5e08d47d..d14fa923d 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -108,6 +108,77 @@ class TestTransformPoints:
assert_allclose(actual, expected)
+class TestTransformBoxes:
+
+ def test_transform_boxes(self, device):
+
+ boxes = torch.Tensor([139.2640, 103.0150, 397.3120, 410.5225]).to(device)
+
+ expected = torch.Tensor([372.7360, 103.0150, 114.6880, 410.5225]).to(device)
+
+ trans_mat = torch.Tensor([[[-1., 0., 512.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]).to(device)
+
+ out = kornia.transform_boxes(trans_mat, boxes)
+ assert_allclose(out, expected)
+
+ def test_transform_boxes_wh(self, device):
+
+ boxes = torch.Tensor([[139.2640, 103.0150, 258.0480, 307.5075],
+ [1.0240, 80.5547, 510.9760, 431.4453],
+ [165.2053, 262.1440, 345.4293, 246.7840],
+ [119.8080, 144.2067, 137.2160, 265.9225]]).to(device)
+
+ expected = torch.Tensor([[372.7360, 103.0150, -258.0480, 307.5075],
+ [510.9760, 80.5547, -510.9760, 431.4453],
+ [346.7947, 262.1440, -345.4293, 246.7840],
+ [392.1920, 144.2067, -137.2160, 265.9225]]).to(device)
+
+ trans_mat = torch.Tensor([[[-1., 0., 512.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]).to(device)
+
+ trans_mat = trans_mat.expand(4, -1, -1)
+
+ out = kornia.transform_boxes(trans_mat, boxes, mode='xywh')
+ assert_allclose(out, expected)
+
+ def test_gradcheck(self, device):
+
+ boxes = torch.Tensor([[139.2640, 103.0150, 258.0480, 307.5075],
+ [1.0240, 80.5547, 510.9760, 431.4453],
+ [165.2053, 262.1440, 345.4293, 246.7840],
+ [119.8080, 144.2067, 137.2160, 265.9225]]).to(device)
+
+ trans_mat = torch.Tensor([[[-1., 0., 512.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]).to(device)
+
+ trans_mat = trans_mat.expand(4, -1, -1)
+ trans_mat = utils.tensor_to_gradcheck_var(trans_mat)
+ boxes = utils.tensor_to_gradcheck_var(boxes)
+
+ assert gradcheck(kornia.transform_boxes, (trans_mat, boxes), raise_exception=True)
+
+ @pytest.mark.skip(reason="turn off all jit for a while")
+ def test_jit(self, device):
+ @torch.jit.script
+ def op_script(transform, boxes):
+ return kornia.transform_boxes(transform, boxes)
+
+ boxes = torch.Tensor([139.2640, 103.0150, 258.0480, 307.5075]).to(device)
+
+ trans_mat = torch.Tensor([[[-1., 0., 512.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]).to(device)
+
+ actual = op_script(trans_mat, boxes)
+ expected = kornia.transform_points(trans_mat, boxes)
+
+ assert_allclose(actual, expected)
+
+
class TestComposeTransforms:
def test_translation_4x4(self, device):