Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

add rotate_bbox #692

Merged
merged 7 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chainercv/transforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from chainercv.transforms.bbox.crop_bbox import crop_bbox # NOQA
from chainercv.transforms.bbox.flip_bbox import flip_bbox # NOQA
from chainercv.transforms.bbox.resize_bbox import resize_bbox # NOQA
from chainercv.transforms.bbox.rotate_bbox import rotate_bbox # NOQA
from chainercv.transforms.bbox.translate_bbox import translate_bbox # NOQA
from chainercv.transforms.image.center_crop import center_crop # NOQA
from chainercv.transforms.image.flip import flip # NOQA
Expand Down
43 changes: 43 additions & 0 deletions chainercv/transforms/bbox/rotate_bbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np


def rotate_bbox(bbox, angle, size):
"""Rotate bounding boxes by degrees.

Args:
bbox (~numpy.ndarray): An array whose shape is :math:`(R, 4)`.
:math:`R` is the number of bounding boxes.
angle (float): Counter clock-wise rotation angle (degree) in
[-180, 180].
image is rotated by 90 degrees.
size (tuple): A tuple of length 2. The height and the width
of the image.

Returns:
~numpy.ndarray:
Bounding boxes rescaled according to the given :obj:`k`.

"""
if angle % 90 != 0:
raise ValueError(
'angle in [180, 90, 0, -90, -180] is only supported: {}'
.format(angle))
return bbox
H, W = size
if angle == 0:
return bbox

if angle == 90:
rotated_bbox = np.concatenate(
(W - bbox[:, 3:4], bbox[:, 0:1],
W - bbox[:, 1:2], bbox[:, 2:3]), axis=1)
elif angle in [180, -180]:
rotated_bbox = np.concatenate(
(H - bbox[:, 2:3], W - bbox[:, 3:4],
H - bbox[:, 0:1], W - bbox[:, 1:2]), axis=1)
elif angle == -90:
rotated_bbox = np.concatenate(
(bbox[:, 1:2], H - bbox[:, 2:3],
bbox[:, 3:4], H - bbox[:, 0:1]), axis=1)
rotated_bbox = rotated_bbox.astype(bbox.dtype)
return rotated_bbox
4 changes: 4 additions & 0 deletions docs/source/reference/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ resize_bbox
~~~~~~~~~~~
.. autofunction:: resize_bbox

rotate_bbox
~~~~~~~~~~~
.. autofunction:: rotate_bbox

translate_bbox
~~~~~~~~~~~~~~
.. autofunction:: translate_bbox
Expand Down
29 changes: 29 additions & 0 deletions tests/transforms_tests/bbox_tests/test_rotate_bbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest

import numpy as np

from chainer import testing
from chainercv.transforms import rotate_bbox
from chainercv.utils import generate_random_bbox


@testing.parameterize(*testing.product({
'angle': [180, 90, 0, -90, -180]
}))
class TestRotateBbox(unittest.TestCase):

def test_rotate_bbox(self):
size = (32, 24)
bbox = generate_random_bbox(10, size, 0, 24)

out = rotate_bbox(bbox, self.angle, size)
if self.angle % 180 != 0:
rotate_size = size[::-1]
else:
rotate_size = size
out = rotate_bbox(out, -1 * self.angle, rotate_size)

np.testing.assert_almost_equal(out, bbox, decimal=6)


testing.run_module(__name__, __file__)