This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #690 from knorth55/rotate
add rotate transforms for image
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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) |
This file contains 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 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,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__) |