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

Commit

Permalink
Merge pull request #690 from knorth55/rotate
Browse files Browse the repository at this point in the history
add rotate transforms for image
  • Loading branch information
yuyu2172 authored Sep 19, 2018
2 parents f973856 + 8916b04 commit 1fd0010
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions chainercv/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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():
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__)

0 comments on commit 1fd0010

Please sign in to comment.