Unverified Commit 8829ff0d authored by Kai Chen's avatar Kai Chen Committed by GitHub
Browse files

Add rgb2gray and gray2rgb (#139)

* add rgb2gray and gray2rgb

* update version

* minor fix for the docstring
parent d361ef21
from .io import imread, imwrite, imfrombytes
from .transforms import (solarize, posterize, bgr2gray, gray2bgr, bgr2rgb,
rgb2bgr, bgr2hsv, hsv2bgr, bgr2hls, hls2bgr, iminvert,
imflip, imrotate, imcrop, impad, impad_to_multiple,
imnormalize, imdenormalize, imresize, imresize_like,
imrescale)
from .transforms import (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)
__all__ = [
'solarize', 'posterize', 'imread', 'imwrite', 'imfrombytes', 'bgr2gray',
'gray2bgr', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'bgr2hls',
'hls2bgr', 'iminvert', 'imflip', 'imrotate', 'imcrop', 'impad',
'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imresize',
'imresize_like', 'imrescale'
'rgb2gray', 'gray2bgr', 'gray2rgb', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv',
'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip', 'imrotate',
'imcrop', 'impad', 'impad_to_multiple', 'imnormalize', 'imdenormalize',
'imresize', 'imresize_like', 'imrescale'
]
from .colorspace import (solarize, posterize, bgr2gray, gray2bgr, bgr2rgb,
rgb2bgr, bgr2hsv, hsv2bgr, bgr2hls, hls2bgr, iminvert)
from .colorspace import (solarize, posterize, bgr2gray, rgb2gray, gray2bgr,
gray2rgb, bgr2rgb, rgb2bgr, bgr2hsv, hsv2bgr, bgr2hls,
hls2bgr, iminvert)
from .geometry import imflip, imrotate, imcrop, impad, impad_to_multiple
from .normalize import imnormalize, imdenormalize
from .resize import imresize, imresize_like, imrescale
__all__ = [
'solarize', 'posterize', 'bgr2gray', 'gray2bgr', 'bgr2rgb', 'rgb2bgr',
'bgr2hsv', 'hsv2bgr', 'bgr2hls', 'hls2bgr', 'iminvert', 'imflip',
'imrotate', 'imcrop', 'impad', 'impad_to_multiple', 'imnormalize',
'imdenormalize', 'imresize', 'imresize_like', 'imrescale'
'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'
]
......@@ -59,11 +59,28 @@ def bgr2gray(img, keepdim=False):
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):
"""Convert a grayscale image to BGR image.
Args:
img (ndarray or str): The input image.
img (ndarray): The input image.
Returns:
ndarray: The converted BGR image.
......@@ -73,6 +90,20 @@ def gray2bgr(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):
code = getattr(cv2, 'COLOR_{}2{}'.format(src.upper(), dst.upper()))
......
__version__ = '0.2.13'
__version__ = '0.2.14'
......@@ -66,6 +66,17 @@ class TestImage(object):
assert out_img_3d.shape == (10, 10, 1)
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):
in_img = np.random.rand(10, 10).astype(np.float32)
out_img = mmcv.gray2bgr(in_img)
......@@ -73,6 +84,13 @@ class TestImage(object):
for i in range(3):
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):
in_img = np.random.rand(10, 10, 3).astype(np.float32)
out_img = mmcv.bgr2rgb(in_img)
......
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