Parcourir la source

Update of salt and pepper noise _updator

Jérôme BUISINE il y a 5 ans
Parent
commit
91e855d88b

+ 2 - 2
README.md

@@ -1,9 +1,9 @@
 Image Processing For Machine Learning
 =====================================
 
-<center>
+<p align="center">
     <img src="ipfml_logo.png" width="40%">
-</center>
+</p>
 
 Installation
 ------------

BIN
docs/build/doctrees/environment.pickle


BIN
docs/build/doctrees/ipfml.doctree


+ 1 - 1
docs/build/html/ipfml.html

@@ -1086,7 +1086,7 @@
 <li><strong>image</strong> – image used as input (2D or 3D image representation)</li>
 <li><strong>n</strong> – used to set importance of noise [1, 999]</li>
 <li><strong>identical</strong> – keep or not identical noise distribution for each canal if RGB Image (default False)</li>
-<li><strong>p</strong> – Noise probability</li>
+<li><strong>p</strong> – probability to increase pixel value otherwise decrease it</li>
 <li><strong>k</strong> – variable that specifies the amount of noise to be taken into account in the output image (default 0.5)</li>
 </ul>
 </td>

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 1
docs/build/html/searchindex.js


+ 16 - 14
ipfml/filters/noise.py

@@ -1,13 +1,10 @@
 import numpy as np
+import random
+
 from ipfml import processing
 
 
-def _global_noise_filter(image,
-                         n,
-                         generator,
-                         updator,
-                         identical=False,
-                         k=0.2):
+def _global_noise_filter(image, n, generator, updator, identical=False, k=0.2):
     """White noise filter to apply on image
 
     Args:
@@ -299,11 +296,7 @@ def mut_white_noise(image,
     return _global_noise_filter(image, n, generator, updator, identical, k)
 
 
-def salt_pepper_noise(image,
-                     n,
-                     identical=False,
-                     p=0.1,
-                     k=0.5):
+def salt_pepper_noise(image, n, identical=False, p=0.1, k=0.5):
     """Pepper salt noise filter to apply on image
 
     Args:
@@ -335,9 +328,18 @@ def salt_pepper_noise(image,
 
         return elements
 
-    # here noise variable is boolean to increase or decrease pixel value
-    updator = lambda x, n, k, noise: (x + n * k) if noise == 1 else (x - n * k)
+    # here noise variable is boolean to update or not pixel value
+    def _updator(x, n, k, noise):
 
-    return _global_noise_filter(image, n, _generator, updator, identical, k)
+        # 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, k)