From db7e9c2b4ab6f7f9314a112107e1a5669392670d Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Tue, 26 Jul 2016 19:25:20 +1000 Subject: [PATCH] Bug fix: correct pixel selection for s&p noise --- skimage/util/noise.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/skimage/util/noise.py b/skimage/util/noise.py index ff20084a..9ee77061 100644 --- a/skimage/util/noise.py +++ b/skimage/util/noise.py @@ -166,23 +166,19 @@ def random_noise(image, mode='gaussian', seed=None, clip=True, **kwargs): amount=kwargs['amount'], salt_vs_pepper=0.) elif mode == 's&p': - # This mode makes no effort to avoid repeat sampling. Thus, the - # exact number of replaced pixels is only approximate. out = image.copy() # Salt mode - num_salt = np.ceil( - kwargs['amount'] * image.size * kwargs['salt_vs_pepper']) - coords = [np.random.randint(0, i - 1, int(num_salt)) - for i in image.shape] - out[coords] = 1 + p_salt = kwargs['amount'] * kwargs['salt_vs_pepper'] + mask = np.random.choice([True, False], size=image.shape, + p=[p_salt, 1 - p_salt]) + out[mask] = 1 # Pepper mode - num_pepper = np.ceil( - kwargs['amount'] * image.size * (1. - kwargs['salt_vs_pepper'])) - coords = [np.random.randint(0, i - 1, int(num_pepper)) - for i in image.shape] - out[coords] = low_clip + p_pepper = kwargs['amount'] * (1 - kwargs['salt_vs_pepper']) + mask = np.random.choice([True, False], size=image.shape, + p=[p_pepper, 1 - p_pepper]) + out[mask] = low_clip elif mode == 'speckle': noise = np.random.normal(kwargs['mean'], kwargs['var'] ** 0.5,