Transformation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. data = reconstruction.svd(img, [begin, end])
  20. if self.transformation == 'ipca_reconstruction':
  21. n_components, batch_size = list(map(int, self.param.split(',')))
  22. data = reconstruction.ipca(img, n_components, batch_size)
  23. if self.transformation == 'fast_ica_reconstruction':
  24. n_components = self.param
  25. data = reconstruction.fast_ica(img, n_components)
  26. if self.transformation == 'min_diff_filter':
  27. w_size, h_size = list(map(int, self.param.split(',')))
  28. h, w = list(map(int, self.size.split(',')))
  29. # bilateral with window of size (`w_size`, `h_size`)
  30. lab_img = transform.get_LAB_L(img)
  31. lab_img = Image.fromarray(lab_img)
  32. lab_img.thumbnail((h, w))
  33. diff_img = convolution.convolution2D(lab_img, kernels.min_bilateral_diff, (w_size, h_size))
  34. data = np.array(diff_img*255, 'uint8')
  35. if self.transformation == 'static':
  36. # static content, we keep input as it is
  37. data = img
  38. return data
  39. def getTransformationPath(self):
  40. path = self.transformation
  41. if self.transformation == 'svd_reconstruction':
  42. begin, end = list(map(int, self.param.split(',')))
  43. path = os.path.join(path, str(begin) + '_' + str(end))
  44. if self.transformation == 'ipca_reconstruction':
  45. n_components, batch_size = list(map(int, self.param.split(',')))
  46. path = os.path.join(path, 'N' + str(n_components) + '_' + str(batch_size))
  47. if self.transformation == 'fast_ica_reconstruction':
  48. n_components = self.param
  49. path = os.path.join(path, 'N' + str(n_components))
  50. if self.transformation == 'min_diff_filter':
  51. w_size, h_size = list(map(int, self.param.split(',')))
  52. w, h = list(map(int, self.size.split(',')))
  53. path = os.path.join(path, 'W_' + str(w_size)) + '_' + str(h_size) + '_S_' + str(w) + '_' + str(h)
  54. if self.transformation == 'static':
  55. # param contains image name to find for each scene
  56. path = self.param
  57. return path
  58. def getName(self):
  59. return self.transformation
  60. def getParam(self):
  61. return self.param
  62. def __str__( self ):
  63. return self.transformation + ' transformation with parameter : ' + self.param