From 120571b079788703d10905bbbfa0e57781d66ff0 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Mon, 15 Oct 2012 22:44:01 -0700 Subject: [PATCH 1/2] BUG: Allow RGB images to be passed to grey2rgb. --- skimage/color/colorconv.py | 13 ++++++++----- skimage/color/tests/test_colorconv.py | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index ec5551d6..2e7f9138 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -525,11 +525,14 @@ 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 image.ndim > 2: + return image + elif image.ndim == 2: + M, N = image.shape + return np.dstack((image, image, image)) + else: + raise ValueError('Gray-level image should be two-dimensional, ' + 'RGB or RGBA.') def xyz2lab(xyz): diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 316d801a..1fc46b62 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -196,5 +196,12 @@ 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) + + if __name__ == "__main__": run_module_suite() From 4a0564a7697e6a0ff81163338ab3a28bdd6bddf2 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 16 Oct 2012 23:33:29 -0700 Subject: [PATCH 2/2] Add is_rgb and is_gray. --- skimage/color/colorconv.py | 51 +++++++++++++++++++-------- skimage/color/tests/test_colorconv.py | 16 +++++++-- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 2e7f9138..38eab995 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -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,14 +550,12 @@ def gray2rgb(image): If the input is not 2-dimensional. """ - if image.ndim > 2: + if is_rgb(image): return image - elif image.ndim == 2: - M, N = image.shape + elif is_gray(image): return np.dstack((image, image, image)) else: - raise ValueError('Gray-level image should be two-dimensional, ' - 'RGB or RGBA.') + raise ValueError("Input image expected to be RGB, RGBA or gray.") def xyz2lab(xyz): diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 1fc46b62..c09c9d22 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -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 @@ -203,5 +204,16 @@ def test_gray2rgb_rgb(): 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()