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 304
add rotate transforms for image #690
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b49fa0c
add rotate for image rotation
knorth55 d5d8e16
add test_rotate.py
knorth55 822cefb
add rotate doc in reference
knorth55 4db8ca9
skip when scipy is not installed
knorth55 0904ec7
fix typo in docstring in rotate.py
knorth55 8916b04
check scipy available in rotate.py
knorth55 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 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__) |
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.