Transformation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from PIL import Image
  10. # Transformation class to store transformation method of image and get usefull information
  11. class Transformation():
  12. def __init__(self, _transformation, _param, _size):
  13. self.transformation = _transformation
  14. self.param = _param
  15. self.size = _size
  16. def getTransformedImage(self, img):
  17. if self.transformation == 'svd_reconstruction':
  18. begin, end = list(map(int, self.param.split(',')))
  19. h, w = list(map(int, self.size.split(',')))
  20. img_reconstructed = reconstruction.svd(img, [begin, end])
  21. data_array = np.array(img_reconstructed, 'uint8')
  22. data = np.array(Image.fromarray(data_array).thumbnail((h, w)))
  23. if self.transformation == 'ipca_reconstruction':
  24. n_components, batch_size = list(map(int, self.param.split(',')))
  25. h, w = list(map(int, self.size.split(',')))
  26. img_reconstructed = reconstruction.ipca(img, n_components, batch_size)
  27. data_array = np.array(img_reconstructed, 'uint8')
  28. data = np.array(Image.fromarray(data_array).thumbnail((h, w)))
  29. if self.transformation == 'fast_ica_reconstruction':
  30. n_components = self.param
  31. h, w = list(map(int, self.size.split(',')))
  32. img_reconstructed = reconstruction.fast_ica(img, n_components)
  33. data_array = np.array(img_reconstructed, 'uint8')
  34. data = np.array(Image.fromarray(data_array).thumbnail((h, w)))
  35. if self.transformation == 'min_diff_filter':
  36. w_size, h_size = list(map(int, self.param.split(',')))
  37. h, w = list(map(int, self.size.split(',')))
  38. # bilateral with window of size (`w_size`, `h_size`)
  39. lab_img = transform.get_LAB_L(img)
  40. lab_img = Image.fromarray(lab_img)
  41. lab_img.thumbnail((h, w))
  42. diff_img = convolution.convolution2D(lab_img, kernels.min_bilateral_diff, (w_size, h_size))
  43. data = np.array(diff_img*255, 'uint8')
  44. if self.transformation == 'static':
  45. # static content, we keep input as it is
  46. data = img
  47. return data
  48. def getTransformationPath(self):
  49. path = self.transformation
  50. if self.transformation == 'svd_reconstruction':
  51. begin, end = list(map(int, self.param.split(',')))
  52. w, h = list(map(int, self.size.split(',')))
  53. path = os.path.join(path, str(begin) + '_' + str(end)) + '_S_' + str(w) + '_' + str(h)
  54. if self.transformation == 'ipca_reconstruction':
  55. n_components, batch_size = list(map(int, self.param.split(',')))
  56. w, h = list(map(int, self.size.split(',')))
  57. path = os.path.join(path, 'N' + str(n_components) + '_' + str(batch_size)) + '_S_' + str(w) + '_' + str(h)
  58. if self.transformation == 'fast_ica_reconstruction':
  59. n_components = self.param
  60. w, h = list(map(int, self.size.split(',')))
  61. path = os.path.join(path, 'N' + str(n_components)) + '_S_' + str(w) + '_' + str(h)
  62. if self.transformation == 'min_diff_filter':
  63. w_size, h_size = list(map(int, self.param.split(',')))
  64. w, h = list(map(int, self.size.split(',')))
  65. path = os.path.join(path, 'W_' + str(w_size)) + '_' + str(h_size) + '_S_' + str(w) + '_' + str(h)
  66. if self.transformation == 'static':
  67. # param contains image name to find for each scene
  68. path = self.param
  69. return path
  70. def getName(self):
  71. return self.transformation
  72. def getParam(self):
  73. return self.param
  74. def __str__( self ):
  75. return self.transformation + ' transformation with parameter : ' + self.param