noise.py 8.6 KB

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