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

add rotate transforms for image #690

Merged
merged 6 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
Expand Up @@ -12,6 +12,7 @@
from chainercv.transforms.image.random_sized_crop import random_sized_crop # NOQA
from chainercv.transforms.image.resize import resize # NOQA
from chainercv.transforms.image.resize_contain import resize_contain # NOQA
from chainercv.transforms.image.rotate import rotate # NOQA
from chainercv.transforms.image.scale import scale # NOQA
from chainercv.transforms.image.ten_crop import ten_crop # NOQA
from chainercv.transforms.point.flip_point import flip_point # NOQA
Expand Down
41 changes: 41 additions & 0 deletions chainercv/transforms/image/rotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import warnings


try:
import scipy.ndimage
_available = True
except ImportError:
_available = False


def _check_available():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked scipy availablity at the top of rotate function.

if not _available:
warnings.warn(
'SciPy is not installed in your environment,'
'so rotate cannot be loaded.'
'Please install SciPy to load dataset.\n\n'
'$ pip install scipy')


def rotate(img, angle, expand=True):
"""Rotate images by degrees.

Args:
img (~numpy.ndarray): An arrays that get rotated. This is in
CHW format.
angle (float): Counter clock-wise rotation angle (degree) in
[-180, 180].
expand (bool): The output shaped is adapted or not.
If :obj:`True`, the input image is contained complete in
the output.

Returns:
~numpy.ndarray:
returns an array :obj:`out_img` that is the result of rotation.

"""

_check_available()

assert 180 >= angle >= -180
return scipy.ndimage.rotate(img, angle, axes=(2, 1), reshape=expand)
4 changes: 4 additions & 0 deletions docs/source/reference/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ resize_contain
~~~~~~~~~~~~~~
.. autofunction:: resize_contain

rotate
~~~~~~
.. autofunction:: rotate

scale
~~~~~
.. autofunction:: scale
Expand Down
32 changes: 32 additions & 0 deletions tests/transforms_tests/image_tests/test_rotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import random
import unittest

import numpy as np

from chainer import testing
from chainercv.transforms import flip
from chainercv.transforms import rotate

try:
import scipy # NOQA
_available = True
except ImportError:
_available = False


@unittest.skipUnless(_available, 'SciPy is not installed')
class TestRotate(unittest.TestCase):

def test_rotate(self):
img = np.random.uniform(size=(3, 32, 24))
angle = random.uniform(0, 180)

out = rotate(img, angle)
expected = flip(img, x_flip=True)
expected = rotate(expected, -1 * angle)
expected = flip(expected, x_flip=True)

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


testing.run_module(__name__, __file__)