Merge pull request #367 from stefanv/gray2rgb_for_rgb

BUG: Allow RGB images to be passed to grey2rgb.
This commit is contained in:
Tony S Yu
2012-10-17 17:15:39 -07:00
2 changed files with 61 additions and 16 deletions
+40 -14
View File
@@ -45,7 +45,7 @@ from __future__ import division
__all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb',
'rgb2rgbcie', 'rgbcie2rgb', 'rgb2grey', 'rgb2gray', 'gray2rgb',
'xyz2lab', 'lab2xyz', 'lab2rgb', 'rgb2lab'
'xyz2lab', 'lab2xyz', 'lab2rgb', 'rgb2lab', 'is_rgb', 'is_gray'
]
__docformat__ = "restructuredtext en"
@@ -55,6 +55,31 @@ from scipy import linalg
from ..util import dtype
def is_rgb(image):
"""Test whether the image is RGB or RGBA.
Parameters
----------
image : ndarray
Input image.
"""
return (image.ndim == 3 and image.shape[2] in (3, 4))
def is_gray(image):
"""Test whether the image is gray (i.e. has only one color band).
Parameters
----------
image : ndarray
Input image.
"""
return image.ndim == 2
def convert_colorspace(arr, fromspace, tospace):
"""Convert an image array to a new color space.
@@ -281,7 +306,7 @@ rgbcie_from_rgb = np.dot(rgbcie_from_xyz, xyz_from_rgb)
rgb_from_rgbcie = np.dot(rgb_from_xyz, xyz_from_rgbcie)
grey_from_rgb = np.array([[0.2125, 0.7154, 0.0721],
gray_from_rgb = np.array([[0.2125, 0.7154, 0.0721],
[0, 0, 0],
[0, 0, 0]])
@@ -458,7 +483,7 @@ def rgbcie2rgb(rgbcie):
return _convert(rgb_from_rgbcie, rgbcie)
def rgb2grey(rgb):
def rgb2gray(rgb):
"""Compute luminance of an RGB image.
Parameters
@@ -475,7 +500,7 @@ def rgb2grey(rgb):
Raises
------
ValueError
If `rgb2grey` is not a 3-D array of shape (.., .., 3) or
If `rgb2gray` is not a 3-D array of shape (.., .., 3) or
(.., .., 4).
References
@@ -493,21 +518,21 @@ def rgb2grey(rgb):
Examples
--------
>>> from skimage.color import rgb2grey
>>> from skimage.color import rgb2gray
>>> from skimage import data
>>> lena = data.lena()
>>> lena_grey = rgb2grey(lena)
>>> lena_gray = rgb2gray(lena)
"""
if rgb.ndim == 2:
return rgb
return _convert(grey_from_rgb, rgb[:, :, :3])[..., 0]
return _convert(gray_from_rgb, rgb[:, :, :3])[..., 0]
rgb2gray = rgb2grey
rgb2grey = rgb2gray
def gray2rgb(image):
"""Create an RGB representation of a grey-level image.
"""Create an RGB representation of a gray-level image.
Parameters
----------
@@ -525,11 +550,12 @@ def gray2rgb(image):
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))
if is_rgb(image):
return image
elif is_gray(image):
return np.dstack((image, image, image))
else:
raise ValueError("Input image expected to be RGB, RGBA or gray.")
def xyz2lab(xyz):
+21 -2
View File
@@ -25,10 +25,11 @@ from skimage.color import (
convert_colorspace,
rgb2grey, gray2rgb,
xyz2lab, lab2xyz,
lab2rgb, rgb2lab
lab2rgb, rgb2lab,
is_rgb, is_gray
)
from skimage import data_dir
from skimage import data_dir, data
import colorsys
@@ -196,5 +197,23 @@ def test_gray2rgb():
assert_equal(z[..., 0], x)
assert_equal(z[0, 1, :], [128, 128, 128])
def test_gray2rgb_rgb():
x = np.random.random((5, 5, 4))
y = gray2rgb(x)
assert_equal(x, y)
def test_is_rgb():
color = data.lena()
gray = data.camera()
assert is_rgb(color)
assert not is_gray(color)
assert is_gray(gray)
assert not is_gray(color)
if __name__ == "__main__":
run_module_suite()