mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-27 11:27:08 +08:00
ENH: Add grey2rgb.
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user