Parcourir la source

White noise updates

Jérôme BUISINE il y a 5 ans
Parent
commit
0b40c4d2e4
2 fichiers modifiés avec 45 ajouts et 14 suppressions
  1. 37 10
      ipfml/filters/noise.py
  2. 8 4
      ipfml/processing.py

+ 37 - 10
ipfml/filters/noise.py

@@ -4,6 +4,7 @@ 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 image - image used as input (2D or 3D image representation)
     @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
@@ -20,23 +21,49 @@ def white_noise(image, n, distribution_interval=(-0.5, 0.5), k=0.2):
     """
 
     image_array = np.asarray(image)
+    nb_chanel = 1
+
+    if image_array.ndim != 3:
+        width, height = image_array.shape
+    else:
+        width, height, nb_chanel = image_array.shape
 
     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)
+    # final output numpy array
+    output_array = []
+
+    for chanel in range(0, nb_chanel):
+
+        # 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)
+
+        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(noisy_image, 'uint8')
+
+        # in order to concatenae output array
+        if nb_chanel == 3:
+            noisy_image = noisy_image[:, :, np.newaxis]
 
-    # 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)])
+        # append new chanel
+        output_array.append(noisy_image)
 
-    # 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)])
+    # concatenate RGB image
+    if nb_chanel == 3:
+        output_array = np.concatenate(output_array, axis=2)
 
-    return noisy_image
+    return np.asarray(output_array)
 
 
 

+ 8 - 4
ipfml/processing.py

@@ -233,11 +233,15 @@ def normalize_2D_arr(arr):
     max_value = arr.max(axis=1).max()
     min_value = arr.min(axis=1).min()
 
-    # lambda computation to normalize
-    g = lambda x : (x - min_value) / (max_value - min_value)
-    f = np.vectorize(g)
+    # normalize each row
+    output_array = []
+    width, height = arr.shape
 
-    return f(arr)
+    for row_index in range(0, height):
+        values = arr[row_index, :]
+        output_array.append(normalize_arr_with_range(values, min_value, max_value))
+
+    return np.asarray(output_array)
 
 
 def rgb_to_mscn(image):