noise.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import numpy as np
  2. from ipfml import processing
  3. def __global_noise_filter(image, n, random_function, 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 random_function - random function we want to use to generate random numpy array
  9. @param distribution_interval - set the distribution interval of uniform distribution
  10. @param k - variable that specifies the amount of noise to be taken into account in the output image
  11. @return Image with specified noise applied
  12. Usage :
  13. >>> from ipfml.filters.noise import global_noise_filter
  14. >>> import numpy as np
  15. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  16. >>> noisy_image = global_noise_filter(image, 10, np.random.uniform)
  17. >>> noisy_image.shape
  18. (100, 100)
  19. """
  20. image_array = np.asarray(image)
  21. nb_chanel = 1
  22. if image_array.ndim != 3:
  23. width, height = image_array.shape
  24. else:
  25. width, height, nb_chanel = image_array.shape
  26. a, b = distribution_interval
  27. nb_pixels = width * height
  28. if identical:
  29. noise_filter = random_function(a, b, nb_pixels)
  30. # final output numpy array
  31. output_array = []
  32. for chanel in range(0, nb_chanel):
  33. # getting flatten information from image and noise
  34. if nb_chanel == 3:
  35. image_array_flatten = image_array[:, :, chanel].reshape(nb_pixels)
  36. else:
  37. image_array_flatten = image_array.reshape(nb_pixels)
  38. # redefine noise if necessary
  39. if not identical:
  40. noise_filter = random_function(a, b, nb_pixels)
  41. # compute new pixel value
  42. # n * k * white_noise_filter[i]
  43. noisy_image = np.asarray([image_array_flatten[i] + n * k * noise_filter[i] for i in range(0, nb_pixels)])
  44. # reshape and normalize new value
  45. noisy_image = noisy_image.reshape((width, height))
  46. noisy_image = np.asarray(noisy_image, 'uint8')
  47. # in order to concatenae output array
  48. if nb_chanel == 3:
  49. noisy_image = noisy_image[:, :, np.newaxis]
  50. # append new chanel
  51. output_array.append(noisy_image)
  52. # concatenate RGB image
  53. if nb_chanel == 3:
  54. output_array = np.concatenate(output_array, axis=2)
  55. return np.asarray(output_array)
  56. def white_noise(image, n, identical=False, distribution_interval=(-0.5, 0.5), k=0.2):
  57. """
  58. @brief White noise filter to apply on image
  59. @param image - image used as input (2D or 3D image representation)
  60. @param n - used to set importance of noise [1, 999]
  61. @param distribution_interval - set the distribution interval of normal law distribution
  62. @param k - variable that specifies the amount of noise to be taken into account in the output image
  63. @return Image with white noise applied
  64. Usage :
  65. >>> from ipfml.filters.noise import white_noise
  66. >>> import numpy as np
  67. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  68. >>> noisy_image = white_noise(image, 10)
  69. >>> noisy_image.shape
  70. (100, 100)
  71. """
  72. return __global_noise_filter(image, n, np.random.uniform, identical, distribution_interval, k)
  73. def gaussian_noise(image, n, identical=False, distribution_interval=(0, 1), k=0.1):
  74. """
  75. @brief Gaussian noise filter to apply on image
  76. @param image - image used as input (2D or 3D image representation)
  77. @param n - used to set importance of noise [1, 999]
  78. @param distribution_interval - set the distribution interval of normal law distribution
  79. @param k - variable that specifies the amount of noise to be taken into account in the output image
  80. @return Image with gaussian noise applied
  81. Usage :
  82. >>> from ipfml.filters.noise import gaussian_noise
  83. >>> import numpy as np
  84. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  85. >>> noisy_image = gaussian_noise(image, 10)
  86. >>> noisy_image.shape
  87. (100, 100)
  88. """
  89. return __global_noise_filter(image, n, np.random.normal, identical, distribution_interval, k)
  90. def laplace_noise(image, n, identical=False, distribution_interval=(0, 1), k=0.1):
  91. """
  92. @brief Laplace noise filter to apply on image
  93. @param image - image used as input (2D or 3D image representation)
  94. @param n - used to set importance of noise [1, 999]
  95. @param distribution_interval - set the distribution interval of normal law distribution
  96. @param k - variable that specifies the amount of noise to be taken into account in the output image
  97. @return Image with Laplace noise applied
  98. Usage :
  99. >>> from ipfml.filters.noise import gaussian_noise
  100. >>> import numpy as np
  101. >>> image = np.random.uniform(0, 255, 10000).reshape((100, 100))
  102. >>> noisy_image = gaussian_noise(image, 10)
  103. >>> noisy_image.shape
  104. (100, 100)
  105. """
  106. return __global_noise_filter(image, n, np.random.laplace, identical, distribution_interval, k)