From ec8f62ef5f8d0674063c191ca0d13be3ffd70187 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 8 Feb 2012 18:27:25 -0800 Subject: [PATCH 1/3] ENH: Fix dtype conversion before hsv2rgb and rgb2hsv. --- skimage/color/colorconv.py | 5 ++--- skimage/color/tests/test_colorconv.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 7848d33d..d1129ec2 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -103,7 +103,7 @@ def convert_colorspace(arr, fromspace, tospace): return todict[tospace](fromdict[fromspace](arr)) -def _prepare_colorarray(arr, dtype=np.float32): +def _prepare_colorarray(arr): """Check the shape of the array, and give it the requested type.""" arr = np.asanyarray(arr) @@ -111,7 +111,7 @@ def _prepare_colorarray(arr, dtype=np.float32): 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 +303,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 diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index fef9bb17..03c23142 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -44,7 +44,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]) From f4330db90d1f5508b3a713e853fd50ab3e1b03f8 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 8 Feb 2012 19:58:26 -0800 Subject: [PATCH 2/3] ENH: Add grey2rgb. --- skimage/color/colorconv.py | 30 +++++++++++++++++++++++++-- skimage/color/tests/test_colorconv.py | 20 +++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index d1129ec2..3f0f4fb8 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" @@ -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)) diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 03c23142..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 @@ -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() From 9c7752eecf384a3b7c5c417b0ed28c50f393e560 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 9 Feb 2012 20:44:10 -0800 Subject: [PATCH 3/3] DOC: Fix outdated docstring. --- skimage/color/colorconv.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 3f0f4fb8..36165d68 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -104,7 +104,10 @@ def convert_colorspace(arr, fromspace, tospace): def _prepare_colorarray(arr): - """Check the shape of the array, and give it the requested type.""" + """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: