-
Notifications
You must be signed in to change notification settings - Fork 634
[Feature] Add RotatedBoxes and QuadriBoxes data structure in mmrotate. #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
93efe5b
Initialize RotatedBoxes and QuadriBoxes
jbwang1997 e32f761
Move all box modes into Structures, Add docstring
jbwang1997 c41ec42
Update docstring
jbwang1997 cdad714
Upload UT
jbwang1997 320040e
Fix comments
jbwang1997 48b1fad
Update inplace operations
jbwang1997 e249f02
Update
jbwang1997 64dea01
Update
jbwang1997 d7b95cc
Update
jbwang1997 848fbd2
Update
jbwang1997 82edffd
Updata rotate and project UT
jbwang1997 56643f6
Update
jbwang1997 406c6e6
Update
jbwang1997 a27e1d5
Update
jbwang1997 d5ae52d
Update
jbwang1997 326873d
update
jbwang1997 2874ef6
Update from_instance_masks
jbwang1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| from .box_converters import (hbox2qbox, hbox2rbox, qbox2hbox, qbox2rbox, | ||
| rbox2hbox, rbox2qbox) | ||
| from .quadri_boxes import QuadriBoxes | ||
| from .rotated_boxes import RotatedBoxes | ||
|
|
||
| __all__ = [ | ||
| 'QuadriBoxes', 'RotatedBoxes', 'hbox2rbox', 'hbox2qbox', 'rbox2hbox', | ||
| 'rbox2qbox', 'qbox2hbox', 'qbox2rbox' | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| import cv2 | ||
| import numpy as np | ||
| import torch | ||
| from mmdet.structures.bbox import HorizontalBoxes, register_box_converter | ||
| from torch import Tensor | ||
|
|
||
| from .quadri_boxes import QuadriBoxes | ||
| from .rotated_boxes import RotatedBoxes | ||
|
|
||
|
|
||
| @register_box_converter(HorizontalBoxes, RotatedBoxes) | ||
| def hbox2rbox(boxes: Tensor) -> Tensor: | ||
| """Convert horizontal boxes to rotated boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): horizontal box tensor with shape of (..., 4). | ||
|
|
||
| Returns: | ||
| Tensor: Rotated box tensor with shape of (..., 5). | ||
| """ | ||
| wh = boxes[..., 2:] - boxes[..., :2] | ||
| ctrs = (boxes[..., 2:] + boxes[..., :2]) / 2 | ||
| theta = boxes.new_zeros((*boxes.shape[:-1], 1)) | ||
| return torch.cat([ctrs, wh, theta], dim=-1) | ||
|
|
||
|
|
||
| @register_box_converter(HorizontalBoxes, QuadriBoxes) | ||
| def hbox2qbox(boxes: Tensor) -> Tensor: | ||
| """Convert horizontal boxes to quadrilateral boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): horizontal box tensor with shape of (..., 4). | ||
|
|
||
| Returns: | ||
| Tensor: Quadrilateral box tensor with shape of (..., 8). | ||
| """ | ||
| x1, y1, x2, y2 = torch.split(boxes, 1, dim=-1) | ||
| return torch.cat([x1, y1, x2, y1, x2, y2, x1, y2], dim=-1) | ||
|
|
||
|
|
||
| @register_box_converter(RotatedBoxes, HorizontalBoxes) | ||
| def rbox2hbox(boxes: Tensor) -> Tensor: | ||
| """Convert rotated boxes to horizontal boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): Rotated box tensor with shape of (..., 5). | ||
|
|
||
| Returns: | ||
| Tensor: Horizontal box tensor with shape of (..., 4). | ||
| """ | ||
| ctrs, w, h, theta = torch.split(boxes, (2, 1, 1, 1), dim=-1) | ||
| cos_value, sin_value = torch.cos(theta), torch.sin(theta) | ||
| x_bias = torch.abs(w / 2 * cos_value) + torch.abs(h / 2 * sin_value) | ||
| y_bias = torch.abs(w / 2 * sin_value) + torch.abs(h / 2 * cos_value) | ||
| bias = torch.cat([x_bias, y_bias], dim=-1) | ||
| return torch.cat([ctrs - bias, ctrs + bias], dim=-1) | ||
|
|
||
|
|
||
| @register_box_converter(RotatedBoxes, QuadriBoxes) | ||
| def rbox2qbox(boxes: Tensor) -> Tensor: | ||
| """Convert rotated boxes to quadrilateral boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): Rotated box tensor with shape of (..., 5). | ||
|
|
||
| Returns: | ||
| Tensor: Quadrilateral box tensor with shape of (..., 8). | ||
| """ | ||
| ctr, w, h, theta = torch.split(boxes, (2, 1, 1, 1), dim=-1) | ||
| cos_value, sin_value = torch.cos(theta), torch.sin(theta) | ||
| vec1 = torch.cat([w / 2 * cos_value, w / 2 * sin_value], dim=-1) | ||
| vec2 = torch.cat([-h / 2 * sin_value, h / 2 * cos_value], dim=-1) | ||
| pt1 = ctr + vec1 + vec2 | ||
| pt2 = ctr + vec1 - vec2 | ||
| pt3 = ctr - vec1 - vec2 | ||
| pt4 = ctr - vec1 + vec2 | ||
| return torch.cat([pt1, pt2, pt3, pt4], dim=-1) | ||
|
|
||
|
|
||
| @register_box_converter(QuadriBoxes, HorizontalBoxes) | ||
| def qbox2hbox(boxes: Tensor) -> Tensor: | ||
| """Convert quadrilateral boxes to horizontal boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): Quadrilateral box tensor with shape of (..., 8). | ||
|
|
||
| Returns: | ||
| Tensor: Horizontal box tensor with shape of (..., 4). | ||
| """ | ||
| boxes = boxes.view(*boxes.shape[:-1], 4, 2) | ||
| x1y1, _ = boxes.min(dim=-2) | ||
| x2y2, _ = boxes.max(dim=-2) | ||
| return torch.cat([x1y1, x2y2], dim=-1) | ||
|
|
||
|
|
||
| @register_box_converter(QuadriBoxes, RotatedBoxes) | ||
| def qbox2rbox(boxes: Tensor) -> Tensor: | ||
| """Convert quadrilateral boxes to rotated boxes. | ||
|
|
||
| Args: | ||
| boxes (Tensor): Quadrilateral box tensor with shape of (..., 8). | ||
|
|
||
| Returns: | ||
| Tensor: Rotated box tensor with shape of (..., 5). | ||
| """ | ||
| # TODO support tensor-based minAreaRect later | ||
| original_shape = boxes.shape[:-1] | ||
| points = boxes.cpu().numpy().reshape(-1, 4, 2) | ||
| rboxes = [] | ||
| for pts in points: | ||
| (x, y), (w, h), angle = cv2.minAreaRect(pts) | ||
| rboxes.append([x, y, w, h, angle / 180 * np.pi]) | ||
| rboxes = boxes.new_tensor(rboxes) | ||
| return rboxes.view(*original_shape, 5) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.