Transformation.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. from ipfml.processing import transform
  3. from ipfml.processing import reconstruction
  4. from ipfml.filters import convolution, kernels
  5. from ipfml import utils
  6. # Transformation class to store transformation method of image and get usefull information
  7. class Transformation():
  8. def __init__(self, _transformation, _param):
  9. self.transformation = _transformation
  10. self.param = _param
  11. def getTransformedImage(self, img):
  12. if self.transformation == 'svd_reconstruction':
  13. begin, end = list(map(int, self.param.split(',')))
  14. data = reconstruction.svd(img, [begin, end])
  15. if self.transformation == 'ipca_reconstruction':
  16. n_components, batch_size = list(map(int, self.param.split(',')))
  17. data = reconstruction.ipca(img, n_components, batch_size)
  18. if self.transformation == 'fast_ica_reconstruction':
  19. n_components = self.param
  20. data = reconstruction.fast_ica(img, n_components)
  21. if self.transformation == 'min_diff_filter':
  22. w_size, h_size = list(map(int, self.param.split(',')))
  23. # bilateral with window of size (`w_size`, `h_size`)
  24. lab_img = transform.get_LAB_L(img)
  25. data = convolution.convolution2D(lab_img, kernels.min_bilateral_diff, (w_size, h_size))
  26. if self.transformation == 'static':
  27. # static content, we keep input as it is
  28. data = img
  29. return data
  30. def getTransformationPath(self):
  31. path = self.transformation
  32. if self.transformation == 'svd_reconstruction':
  33. begin, end = list(map(int, self.param.split(',')))
  34. path = os.path.join(path, str(begin) + '_' + str(end))
  35. if self.transformation == 'ipca_reconstruction':
  36. n_components, batch_size = list(map(int, self.param.split(',')))
  37. path = os.path.join(path, 'N' + str(n_components) + '_' + str(batch_size))
  38. if self.transformation == 'fast_ica_reconstruction':
  39. n_components = self.param
  40. path = os.path.join(path, 'N' + str(n_components))
  41. if self.transformation == 'min_diff_filter':
  42. w_size, h_size = list(map(int, self.param.split(',')))
  43. path = os.path.join(path, 'W_' + str(w_size)) + '_' + str(h_size)
  44. if self.transformation == 'static':
  45. # param contains image name to find for each scene
  46. path = self.param
  47. return path
  48. def getName(self):
  49. return self.transformation
  50. def getParam(self):
  51. return self.param
  52. def __str__( self ):
  53. return self.transformation + ' transformation with parameter : ' + self.param