"router/vscode:/vscode.git/clone" did not exist on "709d8936f68002c2244e245607c6b88d658ebe6f"
Unverified Commit 83d9a9c8 authored by Jerry Jiarui XU's avatar Jerry Jiarui XU Committed by GitHub
Browse files

[Feature] Add diagonal flip (#515)

parent eb0414f4
...@@ -187,16 +187,19 @@ def imflip(img, direction='horizontal'): ...@@ -187,16 +187,19 @@ def imflip(img, direction='horizontal'):
Args: Args:
img (ndarray): Image to be flipped. img (ndarray): Image to be flipped.
direction (str): The flip direction, either "horizontal" or "vertical". direction (str): The flip direction, either "horizontal" or
"vertical" or "diagonal".
Returns: Returns:
ndarray: The flipped image. ndarray: The flipped image.
""" """
assert direction in ['horizontal', 'vertical'] assert direction in ['horizontal', 'vertical', 'diagonal']
if direction == 'horizontal': if direction == 'horizontal':
return np.flip(img, axis=1) return np.flip(img, axis=1)
else: elif direction == 'vertical':
return np.flip(img, axis=0) return np.flip(img, axis=0)
else:
return np.flip(img, axis=(0, 1))
def imflip_(img, direction='horizontal'): def imflip_(img, direction='horizontal'):
...@@ -204,16 +207,19 @@ def imflip_(img, direction='horizontal'): ...@@ -204,16 +207,19 @@ def imflip_(img, direction='horizontal'):
Args: Args:
img (ndarray): Image to be flipped. img (ndarray): Image to be flipped.
direction (str): The flip direction, either "horizontal" or "vertical". direction (str): The flip direction, either "horizontal" or
"vertical" or "diagonal".
Returns: Returns:
ndarray: The flipped image (inplace). ndarray: The flipped image (inplace).
""" """
assert direction in ['horizontal', 'vertical'] assert direction in ['horizontal', 'vertical', 'diagonal']
if direction == 'horizontal': if direction == 'horizontal':
return cv2.flip(img, 1, img) return cv2.flip(img, 1, img)
else: elif direction == 'vertical':
return cv2.flip(img, 0, img) return cv2.flip(img, 0, img)
else:
return cv2.flip(img, -1, img)
def imrotate(img, def imrotate(img,
......
...@@ -108,6 +108,10 @@ class TestGeometric: ...@@ -108,6 +108,10 @@ class TestGeometric:
mmcv.imrescale(self.img, [100, 100]) mmcv.imrescale(self.img, [100, 100])
def test_imflip(self): def test_imflip(self):
# direction must be "horizontal" or "vertical" or "diagonal"
with pytest.raises(AssertionError):
mmcv.imflip(np.random.rand(80, 60, 3), direction='random')
# test horizontal flip (color image) # test horizontal flip (color image)
img = np.random.rand(80, 60, 3) img = np.random.rand(80, 60, 3)
h, w, c = img.shape h, w, c = img.shape
...@@ -117,6 +121,7 @@ class TestGeometric: ...@@ -117,6 +121,7 @@ class TestGeometric:
for j in range(w): for j in range(w):
for k in range(c): for k in range(c):
assert flipped_img[i, j, k] == img[i, w - 1 - j, k] assert flipped_img[i, j, k] == img[i, w - 1 - j, k]
# test vertical flip (color image) # test vertical flip (color image)
flipped_img = mmcv.imflip(img, direction='vertical') flipped_img = mmcv.imflip(img, direction='vertical')
assert flipped_img.shape == img.shape assert flipped_img.shape == img.shape
...@@ -124,6 +129,15 @@ class TestGeometric: ...@@ -124,6 +129,15 @@ class TestGeometric:
for j in range(w): for j in range(w):
for k in range(c): for k in range(c):
assert flipped_img[i, j, k] == img[h - 1 - i, j, k] assert flipped_img[i, j, k] == img[h - 1 - i, j, k]
# test diagonal flip (color image)
flipped_img = mmcv.imflip(img, direction='diagonal')
assert flipped_img.shape == img.shape
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, w - 1 - j, k]
# test horizontal flip (grayscale image) # test horizontal flip (grayscale image)
img = np.random.rand(80, 60) img = np.random.rand(80, 60)
h, w = img.shape h, w = img.shape
...@@ -132,6 +146,7 @@ class TestGeometric: ...@@ -132,6 +146,7 @@ class TestGeometric:
for i in range(h): for i in range(h):
for j in range(w): for j in range(w):
assert flipped_img[i, j] == img[i, w - 1 - j] assert flipped_img[i, j] == img[i, w - 1 - j]
# test vertical flip (grayscale image) # test vertical flip (grayscale image)
flipped_img = mmcv.imflip(img, direction='vertical') flipped_img = mmcv.imflip(img, direction='vertical')
assert flipped_img.shape == img.shape assert flipped_img.shape == img.shape
...@@ -139,7 +154,18 @@ class TestGeometric: ...@@ -139,7 +154,18 @@ class TestGeometric:
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]
# test diagonal flip (grayscale image)
flipped_img = mmcv.imflip(img, direction='diagonal')
assert flipped_img.shape == img.shape
for i in range(h):
for j in range(w):
assert flipped_img[i, j] == img[h - 1 - i, w - 1 - j]
def test_imflip_(self): def test_imflip_(self):
# direction must be "horizontal" or "vertical" or "diagonal"
with pytest.raises(AssertionError):
mmcv.imflip_(np.random.rand(80, 60, 3), direction='random')
# test horizontal flip (color image) # test horizontal flip (color image)
img = np.random.rand(80, 60, 3) img = np.random.rand(80, 60, 3)
h, w, c = img.shape h, w, c = img.shape
...@@ -166,6 +192,18 @@ class TestGeometric: ...@@ -166,6 +192,18 @@ class TestGeometric:
assert flipped_img[i, j, k] == img[h - 1 - i, j, k] assert flipped_img[i, j, k] == img[h - 1 - i, j, k]
assert flipped_img[i, j, k] == img_for_flip[i, j, k] assert flipped_img[i, j, k] == img_for_flip[i, j, k]
# test diagonal flip (color image)
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip, direction='diagonal')
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, w - 1 - j, k]
assert flipped_img[i, j, k] == img_for_flip[i, j, k]
# test horizontal flip (grayscale image) # test horizontal flip (grayscale image)
img = np.random.rand(80, 60) img = np.random.rand(80, 60)
h, w = img.shape h, w = img.shape
...@@ -190,6 +228,17 @@ class TestGeometric: ...@@ -190,6 +228,17 @@ class TestGeometric:
assert flipped_img[i, j] == img[h - 1 - i, j] assert flipped_img[i, j] == img[h - 1 - i, j]
assert flipped_img[i, j] == img_for_flip[i, j] assert flipped_img[i, j] == img_for_flip[i, j]
# test diagonal flip (grayscale image)
img_for_flip = img.copy()
flipped_img = mmcv.imflip_(img_for_flip, direction='diagonal')
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, w - 1 - 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