noise.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import numpy as np
  2. from ipfml import processing
  3. def _global_noise_filter(image,
  4. n,
  5. generator,
  6. updator,
  7. identical=False,
  8. distribution_interval=(-0.5, 0.5),
  9. k=0.2):
  10. """White noise filter to apply on image
  11. Args:
  12. image: image used as input (2D or 3D image representation)
  13. n: used to set importance of noise [1, 999]
  14. generator: lambda function used to generate random numpy array with specific distribution
  15. updator: lambda function used to update pixel value
  16. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  17. distribution_interval: tuple which set the distribution interval of uniform distribution (default (-0.5, 0.5))
  18. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.2)
  19. Returns:
  20. 2D Numpy array with specified noise applied
  21. Example:
  22. >>> from ipfml.filters.noise import _global_noise_filter as gf
  23. >>> import numpy as np
  24. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  25. >>> generator = lambda x: np.random.uniform(-0.5, 0.5, x)
  26. >>> updator = lambda x, n, k, noise: x + n * k * noise
  27. >>> noisy_image = gf(image, 10, generator, updator)
  28. >>> noisy_image.shape
  29. (100, 100)
  30. """
  31. image_array = np.asarray(image)
  32. nb_chanel = 1
  33. if image_array.ndim != 3:
  34. width, height = image_array.shape
  35. else:
  36. width, height, nb_chanel = image_array.shape
  37. a, b = distribution_interval
  38. nb_pixels = width * height
  39. if identical:
  40. noise_filter = generator(nb_pixels)
  41. # final output numpy array
  42. output_array = []
  43. for chanel in range(0, nb_chanel):
  44. # getting flatten information from image and noise
  45. if nb_chanel == 3:
  46. image_array_flatten = image_array[:, :, chanel].reshape(nb_pixels)
  47. else:
  48. image_array_flatten = image_array.reshape(nb_pixels)
  49. # redefine noise if necessary
  50. if not identical:
  51. noise_filter = generator(nb_pixels)
  52. # compute new pixel value
  53. # n * k * white_noise_filter[i]
  54. noisy_image = np.asarray([
  55. updator(image_array_flatten[i], n, k, noise_filter[i])
  56. for i in range(0, nb_pixels)
  57. ])
  58. # reshape and normalize new value
  59. noisy_image = noisy_image.reshape((width, height))
  60. noisy_image = np.asarray(noisy_image, 'uint8')
  61. # in order to concatenae output array
  62. if nb_chanel == 3:
  63. noisy_image = noisy_image[:, :, np.newaxis]
  64. # append new chanel
  65. output_array.append(noisy_image)
  66. # concatenate RGB image
  67. if nb_chanel == 3:
  68. output_array = np.concatenate(output_array, axis=2)
  69. else:
  70. output_array = np.asarray(output_array).reshape(width, height)
  71. return output_array
  72. def white_noise(image,
  73. n,
  74. identical=False,
  75. distribution_interval=(-0.5, 0.5),
  76. k=0.2):
  77. """White noise filter to apply on image
  78. Args:
  79. image: image used as input (2D or 3D image representation)
  80. n: used to set importance of noise [1, 999]
  81. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  82. distribution_interval: set the distribution interval of normal law distribution (default (-0.5, 0.5))
  83. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.2)
  84. Returns:
  85. 2D Numpy array with white noise applied
  86. Example:
  87. >>> from ipfml.filters.noise import white_noise
  88. >>> import numpy as np
  89. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  90. >>> noisy_image = white_noise(image, 10)
  91. >>> noisy_image.shape
  92. (100, 100)
  93. """
  94. a, b = distribution_interval
  95. generator = lambda x: np.random.uniform(a, b, x)
  96. updator = lambda x, n, k, noise: x + n * k * noise
  97. return _global_noise_filter(image, n, generator, updator, identical,
  98. distribution_interval, k)
  99. def gaussian_noise(image,
  100. n,
  101. identical=False,
  102. distribution_interval=(0, 1),
  103. k=0.1):
  104. """Gaussian noise filter to apply on image
  105. Args:
  106. image: image used as input (2D or 3D image representation)
  107. n: used to set importance of noise [1, 999]
  108. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  109. distribution_interval: set the distribution interval of normal law distribution (default (0, 1))
  110. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.1)
  111. Returns:
  112. 2D Numpy array with gaussian noise applied
  113. Example:
  114. >>> from ipfml.filters.noise import gaussian_noise
  115. >>> import numpy as np
  116. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  117. >>> noisy_image = gaussian_noise(image, 10)
  118. >>> noisy_image.shape
  119. (100, 100)
  120. """
  121. a, b = distribution_interval
  122. generator = lambda x: np.random.normal(a, b, x)
  123. updator = lambda x, n, k, noise: x + n * k * noise
  124. return _global_noise_filter(image, n, generator, updator, identical,
  125. distribution_interval, k)
  126. def laplace_noise(image,
  127. n,
  128. identical=False,
  129. distribution_interval=(0, 1),
  130. k=0.1):
  131. """Laplace noise filter to apply on image
  132. Args:
  133. image: image used as input (2D or 3D image representation)
  134. n: used to set importance of noise [1, 999]
  135. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  136. distribution_interval: set the distribution interval of normal law distribution (default (0, 1))
  137. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.1)
  138. Returns:
  139. 2D Numpay array with Laplace noise applied
  140. Example:
  141. >>> from ipfml.filters.noise import laplace_noise
  142. >>> import numpy as np
  143. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  144. >>> noisy_image = laplace_noise(image, 10)
  145. >>> noisy_image.shape
  146. (100, 100)
  147. """
  148. a, b = distribution_interval
  149. generator = lambda x: np.random.laplace(a, b, x)
  150. updator = lambda x, n, k, noise: x + n * k * noise
  151. return _global_noise_filter(image, n, generator, updator, identical,
  152. distribution_interval, k)
  153. def cauchy_noise(image,
  154. n,
  155. identical=False,
  156. distribution_interval=(0, 1),
  157. k=0.0002):
  158. """Cauchy noise filter to apply on image
  159. Args:
  160. image: image used as input (2D or 3D image representation)
  161. n: used to set importance of noise [1, 999]
  162. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  163. distribution_interval: set the distribution interval of normal law distribution (default (0, 1))
  164. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.0002)
  165. Returns:
  166. 2D Numpy array with Cauchy noise applied
  167. Example:
  168. >>> from ipfml.filters.noise import cauchy_noise
  169. >>> import numpy as np
  170. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  171. >>> noisy_image = cauchy_noise(image, 10)
  172. >>> noisy_image.shape
  173. (100, 100)
  174. """
  175. a, b = distribution_interval
  176. generator = lambda x: np.random.standard_cauchy(x)
  177. updator = lambda x, n, k, noise: x + n * k * noise
  178. return _global_noise_filter(image, n, generator, updator, identical,
  179. distribution_interval, k)
  180. def log_normal_noise(image,
  181. n,
  182. identical=False,
  183. distribution_interval=(0, 1),
  184. k=0.05):
  185. """Log-normal noise filter to apply on image
  186. Args:
  187. image: image used as input (2D or 3D image representation)
  188. n: used to set importance of noise [1, 999]
  189. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  190. distribution_interval: set the distribution interval of normal law distribution (default (0, 1))
  191. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.05)
  192. Returns:
  193. 2D Numpy array with Log-normal noise applied
  194. Example:
  195. >>> from ipfml.filters.noise import log_normal_noise
  196. >>> import numpy as np
  197. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  198. >>> noisy_image = log_normal_noise(image, 10)
  199. >>> noisy_image.shape
  200. (100, 100)
  201. """
  202. a, b = distribution_interval
  203. generator = lambda x: np.random.lognormal(a, b, x)
  204. updator = lambda x, n, k, noise: x + n * k * noise
  205. return _global_noise_filter(image, n, generator, updator, identical,
  206. distribution_interval, k)
  207. def mut_white_noise(image,
  208. n,
  209. identical=False,
  210. distribution_interval=(-0.5, 0.5),
  211. k=0.2):
  212. """Multiplied White noise filter to apply on image
  213. Args:
  214. image: image used as input (2D or 3D image representation)
  215. n: used to set importance of noise [1, 999]
  216. identical: keep or not identical noise distribution for each canal if RGB Image (default False)
  217. distribution_interval: set the distribution interval of normal law distribution (default (-0.5, 0.5))
  218. k: variable that specifies the amount of noise to be taken into account in the output image (default 0.2)
  219. Returns:
  220. 2D Numpy array with multiplied white noise applied
  221. Example:
  222. >>> from ipfml.filters.noise import mut_white_noise
  223. >>> import numpy as np
  224. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  225. >>> noisy_image = mut_white_noise(image, 10)
  226. >>> noisy_image.shape
  227. (100, 100)
  228. """
  229. a, b = distribution_interval
  230. generator = lambda x: np.random.uniform(a, b, x)
  231. updator = lambda x, n, k, noise: x * n * k * noise
  232. return _global_noise_filter(image, n, generator, updator, identical,
  233. distribution_interval, k)