From 2992ca7ade10481a3398f0b0de6496e9bb6ecfa4 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Wed, 24 Apr 2013 05:13:34 +0530 Subject: [PATCH 1/6] Deprecating is_rgb() --- skimage/color/colorconv.py | 15 ++------------- skimage/color/tests/test_colorconv.py | 8 +++----- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index fc9ab342..ba670ddb 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -52,7 +52,7 @@ __all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', 'rgb_from_gdx', 'gdx_from_rgb', 'rgb_from_hax', 'hax_from_rgb', 'rgb_from_bro', 'bro_from_rgb', 'rgb_from_bpx', 'bpx_from_rgb', 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb', - 'is_rgb', 'is_gray' + 'is_gray' ] __docformat__ = "restructuredtext en" @@ -62,17 +62,6 @@ 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). @@ -652,7 +641,7 @@ def gray2rgb(image): If the input is not 2-dimensional. """ - if is_rgb(image): + if (image.ndim == 3 and image.shape[2] in (3, 4)): return image elif is_gray(image): return np.dstack((image, image, image)) diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 3365bb1c..470fd168 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -29,7 +29,7 @@ from skimage.color import ( rgb2grey, gray2rgb, xyz2lab, lab2xyz, lab2rgb, rgb2lab, - is_rgb, is_gray + is_gray ) from skimage import data_dir, data @@ -255,16 +255,14 @@ def test_gray2rgb_rgb(): assert_equal(x, y) -def test_is_rgb(): +def test_is_gray(): 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() From f1529aef8ebb21777f4ac88aa78e3ba9902a2e99 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Sat, 27 Apr 2013 11:12:13 +0530 Subject: [PATCH 2/6] Reverting the changes --- skimage/color/colorconv.py | 15 +++++++++++++-- skimage/color/tests/test_colorconv.py | 8 +++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index ba670ddb..fc9ab342 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -52,7 +52,7 @@ __all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', 'rgb_from_gdx', 'gdx_from_rgb', 'rgb_from_hax', 'hax_from_rgb', 'rgb_from_bro', 'bro_from_rgb', 'rgb_from_bpx', 'bpx_from_rgb', 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb', - 'is_gray' + 'is_rgb', 'is_gray' ] __docformat__ = "restructuredtext en" @@ -62,6 +62,17 @@ 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). @@ -641,7 +652,7 @@ def gray2rgb(image): If the input is not 2-dimensional. """ - if (image.ndim == 3 and image.shape[2] in (3, 4)): + if is_rgb(image): return image elif is_gray(image): return np.dstack((image, image, image)) diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 470fd168..3365bb1c 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -29,7 +29,7 @@ from skimage.color import ( rgb2grey, gray2rgb, xyz2lab, lab2xyz, lab2rgb, rgb2lab, - is_gray + is_rgb, is_gray ) from skimage import data_dir, data @@ -255,14 +255,16 @@ def test_gray2rgb_rgb(): assert_equal(x, y) -def test_is_gray(): +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() From 4cb251bbe6fd641e40219513287c7fd94c1b8532 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 30 Apr 2013 18:44:42 +0530 Subject: [PATCH 3/6] Deprecating is_gray and replacing it with is_gray_2d --- skimage/color/colorconv.py | 17 ++++++++++++++--- skimage/color/tests/test_colorconv.py | 13 +++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index fc9ab342..e7e7720a 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -52,7 +52,7 @@ __all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', 'rgb_from_gdx', 'gdx_from_rgb', 'rgb_from_hax', 'hax_from_rgb', 'rgb_from_bro', 'bro_from_rgb', 'rgb_from_bpx', 'bpx_from_rgb', 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb', - 'is_rgb', 'is_gray' + 'is_rgb', 'is_gray', 'is_gray_2d' ] __docformat__ = "restructuredtext en" @@ -60,6 +60,7 @@ __docformat__ = "restructuredtext en" import numpy as np from scipy import linalg from ..util import dtype +from skimage._shared.utils import deprecated def is_rgb(image): @@ -73,7 +74,7 @@ def is_rgb(image): """ return (image.ndim == 3 and image.shape[2] in (3, 4)) - +@deprecated('is_gray_2d') def is_gray(image): """Test whether the image is gray (i.e. has only one color band). @@ -83,8 +84,18 @@ def is_gray(image): Input image. """ - return image.ndim == 2 + return is_gray_2d(image) +def is_gray_2d(image): + """Test whether the image is 2d and grayscale. + + Parameters + ---------- + image : ndarray + Input image. + + """ + return np.squeeze(image).ndim == 2 def convert_colorspace(arr, fromspace, tospace): """Convert an image array to a new color space. diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 3365bb1c..2cdde1fe 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -29,7 +29,7 @@ from skimage.color import ( rgb2grey, gray2rgb, xyz2lab, lab2xyz, lab2rgb, rgb2lab, - is_rgb, is_gray + is_rgb, is_gray, is_gray_2d ) from skimage import data_dir, data @@ -232,19 +232,16 @@ class TestColorconv(TestCase): assert_array_almost_equal(lab2rgb(rgb2lab(img_rgb)), img_rgb) def test_gray2rgb(): - x = np.array([0, 0.5, 1]) - assert_raises(ValueError, gray2rgb, x) - - x = x.reshape((3, 1)) + x = np.array([[0, 0.5, 1], [0, 0.5, 1]]) y = gray2rgb(x) - assert_equal(y.shape, (3, 1, 3)) + assert_equal(y.shape, (2, 3, 3)) assert_equal(y.dtype, x.dtype) - x = np.array([[0, 128, 255]], dtype=np.uint8) + x = np.array([[0, 128], [128, 255], [255, 0]], dtype=np.uint8) z = gray2rgb(x) - assert_equal(z.shape, (1, 3, 3)) + assert_equal(z.shape, (3, 2, 3)) assert_equal(z[..., 0], x) assert_equal(z[0, 1, :], [128, 128, 128]) From 7dfea72ec3a3c5bd8c91ba4145255211878f3aa7 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Thu, 16 May 2013 00:52:51 +0800 Subject: [PATCH 4/6] Removing is_gray() and is_rgb() --- skimage/color/colorconv.py | 41 ++------------------------- skimage/color/tests/test_colorconv.py | 14 +-------- 2 files changed, 4 insertions(+), 51 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index e7e7720a..6260d9f5 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -51,8 +51,7 @@ __all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', 'rgb_from_bex', 'bex_from_rgb', 'rgb_from_rbd', 'rbd_from_rgb', 'rgb_from_gdx', 'gdx_from_rgb', 'rgb_from_hax', 'hax_from_rgb', 'rgb_from_bro', 'bro_from_rgb', 'rgb_from_bpx', 'bpx_from_rgb', - 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb', - 'is_rgb', 'is_gray', 'is_gray_2d' + 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb' ] __docformat__ = "restructuredtext en" @@ -63,40 +62,6 @@ from ..util import dtype from skimage._shared.utils import deprecated -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)) - -@deprecated('is_gray_2d') -def is_gray(image): - """Test whether the image is gray (i.e. has only one color band). - - Parameters - ---------- - image : ndarray - Input image. - - """ - return is_gray_2d(image) - -def is_gray_2d(image): - """Test whether the image is 2d and grayscale. - - Parameters - ---------- - image : ndarray - Input image. - - """ - return np.squeeze(image).ndim == 2 - def convert_colorspace(arr, fromspace, tospace): """Convert an image array to a new color space. @@ -663,9 +628,9 @@ def gray2rgb(image): If the input is not 2-dimensional. """ - if is_rgb(image): + if np.squeeze(image).ndim == 3 and image.shape[2] in (3, 4): return image - elif is_gray(image): + elif np.squeeze(image).ndim == 2: return np.dstack((image, image, image)) else: raise ValueError("Input image expected to be RGB, RGBA or gray.") diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 2cdde1fe..d31ef34c 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -28,8 +28,7 @@ from skimage.color import ( convert_colorspace, rgb2grey, gray2rgb, xyz2lab, lab2xyz, - lab2rgb, rgb2lab, - is_rgb, is_gray, is_gray_2d + lab2rgb, rgb2lab ) from skimage import data_dir, data @@ -252,16 +251,5 @@ 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() From dc3ecf58ad25d2cfdb62d4d21ad714201cf014e8 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Thu, 16 May 2013 00:56:54 +0800 Subject: [PATCH 5/6] Removing redundant imports used in previous commits --- skimage/color/colorconv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 6260d9f5..c718c2bb 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -59,7 +59,6 @@ __docformat__ = "restructuredtext en" import numpy as np from scipy import linalg from ..util import dtype -from skimage._shared.utils import deprecated def convert_colorspace(arr, fromspace, tospace): From d1a7a90a59f8480b4b0046e42cb8922a5a9a43a5 Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Tue, 21 May 2013 02:00:06 +0800 Subject: [PATCH 6/6] Deprecating is_rgb and is_gray --- skimage/color/colorconv.py | 42 ++++++++++++++++++--------- skimage/color/tests/test_colorconv.py | 25 ++++++++++++---- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index c718c2bb..1ef5a6c9 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -43,22 +43,36 @@ References from __future__ import division -__all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', - 'rgb2rgbcie', 'rgbcie2rgb', 'rgb2grey', 'rgb2gray', 'gray2rgb', - 'xyz2lab', 'lab2xyz', 'lab2rgb', 'rgb2lab', 'rgb2hed', 'hed2rgb', - 'separate_stains', 'combine_stains', 'rgb_from_hed', 'hed_from_rgb', - 'rgb_from_hdx', 'hdx_from_rgb', 'rgb_from_fgx', 'fgx_from_rgb', - 'rgb_from_bex', 'bex_from_rgb', 'rgb_from_rbd', 'rbd_from_rgb', - 'rgb_from_gdx', 'gdx_from_rgb', 'rgb_from_hax', 'hax_from_rgb', - 'rgb_from_bro', 'bro_from_rgb', 'rgb_from_bpx', 'bpx_from_rgb', - 'rgb_from_ahx', 'ahx_from_rgb', 'rgb_from_hpx', 'hpx_from_rgb' - ] - -__docformat__ = "restructuredtext en" - import numpy as np from scipy import linalg from ..util import dtype +from skimage._shared.utils import deprecated + + +@deprecated() +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)) + + +@deprecated() +def is_gray(image): + """Test whether the image is gray (i.e. has only one color band). + + Parameters + ---------- + image : ndarray + Input image. + + """ + return np.squeeze(image).ndim == 2 def convert_colorspace(arr, fromspace, tospace): @@ -629,7 +643,7 @@ def gray2rgb(image): """ if np.squeeze(image).ndim == 3 and image.shape[2] in (3, 4): return image - elif np.squeeze(image).ndim == 2: + elif image.ndim == 2 or np.squeeze(image).ndim == 2: return np.dstack((image, image, image)) else: raise ValueError("Input image expected to be RGB, RGBA or gray.") diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index d31ef34c..3365bb1c 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -28,7 +28,8 @@ from skimage.color import ( convert_colorspace, rgb2grey, gray2rgb, xyz2lab, lab2xyz, - lab2rgb, rgb2lab + lab2rgb, rgb2lab, + is_rgb, is_gray ) from skimage import data_dir, data @@ -231,16 +232,19 @@ class TestColorconv(TestCase): assert_array_almost_equal(lab2rgb(rgb2lab(img_rgb)), img_rgb) def test_gray2rgb(): - x = np.array([[0, 0.5, 1], [0, 0.5, 1]]) + x = np.array([0, 0.5, 1]) + assert_raises(ValueError, gray2rgb, x) + + x = x.reshape((3, 1)) y = gray2rgb(x) - assert_equal(y.shape, (2, 3, 3)) + assert_equal(y.shape, (3, 1, 3)) assert_equal(y.dtype, x.dtype) - x = np.array([[0, 128], [128, 255], [255, 0]], dtype=np.uint8) + x = np.array([[0, 128, 255]], dtype=np.uint8) z = gray2rgb(x) - assert_equal(z.shape, (3, 2, 3)) + assert_equal(z.shape, (1, 3, 3)) assert_equal(z[..., 0], x) assert_equal(z[0, 1, :], [128, 128, 128]) @@ -251,5 +255,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()