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 @@
from .io import imfrombytes, imread, imwrite, supported_backends, use_backend
from .transforms import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr,
gray2rgb, hls2bgr, hsv2bgr, imcrop, imdenormalize,
imflip, iminvert, imnormalize, impad,
imflip, imflip_, iminvert, imnormalize, impad,
impad_to_multiple, imrescale, imresize, imresize_like,
imrotate, posterize, rgb2bgr, rgb2gray, solarize)
__all__ = [
'solarize', 'posterize', 'imread', 'imwrite', 'imfrombytes', 'bgr2gray',
'rgb2gray', 'gray2bgr', 'gray2rgb', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv',
'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip', 'imrotate',
'imcrop', 'impad', 'impad_to_multiple', 'imnormalize', 'imdenormalize',
'imresize', 'imresize_like', 'imrescale', 'use_backend',
'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip', 'imflip_',
'imrotate', 'imcrop', 'impad', 'impad_to_multiple', 'imnormalize',
'imdenormalize', 'imresize', 'imresize_like', 'imrescale', 'use_backend',
'supported_backends'
]
......@@ -2,13 +2,15 @@
from .colorspace import (bgr2gray, bgr2hls, bgr2hsv, bgr2rgb, gray2bgr,
gray2rgb, hls2bgr, hsv2bgr, iminvert, posterize,
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 .resize import imrescale, imresize, imresize_like
__all__ = [
'solarize', 'posterize', 'bgr2gray', 'rgb2gray', 'gray2bgr', 'gray2rgb',
'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'bgr2hls', 'hls2bgr',
'iminvert', 'imflip', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple',
'imnormalize', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale'
'iminvert', 'imflip', 'imflip_', 'imrotate', 'imcrop', 'impad',
'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imresize',
'imresize_like', 'imrescale'
]
......@@ -22,6 +22,22 @@ def imflip(img, direction='horizontal'):
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,
angle,
center=None,
......
......@@ -313,6 +313,57 @@ class TestImage(object):
for j in range(w):
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):
# yapf: disable
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