Pārlūkot izejas kodu

Add of white noise into filters

Jérôme BUISINE 6 gadi atpakaļ
vecāks
revīzija
dee5135a1f
2 mainītis faili ar 46 papildinājumiem un 2 dzēšanām
  1. 44 0
      ipfml/filters/noise.py
  2. 2 2
      setup.py

+ 44 - 0
ipfml/filters/noise.py

@@ -0,0 +1,44 @@
+import numpy as np
+from ipfml import processing
+
+def white_noise(image, n, distribution_interval=(-0.5, 0.5), k=0.2):
+    """
+    @brief White noise filter to apply on image
+    @param n - used to set importance of noise [1, 999]
+    @param distribution_interval - set the distribution interval of uniform distribution
+    @param k - variable that specifies the amount of noise to be taken into account in the output image
+    @return Image with white noise applied
+
+    Usage :
+
+    >>> from ipfml.filters.noise import white_noise
+    >>> import numpy as np
+    >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
+    >>> noisy_image = white_noise(image, 10)
+    >>> noisy_image.shape
+    (100, 100)
+    """
+
+    image_array = np.asarray(image)
+
+    a, b = distribution_interval
+    width, height = image_array.shape
+    nb_pixels = width * height
+
+    # getting flatten information from image and noise
+    image_array_flatten = image_array.reshape(nb_pixels)
+    white_noise_filter = np.random.uniform(a, b, nb_pixels)
+
+    # compute new pixel value
+    noisy_image = np.asarray([image_array_flatten[i] + n * k * white_noise_filter[i] for i in range(0, nb_pixels)])
+
+    # reshape and normalize new value
+    noisy_image = noisy_image.reshape((width, height))
+    noisy_image = np.asarray([np.array(processing.normalize_arr_with_range(noisy_image[i, :], 0, 255), 'uint8') for i in range(0, height)])
+
+    return noisy_image
+
+
+
+
+

+ 2 - 2
setup.py

@@ -14,11 +14,11 @@ class BuildTestCommand(setuptools.command.build_py.build_py):
     import doctest
     from ipfml import processing
     from ipfml import metrics
-    from ipfml import filters
+    from ipfml.filters import noise as noise_filters
 
     doctest.testmod(processing)
     doctest.testmod(metrics)
-    doctest.testmod(filters)
+    doctest.testmod(noise_filters)
 
     setuptools.command.build_py.build_py.run(self)