π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
transform boxes
dvd42a62932fc7ae3
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex 8b4898044..33edf4990 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -217,7 +217,7 @@ def transform_points(trans_01: torch.Tensor,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)+ be a tensor of the shape (N, 4) or a batch of boxes (B, N, 4) and trans_mat must be a (3, 3)transformation matrixArgs:@@ -245,20 +245,31 @@ def transform_boxes(trans_mat: torch.Tensor, boxes: torch.Tensor, mode: str = "xif mode not in ("xyxy", "xywh"):raise ValueError(f"Mode must be one of 'xyxy', 'xywh'. Got {mode}")+ device: torch.device = trans_mat.deviceboxes = boxes.unsqueeze(0)- # convert box to format xyxy+ # convert boxes to format xyxyif mode == "xywh":boxes[..., -2] = boxes[..., 0] + boxes[..., -2] # x + wboxes[..., -1] = boxes[..., 1] + boxes[..., -1] # y + h- transformed_boxes: torch.Tensor = kornia.transform_points(trans_mat, boxes.view(-1, 2, 2)).view(-1, 4)+ # adjust transfomation matrix shape+ to_expand: int = boxes.view(-1, 2, 2).shape[0]+ b_size: int = trans_mat.shape[0]+ trans_mat = trans_mat.expand(to_expand // b_size, -1, -1, -1).reshape(-1, 3, 3) # B * N x 3 x 3++ # put transformations in the correct order so as to all boxes belonging to the same batch+ # element are transformed in the same way+ perm: torch.Tensor = torch.cat([torch.arange(i, to_expand, b_size) for i in range(b_size)]).to(device)+ trans_mat = trans_mat[perm, ...]++ transformed_boxes: torch.Tensor = kornia.transform_points(trans_mat, boxes.view(-1, 2, 2)).view_as(boxes)if mode == 'xywh':- transformed_boxes[:, 2] = transformed_boxes[:, 2] - transformed_boxes[:, 0]- transformed_boxes[:, 3] = transformed_boxes[:, 3] - transformed_boxes[:, 1]+ transformed_boxes[..., 2] = transformed_boxes[..., 2] - transformed_boxes[..., 0]+ transformed_boxes[..., 3] = transformed_boxes[..., 3] - transformed_boxes[..., 1]- return transformed_boxes+ return transformed_boxes.squeeze()def perspective_transform_lafs(trans_01: torch.Tensor,diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.pyindex d14fa923d..8b01b11a9 100644--- a/test/geometry/test_linalg.py+++ b/test/geometry/test_linalg.py@@ -123,6 +123,36 @@ class TestTransformBoxes:out = kornia.transform_boxes(trans_mat, boxes)assert_allclose(out, expected)+ def test_transform_multiple_boxes(self, device):++ boxes = torch.Tensor([[139.2640, 103.0150, 397.3120, 410.5225],+ [1.0240, 80.5547, 512.0000, 512.0000],+ [165.2053, 262.1440, 510.6347, 508.9280],+ [119.8080, 144.2067, 257.0240, 410.1292]]).to(device)++ boxes = boxes.repeat(2, 1, 1) # 2, 4, 4 two images 4 boxes each++ expected = torch.Tensor([[[372.7360, 103.0150, 114.6880, 410.5225],+ [510.9760, 80.5547, 0.0000, 512.0000],+ [346.7947, 262.1440, 1.3653, 508.9280],+ [392.1920, 144.2067, 254.9760, 410.1292]],++ [[139.2640, 103.0150, 397.3120, 410.5225],+ [1.0240, 80.5547, 512.0000, 512.0000],+ [165.2053, 262.1440, 510.6347, 508.9280],+ [119.8080, 144.2067, 257.0240, 410.1292]]]).to(device)++ trans_mat = torch.Tensor([[[-1., 0., 512.],+ [0., 1., 0.],+ [0., 0., 1.]],++ [[1., 0., 0.],+ [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],