ENH: Add grey2rgb.

This commit is contained in:
Stefan van der Walt
2012-02-08 19:58:26 -08:00
parent ec8f62ef5f
commit f4330db90d
2 changed files with 47 additions and 3 deletions
+28 -2
View File
@@ -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"
@@ -495,7 +495,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
@@ -511,3 +511,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))
+19 -1
View File
@@ -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
@@ -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()