Transformation.py 3.7 KB

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