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
8829ff0d
Unverified
Commit
8829ff0d
authored
Oct 06, 2019
by
Kai Chen
Committed by
GitHub
Oct 06, 2019
Browse files
Add rgb2gray and gray2rgb (#139)
* add rgb2gray and gray2rgb * update version * minor fix for the docstring
parent
d361ef21
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
67 additions
and
17 deletions
+67
-17
mmcv/image/__init__.py
mmcv/image/__init__.py
+9
-9
mmcv/image/transforms/__init__.py
mmcv/image/transforms/__init__.py
+7
-6
mmcv/image/transforms/colorspace.py
mmcv/image/transforms/colorspace.py
+32
-1
mmcv/version.py
mmcv/version.py
+1
-1
tests/test_image.py
tests/test_image.py
+18
-0
No files found.
mmcv/image/__init__.py
View file @
8829ff0d
from
.io
import
imread
,
imwrite
,
imfrombytes
from
.io
import
imread
,
imwrite
,
imfrombytes
from
.transforms
import
(
solarize
,
posterize
,
bgr2gray
,
gray
2bgr
,
b
gr
2rgb
,
from
.transforms
import
(
solarize
,
posterize
,
bgr2gray
,
rgb2
gray
,
gr
ay2bgr
,
rgb2bgr
,
bgr2hsv
,
hsv2bgr
,
bgr2hls
,
hls2bgr
,
iminvert
,
gray2rgb
,
bgr2rgb
,
rgb2bgr
,
bgr2hsv
,
hsv2bgr
,
bgr2hls
,
imflip
,
imrotate
,
imcrop
,
impad
,
impad_to_multiple
,
hls2bgr
,
iminvert
,
imflip
,
imrotate
,
imcrop
,
impad
,
im
normaliz
e
,
im
de
normalize
,
im
resize
,
imresize_lik
e
,
im
pad_to_multipl
e
,
imnormalize
,
im
denormaliz
e
,
imrescale
)
imresize
,
imresize_like
,
imrescale
)
__all__
=
[
__all__
=
[
'solarize'
,
'posterize'
,
'imread'
,
'imwrite'
,
'imfrombytes'
,
'bgr2gray'
,
'solarize'
,
'posterize'
,
'imread'
,
'imwrite'
,
'imfrombytes'
,
'bgr2gray'
,
'
gray2bgr
'
,
'bgr2rgb'
,
'rgb2bgr'
,
'bgr2hsv'
,
'hsv2bgr'
,
'bgr2hls'
,
'
rgb2gray'
,
'gray2bgr'
,
'gray2rgb
'
,
'bgr2rgb'
,
'rgb2bgr'
,
'bgr2hsv'
,
'hls2bgr'
,
'iminvert'
,
'imflip'
,
'imrotate'
,
'imcrop'
,
'impad'
,
'hsv2bgr'
,
'bgr2hls'
,
'hls2bgr'
,
'iminvert'
,
'imflip'
,
'imrotate'
,
'impad_to_multiple'
,
'imnormalize'
,
'imdenormalize'
,
'imresize'
,
'imcrop'
,
'impad'
,
'impad_to_multiple'
,
'imnormalize'
,
'imdenormalize'
,
'imresize_like'
,
'imrescale'
'imresize'
,
'imresize_like'
,
'imrescale'
]
]
mmcv/image/transforms/__init__.py
View file @
8829ff0d
from
.colorspace
import
(
solarize
,
posterize
,
bgr2gray
,
gray2bgr
,
bgr2rgb
,
from
.colorspace
import
(
solarize
,
posterize
,
bgr2gray
,
rgb2gray
,
gray2bgr
,
rgb2bgr
,
bgr2hsv
,
hsv2bgr
,
bgr2hls
,
hls2bgr
,
iminvert
)
gray2rgb
,
bgr2rgb
,
rgb2bgr
,
bgr2hsv
,
hsv2bgr
,
bgr2hls
,
hls2bgr
,
iminvert
)
from
.geometry
import
imflip
,
imrotate
,
imcrop
,
impad
,
impad_to_multiple
from
.geometry
import
imflip
,
imrotate
,
imcrop
,
impad
,
impad_to_multiple
from
.normalize
import
imnormalize
,
imdenormalize
from
.normalize
import
imnormalize
,
imdenormalize
from
.resize
import
imresize
,
imresize_like
,
imrescale
from
.resize
import
imresize
,
imresize_like
,
imrescale
__all__
=
[
__all__
=
[
'solarize'
,
'posterize'
,
'bgr2gray'
,
'gray
2bgr
'
,
'
b
gr
2rgb'
,
'rgb2bgr
'
,
'solarize'
,
'posterize'
,
'bgr2gray'
,
'
rgb2
gray'
,
'gr
ay2bgr'
,
'gray2rgb
'
,
'bgr2hsv'
,
'hsv2bgr'
,
'bgr2hls'
,
'hls2bgr'
,
'iminvert'
,
'imflip'
,
'bgr2rgb'
,
'rgb2bgr'
,
'bgr2hsv'
,
'hsv2bgr'
,
'bgr2hls'
,
'hls2bgr'
,
'imrotate'
,
'imcrop'
,
'impad'
,
'impad_to_multiple'
,
'imnormalize'
,
'iminvert'
,
'imflip'
,
'imrotate'
,
'imcrop'
,
'impad'
,
'impad_to_multiple'
,
'imdenormalize'
,
'imresize'
,
'imresize_like'
,
'imrescale'
'imnormalize'
,
'imdenormalize'
,
'imresize'
,
'imresize_like'
,
'imrescale'
]
]
mmcv/image/transforms/colorspace.py
View file @
8829ff0d
...
@@ -59,11 +59,28 @@ def bgr2gray(img, keepdim=False):
...
@@ -59,11 +59,28 @@ def bgr2gray(img, keepdim=False):
return
out_img
return
out_img
def
rgb2gray
(
img
,
keepdim
=
False
):
"""Convert a RGB image to grayscale image.
Args:
img (ndarray): The input image.
keepdim (bool): If False (by default), then return the grayscale image
with 2 dims, otherwise 3 dims.
Returns:
ndarray: The converted grayscale image.
"""
out_img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_RGB2GRAY
)
if
keepdim
:
out_img
=
out_img
[...,
None
]
return
out_img
def
gray2bgr
(
img
):
def
gray2bgr
(
img
):
"""Convert a grayscale image to BGR image.
"""Convert a grayscale image to BGR image.
Args:
Args:
img (ndarray
or str
): The input image.
img (ndarray): The input image.
Returns:
Returns:
ndarray: The converted BGR image.
ndarray: The converted BGR image.
...
@@ -73,6 +90,20 @@ def gray2bgr(img):
...
@@ -73,6 +90,20 @@ def gray2bgr(img):
return
out_img
return
out_img
def
gray2rgb
(
img
):
"""Convert a grayscale image to RGB image.
Args:
img (ndarray): The input image.
Returns:
ndarray: The converted BGR image.
"""
img
=
img
[...,
None
]
if
img
.
ndim
==
2
else
img
out_img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_GRAY2RGB
)
return
out_img
def
convert_color_factory
(
src
,
dst
):
def
convert_color_factory
(
src
,
dst
):
code
=
getattr
(
cv2
,
'COLOR_{}2{}'
.
format
(
src
.
upper
(),
dst
.
upper
()))
code
=
getattr
(
cv2
,
'COLOR_{}2{}'
.
format
(
src
.
upper
(),
dst
.
upper
()))
...
...
mmcv/version.py
View file @
8829ff0d
__version__
=
'0.2.1
3
'
__version__
=
'0.2.1
4
'
tests/test_image.py
View file @
8829ff0d
...
@@ -66,6 +66,17 @@ class TestImage(object):
...
@@ -66,6 +66,17 @@ class TestImage(object):
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_rgb2gray
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
rgb2gray
(
in_img
)
computed_gray
=
(
in_img
[:,
:,
0
]
*
0.299
+
in_img
[:,
:,
1
]
*
0.587
+
in_img
[:,
:,
2
]
*
0.114
)
assert_array_almost_equal
(
out_img
,
computed_gray
,
decimal
=
4
)
out_img_3d
=
mmcv
.
rgb2gray
(
in_img
,
True
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_gray2bgr
(
self
):
def
test_gray2bgr
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2bgr
(
in_img
)
out_img
=
mmcv
.
gray2bgr
(
in_img
)
...
@@ -73,6 +84,13 @@ class TestImage(object):
...
@@ -73,6 +84,13 @@ class TestImage(object):
for
i
in
range
(
3
):
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_gray2rgb
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2rgb
(
in_img
)
assert
out_img
.
shape
==
(
10
,
10
,
3
)
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_bgr2rgb
(
self
):
def
test_bgr2rgb
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2rgb
(
in_img
)
out_img
=
mmcv
.
bgr2rgb
(
in_img
)
...
...
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