Transformation.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # main imports
  2. import os
  3. import numpy as np
  4. # image processing imports
  5. from ipfml.processing import transform
  6. from ipfml.processing import reconstruction
  7. from ipfml.filters import convolution, kernels
  8. from ipfml import utils
  9. import cv2
  10. from skimage.restoration import denoise_nl_means, estimate_sigma
  11. from PIL import Image
  12. # modules imports
  13. from ..utils import data as functions
  14. # Transformation class to store transformation method of image and get usefull information
  15. class Transformation():
  16. def __init__(self, _transformation, _param, _size):
  17. self.transformation = _transformation
  18. self.param = _param
  19. self.size = _size
  20. def getTransformedImage(self, img):
  21. if self.transformation == 'svd_reconstruction':
  22. begin, end = list(map(int, self.param.split(',')))
  23. h, w = list(map(int, self.size.split(',')))
  24. img_reconstructed = reconstruction.svd(img, [begin, end])
  25. data_array = np.array(img_reconstructed, 'uint8')
  26. img_array = Image.fromarray(data_array)
  27. img_array.thumbnail((h, w))
  28. data = np.array(img_array)
  29. if self.transformation == 'ipca_reconstruction':
  30. n_components, batch_size = list(map(int, self.param.split(',')))
  31. h, w = list(map(int, self.size.split(',')))
  32. img_reconstructed = reconstruction.ipca(img, n_components, batch_size)
  33. data_array = np.array(img_reconstructed, 'uint8')
  34. img_array = Image.fromarray(data_array)
  35. img_array.thumbnail((h, w))
  36. data = np.array(img_array)
  37. if self.transformation == 'fast_ica_reconstruction':
  38. n_components = self.param
  39. h, w = list(map(int, self.size.split(',')))
  40. img_reconstructed = reconstruction.fast_ica(img, n_components)
  41. data_array = np.array(img_reconstructed, 'uint8')
  42. img_array = Image.fromarray(data_array)
  43. img_array.thumbnail((h, w))
  44. data = np.array(img_array)
  45. if self.transformation == 'sobel_based_filter':
  46. k_size, p_limit = list(map(int, self.param.split(',')))
  47. h, w = list(map(int, self.size.split(',')))
  48. lab_img = transform.get_LAB_L(img)
  49. weight, height = lab_img.shape
  50. sobelx = cv2.Sobel(lab_img, cv2.CV_64F, 1, 0, ksize=k_size)
  51. sobely = cv2.Sobel(lab_img, cv2.CV_64F, 0, 1,ksize=k_size)
  52. sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
  53. sobel_mag_limit = functions.remove_pixel(sobel_mag, p_limit)
  54. # use distribution value of pixel to fill `0` values
  55. sobel_mag_limit_without_0 = [x for x in sobel_mag_limit.reshape((weight*height)) if x != 0]
  56. distribution = functions.distribution_from_data(sobel_mag_limit_without_0)
  57. min_value = int(min(sobel_mag_limit_without_0))
  58. l = lambda: functions.get_random_value(distribution) + min_value
  59. img_reconstructed = functions.fill_image_with_rand_value(sobel_mag_limit, l, 0)
  60. img_reconstructed_norm = utils.normalize_2D_arr(img_reconstructed)
  61. img_reconstructed_norm = np.array(img_reconstructed_norm*255, 'uint8')
  62. sobel_reconstructed = Image.fromarray(img_reconstructed_norm)
  63. sobel_reconstructed.thumbnail((h, w))
  64. data = np.array(sobel_reconstructed)
  65. if self.transformation == 'nl_mean_noise_mask':
  66. patch_size, patch_distance = list(map(int, self.param.split(',')))
  67. h, w = list(map(int, self.size.split(',')))
  68. img = np.array(img)
  69. sigma_est = np.mean(estimate_sigma(img, multichannel=True))
  70. patch_kw = dict(patch_size=patch_size, # 5x5 patches
  71. patch_distance=patch_distance, # 13x13 search area
  72. multichannel=True)
  73. # slow algorithm
  74. denoise = denoise_nl_means(img, h=0.8 * sigma_est, sigma=sigma_est,
  75. fast_mode=False,
  76. **patch_kw)
  77. denoise = np.array(denoise, 'uint8')
  78. noise_mask = np.abs(denoise - img)
  79. data_array = np.array(noise_mask, 'uint8')
  80. img_array = Image.fromarray(data_array)
  81. img_array.thumbnail((h, w))
  82. data = np.array(img_array)
  83. if self.transformation == 'static':
  84. # static content, we keep input as it is
  85. data = img
  86. return data
  87. def getTransformationPath(self):
  88. path = self.transformation
  89. if self.transformation == 'svd_reconstruction':
  90. begin, end = list(map(int, self.param.split(',')))
  91. w, h = list(map(int, self.size.split(',')))
  92. path = os.path.join(path, str(begin) + '_' + str(end)) + '_S_' + str(w) + '_' + str(h)
  93. if self.transformation == 'ipca_reconstruction':
  94. n_components, batch_size = list(map(int, self.param.split(',')))
  95. w, h = list(map(int, self.size.split(',')))
  96. path = os.path.join(path, 'N' + str(n_components) + '_' + str(batch_size)) + '_S_' + str(w) + '_' + str(h)
  97. if self.transformation == 'fast_ica_reconstruction':
  98. n_components = self.param
  99. w, h = list(map(int, self.size.split(',')))
  100. path = os.path.join(path, 'N' + str(n_components)) + '_S_' + str(w) + '_' + str(h)
  101. if self.transformation == 'min_diff_filter':
  102. w_size, h_size, stride = list(map(int, self.param.split(',')))
  103. w, h = list(map(int, self.size.split(',')))
  104. path = os.path.join(path, 'W_' + str(w_size)) + '_' + str(h_size) + '_Stride_' + str(stride) + '_S_' + str(w) + '_' + str(h)
  105. if self.transformation == 'sobel_based_filter':
  106. k_size, p_limit = list(map(int, self.param.split(',')))
  107. h, w = list(map(int, self.size.split(',')))
  108. path = os.path.join(path, 'K_' + str(k_size)) + '_L' + str(p_limit) + '_S_' + str(w) + '_' + str(h)
  109. if self.transformation == 'nl_mean_noise_mask':
  110. patch_size, patch_distance = list(map(int, self.param.split(',')))
  111. h, w = list(map(int, self.size.split(',')))
  112. path = os.path.join(path, 'S' + str(patch_size)) + '_D' + str(patch_distance) + '_S_' + str(w) + '_' + str(h)
  113. if self.transformation == 'static':
  114. # param contains image name to find for each scene
  115. path = self.param
  116. return path
  117. def getName(self):
  118. return self.transformation
  119. def getParam(self):
  120. return self.param
  121. def __str__( self ):
  122. return self.transformation + ' transformation with parameter : ' + self.param