From 65a29071fe31e7ea6e89f0d09e36e6cb3db2e0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 31 Jan 2016 19:21:03 +0100 Subject: [PATCH 1/3] Speedup rgb2gray and return congiguous array --- skimage/color/colorconv.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 137c14c0..875c937b 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -707,10 +707,17 @@ def rgb2gray(rgb): >>> img = data.astronaut() >>> img_gray = rgb2gray(img) """ + if rgb.ndim == 2: return rgb - return _convert(gray_from_rgb, rgb[:, :, :3])[..., 0] + rgb = _prepare_colorarray(rgb) + + gray = 0.2125 * rgb[..., 0] + gray[:] += 0.7154 * rgb[..., 1] + gray[:] += 0.0721 * rgb[..., 2] + + return gray rgb2grey = rgb2gray From f4e0370bc28cf0821c351886940bf2837de06e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 31 Jan 2016 19:35:53 +0100 Subject: [PATCH 2/3] Make sure rgb2gray output is contiguous --- skimage/color/colorconv.py | 3 ++- skimage/color/tests/test_colorconv.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 875c937b..e9da10e5 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -709,7 +709,7 @@ def rgb2gray(rgb): """ if rgb.ndim == 2: - return rgb + return np.ascontiguousarray(rgb) rgb = _prepare_colorarray(rgb) @@ -719,6 +719,7 @@ def rgb2gray(rgb): return gray + rgb2grey = rgb2gray diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 4c991416..b7805786 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -226,6 +226,11 @@ class TestColorconv(TestCase): assert_equal(g.shape, (1, 1)) + def test_rgb2grey_contiguous(self): + x = np.random.rand(10, 10, 3) + assert rgb2grey(x).flags["C_CONTIGUOUS"] + assert rgb2grey(x[:5, :5]).flags["C_CONTIGUOUS"] + def test_rgb2grey_on_grey(self): rgb2grey(np.random.rand(5, 5)) From 4e5c4d3c4d7242c408cab7214db825c08efc215b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 31 Jan 2016 21:50:56 +0100 Subject: [PATCH 3/3] Fix rgb2grey for alpha channel --- skimage/color/colorconv.py | 2 +- skimage/color/tests/test_colorconv.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index e9da10e5..1eb63721 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -711,7 +711,7 @@ def rgb2gray(rgb): if rgb.ndim == 2: return np.ascontiguousarray(rgb) - rgb = _prepare_colorarray(rgb) + rgb = _prepare_colorarray(rgb[..., :3]) gray = 0.2125 * rgb[..., 0] gray[:] += 0.7154 * rgb[..., 1] diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index b7805786..96057e3b 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -231,6 +231,10 @@ class TestColorconv(TestCase): assert rgb2grey(x).flags["C_CONTIGUOUS"] assert rgb2grey(x[:5, :5]).flags["C_CONTIGUOUS"] + def test_rgb2grey_alpha(self): + x = np.random.rand(10, 10, 4) + assert rgb2grey(x).ndim == 2 + def test_rgb2grey_on_grey(self): rgb2grey(np.random.rand(5, 5))