Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
83d9a9c8
Unverified
Commit
83d9a9c8
authored
Aug 22, 2020
by
Jerry Jiarui XU
Committed by
GitHub
Aug 22, 2020
Browse files
[Feature] Add diagonal flip (#515)
parent
eb0414f4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
6 deletions
+61
-6
mmcv/image/geometric.py
mmcv/image/geometric.py
+12
-6
tests/test_image/test_geometric.py
tests/test_image/test_geometric.py
+49
-0
No files found.
mmcv/image/geometric.py
View file @
83d9a9c8
...
...
@@ -187,16 +187,19 @@ def imflip(img, direction='horizontal'):
Args:
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:
ndarray: The flipped image.
"""
assert
direction
in
[
'horizontal'
,
'vertical'
]
assert
direction
in
[
'horizontal'
,
'vertical'
,
'diagonal'
]
if
direction
==
'horizontal'
:
return
np
.
flip
(
img
,
axis
=
1
)
el
se
:
el
if
direction
==
'vertical'
:
return
np
.
flip
(
img
,
axis
=
0
)
else
:
return
np
.
flip
(
img
,
axis
=
(
0
,
1
))
def
imflip_
(
img
,
direction
=
'horizontal'
):
...
...
@@ -204,16 +207,19 @@ def imflip_(img, direction='horizontal'):
Args:
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:
ndarray: The flipped image (inplace).
"""
assert
direction
in
[
'horizontal'
,
'vertical'
]
assert
direction
in
[
'horizontal'
,
'vertical'
,
'diagonal'
]
if
direction
==
'horizontal'
:
return
cv2
.
flip
(
img
,
1
,
img
)
el
se
:
el
if
direction
==
'vertical'
:
return
cv2
.
flip
(
img
,
0
,
img
)
else
:
return
cv2
.
flip
(
img
,
-
1
,
img
)
def
imrotate
(
img
,
...
...
tests/test_image/test_geometric.py
View file @
83d9a9c8
...
...
@@ -108,6 +108,10 @@ class TestGeometric:
mmcv
.
imrescale
(
self
.
img
,
[
100
,
100
])
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)
img
=
np
.
random
.
rand
(
80
,
60
,
3
)
h
,
w
,
c
=
img
.
shape
...
...
@@ -117,6 +121,7 @@ class TestGeometric:
for
j
in
range
(
w
):
for
k
in
range
(
c
):
assert
flipped_img
[
i
,
j
,
k
]
==
img
[
i
,
w
-
1
-
j
,
k
]
# test vertical flip (color image)
flipped_img
=
mmcv
.
imflip
(
img
,
direction
=
'vertical'
)
assert
flipped_img
.
shape
==
img
.
shape
...
...
@@ -124,6 +129,15 @@ class TestGeometric:
for
j
in
range
(
w
):
for
k
in
range
(
c
):
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)
img
=
np
.
random
.
rand
(
80
,
60
)
h
,
w
=
img
.
shape
...
...
@@ -132,6 +146,7 @@ class TestGeometric:
for
i
in
range
(
h
):
for
j
in
range
(
w
):
assert
flipped_img
[
i
,
j
]
==
img
[
i
,
w
-
1
-
j
]
# test vertical flip (grayscale image)
flipped_img
=
mmcv
.
imflip
(
img
,
direction
=
'vertical'
)
assert
flipped_img
.
shape
==
img
.
shape
...
...
@@ -139,7 +154,18 @@ class TestGeometric:
for
j
in
range
(
w
):
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
):
# 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)
img
=
np
.
random
.
rand
(
80
,
60
,
3
)
h
,
w
,
c
=
img
.
shape
...
...
@@ -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_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)
img
=
np
.
random
.
rand
(
80
,
60
)
h
,
w
=
img
.
shape
...
...
@@ -190,6 +228,17 @@ class TestGeometric:
assert
flipped_img
[
i
,
j
]
==
img
[
h
-
1
-
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
):
# yapf: disable
bboxes
=
np
.
array
([[
100
,
100
,
199
,
199
],
# center
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment