mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-12 14:00:47 +08:00
Merge pull request #1919 from ahojnnes/rgb2gray
Speedup rgb2gray and return congiguous array
This commit is contained in:
@@ -707,10 +707,18 @@ def rgb2gray(rgb):
|
||||
>>> img = data.astronaut()
|
||||
>>> img_gray = rgb2gray(img)
|
||||
"""
|
||||
if rgb.ndim == 2:
|
||||
return rgb
|
||||
|
||||
return _convert(gray_from_rgb, rgb[:, :, :3])[..., 0]
|
||||
if rgb.ndim == 2:
|
||||
return np.ascontiguousarray(rgb)
|
||||
|
||||
rgb = _prepare_colorarray(rgb[..., :3])
|
||||
|
||||
gray = 0.2125 * rgb[..., 0]
|
||||
gray[:] += 0.7154 * rgb[..., 1]
|
||||
gray[:] += 0.0721 * rgb[..., 2]
|
||||
|
||||
return gray
|
||||
|
||||
|
||||
rgb2grey = rgb2gray
|
||||
|
||||
|
||||
@@ -226,6 +226,15 @@ 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_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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user