Transformation.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 PIL import Image
  11. # modules imports
  12. from ..utils import data as functions
  13. # Transformation class to store transformation method of image and get usefull information
  14. class Transformation():
  15. def __init__(self, _transformation, _param, _size):
  16. self.transformation = _transformation
  17. self.param = _param
  18. self.size = _size
  19. def getTransformedImage(self, img):
  20. if self.transformation == 'svd_reconstruction':
  21. begin, end = list(map(int, self.param.split(',')))
  22. h, w = list(map(int, self.size.split(',')))
  23. img_reconstructed = reconstruction.svd(img, [begin, end])
  24. data_array = np.array(img_reconstructed, 'uint8')
  25. img_array = Image.fromarray(data_array)
  26. img_array.thumbnail((h, w))
  27. data = np.array(img_array)
  28. if self.transformation == 'ipca_reconstruction':
  29. n_components, batch_size = list(map(int, self.param.split(',')))
  30. h, w = list(map(int, self.size.split(',')))
  31. img_reconstructed = reconstruction.ipca(img, n_components, batch_size)
  32. data_array = np.array(img_reconstructed, 'uint8')
  33. img_array = Image.fromarray(data_array)
  34. img_array.thumbnail((h, w))
  35. data = np.array(img_array)
  36. if self.transformation == 'fast_ica_reconstruction':
  37. n_components = self.param
  38. h, w = list(map(int, self.size.split(',')))
  39. img_reconstructed = reconstruction.fast_ica(img, n_components)
  40. data_array = np.array(img_reconstructed, 'uint8')
  41. img_array = Image.fromarray(data_array)
  42. img_array.thumbnail((h, w))
  43. data = np.array(img_array)
  44. if self.transformation == 'sobel_based_filter':
  45. k_size, p_limit = list(map(int, self.param.split(',')))
  46. h, w = list(map(int, self.size.split(',')))
  47. lab_img = transform.get_LAB_L(img)
  48. weight, height = lab_img.shape
  49. sobelx = cv2.Sobel(lab_img, cv2.CV_64F, 1, 0, ksize=k_size)
  50. sobely = cv2.Sobel(lab_img, cv2.CV_64F, 0, 1,ksize=k_size)
  51. sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
  52. sobel_mag_limit = functions.remove_pixel(sobel_mag, p_limit)
  53. # use distribution value of pixel to fill `0` values
  54. sobel_mag_limit_without_0 = [x for x in sobel_mag_limit.reshape((weight*height)) if x != 0]
  55. distribution = functions.distribution_from_data(sobel_mag_limit_without_0)
  56. min_value = int(min(sobel_mag_limit_without_0))
  57. l = lambda: functions.get_random_value(distribution) + min_value
  58. img_reconstructed = functions.fill_image_with_rand_value(sobel_mag_limit, l, 0)
  59. img_reconstructed_norm = utils.normalize_2D_arr(img_reconstructed)
  60. img_reconstructed_norm = np.array(img_reconstructed_norm*255, 'uint8')
  61. sobel_reconstructed = Image.fromarray(img_reconstructed_norm)
  62. sobel_reconstructed.thumbnail((h, w))
  63. data = np.array(sobel_reconstructed)
  64. if self.transformation == 'static':
  65. # static content, we keep input as it is
  66. data = img
  67. return data
  68. def getTransformationPath(self):
  69. path = self.transformation
  70. if self.transformation == 'svd_reconstruction':
  71. begin, end = list(map(int, self.param.split(',')))
  72. w, h = list(map(int, self.size.split(',')))
  73. path = os.path.join(path, str(begin) + '_' + str(end)) + '_S_' + str(w) + '_' + str(h)
  74. if self.transformation == 'ipca_reconstruction':
  75. n_components, batch_size = list(map(int, self.param.split(',')))
  76. w, h = list(map(int, self.size.split(',')))
  77. path = os.path.join(path, 'N' + str(n_components) + '_' + str(batch_size)) + '_S_' + str(w) + '_' + str(h)
  78. if self.transformation == 'fast_ica_reconstruction':
  79. n_components = self.param
  80. w, h = list(map(int, self.size.split(',')))
  81. path = os.path.join(path, 'N' + str(n_components)) + '_S_' + str(w) + '_' + str(h)
  82. if self.transformation == 'min_diff_filter':
  83. w_size, h_size, stride = list(map(int, self.param.split(',')))
  84. w, h = list(map(int, self.size.split(',')))
  85. path = os.path.join(path, 'W_' + str(w_size)) + '_' + str(h_size) + '_Stride_' + str(stride) + '_S_' + str(w) + '_' + str(h)
  86. if self.transformation == 'sobel_based_filter':
  87. k_size, p_limit = list(map(int, self.param.split(',')))
  88. h, w = list(map(int, self.size.split(',')))
  89. path = os.path.join(path, 'K_' + str(k_size)) + '_L' + str(p_limit) + '_S_' + str(w) + '_' + str(h)
  90. if self.transformation == 'static':
  91. # param contains image name to find for each scene
  92. path = self.param
  93. return path
  94. def getName(self):
  95. return self.transformation
  96. def getParam(self):
  97. return self.param
  98. def __str__( self ):
  99. return self.transformation + ' transformation with parameter : ' + self.param