|
@@ -1,24 +1,17 @@
|
|
|
import numpy as np
|
|
|
+import random
|
|
|
+
|
|
|
from ipfml import processing
|
|
|
|
|
|
|
|
|
-def _global_noise_filter(image,
|
|
|
- n,
|
|
|
- generator,
|
|
|
- updator,
|
|
|
- identical=False,
|
|
|
- distribution_interval=(-0.5, 0.5),
|
|
|
- k=0.2):
|
|
|
+def _global_noise_filter(image, generator, updator, identical=False):
|
|
|
"""White noise filter to apply on image
|
|
|
|
|
|
Args:
|
|
|
image: image used as input (2D or 3D image representation)
|
|
|
- n: used to set importance of noise [1, 999]
|
|
|
generator: lambda function used to generate random numpy array with specific distribution
|
|
|
updator: lambda function used to update pixel value
|
|
|
identical: keep or not identical noise distribution for each canal if RGB Image (default False)
|
|
|
- distribution_interval: tuple which set the distribution interval of uniform distribution (default (-0.5, 0.5))
|
|
|
- k: variable that specifies the amount of noise to be taken into account in the output image (default 0.2)
|
|
|
|
|
|
Returns:
|
|
|
2D Numpy array with specified noise applied
|
|
@@ -28,9 +21,11 @@ def _global_noise_filter(image,
|
|
|
>>> from ipfml.filters.noise import _global_noise_filter as gf
|
|
|
>>> import numpy as np
|
|
|
>>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
|
|
|
- >>> generator = lambda x: np.random.uniform(-0.5, 0.5, x)
|
|
|
- >>> updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
- >>> noisy_image = gf(image, 10, generator, updator)
|
|
|
+ >>> generator = lambda h, w: np.random.uniform(-0.5, 0.5, (h, w))
|
|
|
+ >>> n = 10
|
|
|
+ >>> k = 0.2
|
|
|
+ >>> updator = lambda x, noise: x + n * k * noise
|
|
|
+ >>> noisy_image = gf(image, generator, updator)
|
|
|
>>> noisy_image.shape
|
|
|
(100, 100)
|
|
|
"""
|
|
@@ -39,57 +34,57 @@ def _global_noise_filter(image,
|
|
|
nb_chanel = 1
|
|
|
|
|
|
if image_array.ndim != 3:
|
|
|
- width, height = image_array.shape
|
|
|
+ height, width = image_array.shape
|
|
|
else:
|
|
|
- width, height, nb_chanel = image_array.shape
|
|
|
-
|
|
|
- a, b = distribution_interval
|
|
|
- nb_pixels = width * height
|
|
|
+ height, width, nb_chanel = image_array.shape
|
|
|
|
|
|
- if identical:
|
|
|
- noise_filter = generator(nb_pixels)
|
|
|
+ if nb_chanel == 1 or identical:
|
|
|
+ noise_filter = generator(width, height)
|
|
|
|
|
|
# final output numpy array
|
|
|
output_array = []
|
|
|
|
|
|
- for chanel in range(0, nb_chanel):
|
|
|
+ # check number of chanel
|
|
|
+ if nb_chanel == 1:
|
|
|
|
|
|
- # getting flatten information from image and noise
|
|
|
- if nb_chanel == 3:
|
|
|
- image_array_flatten = image_array[:, :, chanel].reshape(nb_pixels)
|
|
|
- else:
|
|
|
- image_array_flatten = image_array.reshape(nb_pixels)
|
|
|
+ image_array_flatten = image_array
|
|
|
+
|
|
|
+ noisy_image = np.array(
|
|
|
+ list(map(updator, image_array_flatten, noise_filter)))
|
|
|
+
|
|
|
+ return np.array(noisy_image, 'uint8')
|
|
|
+
|
|
|
+ else:
|
|
|
+ # final output numpy array
|
|
|
+ output_array = []
|
|
|
|
|
|
- # redefine noise if necessary
|
|
|
- if not identical:
|
|
|
- noise_filter = generator(nb_pixels)
|
|
|
+ for chanel in range(0, nb_chanel):
|
|
|
|
|
|
- # compute new pixel value
|
|
|
- # n * k * white_noise_filter[i]
|
|
|
- noisy_image = np.asarray([
|
|
|
- updator(image_array_flatten[i], n, k, noise_filter[i])
|
|
|
- for i in range(0, nb_pixels)
|
|
|
- ])
|
|
|
+ # getting flatten information from image and noise
|
|
|
+ image_array_flatten = image_array[:, :, chanel]
|
|
|
|
|
|
- # reshape and normalize new value
|
|
|
- noisy_image = noisy_image.reshape((width, height))
|
|
|
+ # redefine noise if necessary
|
|
|
+ if not identical:
|
|
|
+ noise_filter = generator(height, width)
|
|
|
|
|
|
- noisy_image = np.asarray(noisy_image, 'uint8')
|
|
|
+ # compute new pixel value
|
|
|
+ # x + n * k * white_noise_filter[i] as example
|
|
|
+ noisy_image = np.array(
|
|
|
+ list(map(updator, image_array_flatten, noise_filter)))
|
|
|
|
|
|
- # in order to concatenae output array
|
|
|
- if nb_chanel == 3:
|
|
|
+ # set uint8 values
|
|
|
+ noisy_image = np.array(noisy_image, 'uint8')
|
|
|
+
|
|
|
+ # in order to concatenate output array
|
|
|
noisy_image = noisy_image[:, :, np.newaxis]
|
|
|
|
|
|
- # append new chanel
|
|
|
- output_array.append(noisy_image)
|
|
|
+ # append new chanel
|
|
|
+ output_array.append(noisy_image)
|
|
|
|
|
|
- # concatenate RGB image
|
|
|
- if nb_chanel == 3:
|
|
|
+ # concatenate RGB image
|
|
|
output_array = np.concatenate(output_array, axis=2)
|
|
|
- else:
|
|
|
- output_array = np.asarray(output_array).reshape(width, height)
|
|
|
|
|
|
- return output_array
|
|
|
+ return output_array
|
|
|
|
|
|
|
|
|
def white_noise(image,
|
|
@@ -120,12 +115,11 @@ def white_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.uniform(a, b, x)
|
|
|
+ generator = lambda h, w: np.random.uniform(a, b, (h, w))
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
+ updator = lambda x, noise: x + n * k * noise
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
|
|
|
|
|
|
def gaussian_noise(image,
|
|
@@ -156,12 +150,11 @@ def gaussian_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.normal(a, b, x)
|
|
|
+ generator = lambda h, w: np.random.normal(a, b, (h, w))
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
+ updator = lambda x, noise: x + n * k * noise
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
|
|
|
|
|
|
def laplace_noise(image,
|
|
@@ -192,12 +185,11 @@ def laplace_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.laplace(a, b, x)
|
|
|
+ generator = lambda h, w: np.random.laplace(a, b, (h, w))
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
+ updator = lambda x, noise: x + n * k * noise
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
|
|
|
|
|
|
def cauchy_noise(image,
|
|
@@ -228,12 +220,11 @@ def cauchy_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.standard_cauchy(x)
|
|
|
+ generator = lambda h, w: np.random.standard_cauchy((h, w))
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
+ updator = lambda x, noise: x + n * k * noise
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
|
|
|
|
|
|
def log_normal_noise(image,
|
|
@@ -264,12 +255,11 @@ def log_normal_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.lognormal(a, b, x)
|
|
|
+ generator = lambda h, w: np.random.lognormal(a, b, (h, w))
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x + n * k * noise
|
|
|
+ updator = lambda x, noise: x + n * k * noise
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
|
|
|
|
|
|
def mut_white_noise(image,
|
|
@@ -300,9 +290,63 @@ def mut_white_noise(image,
|
|
|
"""
|
|
|
|
|
|
a, b = distribution_interval
|
|
|
- generator = lambda x: np.random.uniform(a, b, x)
|
|
|
+ generator = lambda h, w: np.random.uniform(a, b, (h, w))
|
|
|
+
|
|
|
+ updator = lambda x, noise: x * n * k * noise
|
|
|
+
|
|
|
+ return _global_noise_filter(image, generator, updator, identical)
|
|
|
+
|
|
|
|
|
|
- updator = lambda x, n, k, noise: x * n * k * noise
|
|
|
+def salt_pepper_noise(image, n, identical=False, p=0.1, k=0.5):
|
|
|
+ """Pepper salt noise filter to apply on image
|
|
|
+
|
|
|
+ Args:
|
|
|
+ image: image used as input (2D or 3D image representation)
|
|
|
+ n: used to set importance of noise [1, 999]
|
|
|
+ identical: keep or not identical noise distribution for each canal if RGB Image (default False)
|
|
|
+ p: probability to increase pixel value otherwise decrease it
|
|
|
+ k: variable that specifies the amount of noise to be taken into account in the output image (default 0.5)
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ 2D Numpy array with salt and pepper noise applied
|
|
|
+
|
|
|
+ Example:
|
|
|
+
|
|
|
+ >>> from ipfml.filters.noise import salt_pepper_noise
|
|
|
+ >>> import numpy as np
|
|
|
+ >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
|
|
|
+ >>> noisy_image = salt_pepper_noise(image, 10)
|
|
|
+ >>> noisy_image.shape
|
|
|
+ (100, 100)
|
|
|
+ """
|
|
|
+
|
|
|
+ def _generator(h, w):
|
|
|
+
|
|
|
+ x = w * h
|
|
|
+ nb_elem = int(p * x)
|
|
|
+
|
|
|
+ elements = np.full(x, 0)
|
|
|
+ elements[0:nb_elem] = 1
|
|
|
+ np.random.shuffle(elements)
|
|
|
+
|
|
|
+ return elements.reshape(h, w)
|
|
|
+
|
|
|
+ # here noise variable is boolean to update or not pixel value
|
|
|
+ def _updator(x, noise):
|
|
|
+
|
|
|
+ # apply specific changes to each value of 1D array
|
|
|
+ if isinstance(x, np.ndarray):
|
|
|
+ return np.array(list(map(_updator, x, noise)))
|
|
|
+
|
|
|
+ # probabilty to increase or decrease pixel value
|
|
|
+ rand = random.uniform(0, 1)
|
|
|
+
|
|
|
+ if noise:
|
|
|
+ if rand > 0.5:
|
|
|
+ return x + n * k
|
|
|
+ else:
|
|
|
+ return x - n * k
|
|
|
+ else:
|
|
|
+ return x
|
|
|
|
|
|
- return _global_noise_filter(image, n, generator, updator, identical,
|
|
|
- distribution_interval, k)
|
|
|
+ return _global_noise_filter(image, _generator, _updator, identical)
|