diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 7848d33d..36165d68 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -44,7 +44,7 @@ References from __future__ import division __all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', - 'rgb2rgbcie', 'rgbcie2rgb', 'rgb2grey', 'rgb2gray'] + 'rgb2rgbcie', 'rgbcie2rgb', 'rgb2grey', 'rgb2gray', 'gray2rgb'] __docformat__ = "restructuredtext en" @@ -103,15 +103,18 @@ def convert_colorspace(arr, fromspace, tospace): return todict[tospace](fromdict[fromspace](arr)) -def _prepare_colorarray(arr, dtype=np.float32): - """Check the shape of the array, and give it the requested type.""" +def _prepare_colorarray(arr): + """Check the shape of the array and convert it to + floating point representation. + + """ arr = np.asanyarray(arr) if arr.ndim != 3 or arr.shape[2] != 3: msg = "the input array must be have a shape == (.,.,3))" raise ValueError(msg) - return arr.astype(dtype) + return dtype.img_as_float(arr) def rgb2hsv(rgb): @@ -303,7 +306,6 @@ def _convert(matrix, arr): out : ndarray, dtype=float The converted array. """ - arr = dtype.img_as_float(arr) arr = _prepare_colorarray(arr) arr = np.swapaxes(arr, 0, 2) oldshape = arr.shape @@ -496,7 +498,7 @@ def rgb2grey(rgb): CRT phosphors:: Y = 0.2125 R + 0.7154 G + 0.0721 B - + If there is an alpha channel present, it is ignored. Examples @@ -512,3 +514,29 @@ def rgb2grey(rgb): return _convert(grey_from_rgb, rgb[:, :, :3])[..., 0] rgb2gray = rgb2grey + + +def gray2rgb(image): + """Create an RGB representation of a grey-level image. + + Parameters + ---------- + image : array_like + Input image of shape ``(M, N)``. + + Returns + ------- + rgb : ndarray + RGB image of shape ``(M, N, 3)``. + + Raises + ------ + ValueError + If the input is not 2-dimensional. + + """ + if image.ndim != 2: + raise ValueError('Gray-level image should be two-dimensional.') + + M, N = image.shape + return np.dstack((image, image, image)) diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index fef9bb17..be0d330c 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -16,13 +16,14 @@ import os.path import numpy as np from numpy.testing import * +from skimage import img_as_float from skimage.io import imread from skimage.color import ( rgb2hsv, hsv2rgb, rgb2xyz, xyz2rgb, rgb2rgbcie, rgbcie2rgb, convert_colorspace, - rgb2grey + rgb2grey, gray2rgb ) from skimage import data_dir @@ -44,7 +45,7 @@ class TestColorconv(TestCase): # RGB to HSV def test_rgb2hsv_conversion(self): - rgb = self.img_rgb.astype("float32")[::16, ::16] + rgb = img_as_float(self.img_rgb)[::16, ::16] hsv = rgb2hsv(rgb).reshape(-1, 3) # ground truth from colorsys gt = np.array([colorsys.rgb_to_hsv(pt[0], pt[1], pt[2]) @@ -150,6 +151,23 @@ class TestColorconv(TestCase): assert_equal(g.shape, (1, 1)) +def test_gray2rgb(): + x = np.array([0, 0.5, 1]) + assert_raises(ValueError, gray2rgb, x) + + x = x.reshape((3, 1)) + y = gray2rgb(x) + + assert_equal(y.shape, (3, 1, 3)) + assert_equal(y.dtype, x.dtype) + + x = np.array([[0, 128, 255]], dtype=np.uint8) + z = gray2rgb(x) + + assert_equal(z.shape, (1, 3, 3)) + assert_equal(z[..., 0], x) + assert_equal(z[0, 1, :], [128, 128, 128]) + if __name__ == "__main__": run_module_suite()