"docs/git@developer.sourcefind.cn:OpenDAS/vision.git" did not exist on "e1c6a757183b8139ae50bdaa6af1c82e43b27589"
Unverified Commit 5ee538d4 authored by Joanna's avatar Joanna Committed by GitHub
Browse files

add inplace imflip (#191)



* add implace flip

* add tests
Signed-off-by: default avatarlixuanyi <lixuanyi@sensetime.com>

* modify tests
Signed-off-by: default avatarlixuanyi <lixuanyi@sensetime.com>

* add inplace tests
Signed-off-by: default avatarlixuanyi <lixuanyi@sensetime.com>

* add comments
Signed-off-by: default avatarlixuanyi <lixuanyi@sensetime.com>

* fix
Signed-off-by: default avatarlixuanyi <lixuanyi@sensetime.com>
parent 4eca2c59
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
from .io import imfrombytes, imread, imwrite, supported_backends, use_backend from .io import imfrombytes, imread, imwrite, supported_backends, use_backend
from .transforms import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr, from .transforms import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr,
gray2rgb, hls2bgr, hsv2bgr, imcrop, imdenormalize, gray2rgb, hls2bgr, hsv2bgr, imcrop, imdenormalize,
imflip, iminvert, imnormalize, impad, imflip, imflip_, iminvert, imnormalize, impad,
impad_to_multiple, imrescale, imresize, imresize_like, impad_to_multiple, imrescale, imresize, imresize_like,
imrotate, posterize, rgb2bgr, rgb2gray, solarize) imrotate, posterize, rgb2bgr, rgb2gray, solarize)
__all__ = [ __all__ = [
'solarize', 'posterize', 'imread', 'imwrite', 'imfrombytes', 'bgr2gray', 'solarize', 'posterize', 'imread', 'imwrite', 'imfrombytes', 'bgr2gray',
'rgb2gray', 'gray2bgr', 'gray2rgb', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'rgb2gray', 'gray2bgr', 'gray2rgb', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv',
'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip', 'imrotate', 'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip', 'imflip_',
'imcrop', 'impad', 'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple', 'imnormalize',
'imresize', 'imresize_like', 'imrescale', 'use_backend', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale', 'use_backend',
'supported_backends' 'supported_backends'
] ]
...@@ -2,13 +2,15 @@ ...@@ -2,13 +2,15 @@
from .colorspace import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr, from .colorspace import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr,
gray2rgb, hls2bgr, hsv2bgr, iminvert, posterize, gray2rgb, hls2bgr, hsv2bgr, iminvert, posterize,
rgb2bgr, rgb2gray, solarize) rgb2bgr, rgb2gray, solarize)
from .geometry import imcrop, imflip, impad, impad_to_multiple, imrotate from .geometry import (imcrop, imflip, imflip_, impad, impad_to_multiple,
imrotate)
from .normalize import imdenormalize, imnormalize from .normalize import imdenormalize, imnormalize
from .resize import imrescale, imresize, imresize_like from .resize import imrescale, imresize, imresize_like
__all__ = [ __all__ = [
'solarize', 'posterize', 'bgr2gray', 'rgb2gray', 'gray2bgr', 'gray2rgb', 'solarize', 'posterize', 'bgr2gray', 'rgb2gray', 'gray2bgr', 'gray2rgb',
'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'bgr2hls', 'hls2bgr', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'bgr2hls', 'hls2bgr',
'iminvert', 'imflip', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple', 'iminvert', 'imflip', 'imflip_', 'imrotate', 'imcrop', 'impad',
'imnormalize', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale' 'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imresize',
'imresize_like', 'imrescale'
] ]
...@@ -22,6 +22,22 @@ def imflip(img, direction='horizontal'): ...@@ -22,6 +22,22 @@ def imflip(img, direction='horizontal'):
return np.flip(img, axis=0) return np.flip(img, axis=0)
def imflip_(img, direction='horizontal'):
"""Inplace flip an image horizontally or vertically.
Args:
img (ndarray): Image to be flipped.
direction (str): The flip direction, either "horizontal" or "vertical".
Returns:
ndarray: The flipped image(inplace).
"""
assert direction in ['horizontal', 'vertical']
if direction == 'horizontal':
return cv2.flip(img, 1, img)
else:
return cv2.flip(img, 0, img)
def imrotate(img, def imrotate(img,
angle, angle,
center=None, center=None,
......
...@@ -313,6 +313,57 @@ class TestImage(object): ...@@ -313,6 +313,57 @@ class TestImage(object):
for j in range(w): for j in range(w):
assert flipped_img[i, j] == img[h - 1 - i, j] assert flipped_img[i, j] == img[h - 1 - i, j]
def test_imflip_(self):
# test horizontal flip (color image)
img = np.random.rand(80, 60, 3)
h, w, c = img.shape
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip)
assert flipped_img.shape == img.shape
assert flipped_img.shape == img_for_flip.shape
assert id(flipped_img) == id(img_for_flip)
for i in range(h):
for j in range(w):
for k in range(c):
assert flipped_img[i, j, k] == img[i, w - 1 - j, k]
assert flipped_img[i, j, k] == img_for_flip[i, j, k]
# test vertical flip (color image)
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip, direction='vertical')
assert flipped_img.shape == img.shape
assert flipped_img.shape == img_for_flip.shape
assert id(flipped_img) == id(img_for_flip)
for i in range(h):
for j in range(w):
for k in range(c):
assert flipped_img[i, j, k] == img[h - 1 - i, j, k]
assert flipped_img[i, j, k] == img_for_flip[i, j, k]
# test horizontal flip (grayscale image)
img = np.random.rand(80, 60)
h, w = img.shape
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip)
assert flipped_img.shape == img.shape
assert flipped_img.shape == img_for_flip.shape
assert id(flipped_img) == id(img_for_flip)
for i in range(h):
for j in range(w):
assert flipped_img[i, j] == img[i, w - 1 - j]
assert flipped_img[i, j] == img_for_flip[i, j]
# test vertical flip (grayscale image)
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip, direction='vertical')
assert flipped_img.shape == img.shape
assert flipped_img.shape == img_for_flip.shape
assert id(flipped_img) == id(img_for_flip)
for i in range(h):
for j in range(w):
assert flipped_img[i, j] == img[h - 1 - i, j]
assert flipped_img[i, j] == img_for_flip[i, j]
def test_imcrop(self): def test_imcrop(self):
# yapf: disable # yapf: disable
bboxes = np.array([[100, 100, 199, 199], # center bboxes = np.array([[100, 100, 199, 199], # center
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment