reconstruction.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Functions for reconstruction process of image using reduction/compression methods
  3. """
  4. # main imports
  5. import numpy as np
  6. # image processing imports
  7. from numpy.linalg import svd as np_svd
  8. from sklearn.decomposition import FastICA, IncrementalPCA
  9. # ipfml imports
  10. from ipfml.processing import transform
  11. def svd(image, interval):
  12. """Reconstruct an image from SVD compression using specific interval of Singular Values
  13. Args:
  14. image: PIL Image, Numpy array or path of 3D image
  15. interval: Interval used for reconstruction
  16. Returns:
  17. Reconstructed image
  18. Example:
  19. >>> from PIL import Image
  20. >>> import numpy as np
  21. >>> from ipfml.processing import reconstruction
  22. >>> image_values = Image.open('./images/test_img.png')
  23. >>> reconstructed_image = reconstruction.svd(image_values, (100, 200))
  24. >>> reconstructed_image.shape
  25. (200, 200)
  26. """
  27. begin, end = interval
  28. lab_img = transform.get_LAB_L(image)
  29. lab_img = np.array(lab_img, 'uint8')
  30. U, s, V = np_svd(lab_img, full_matrices=True)
  31. # reconstruction using specific interval
  32. smat = np.zeros((end - begin, end - begin), dtype=complex)
  33. smat[:, :] = np.diag(s[begin:end])
  34. output_img = np.dot(U[:, begin:end], np.dot(smat, V[begin:end, :]))
  35. return output_img
  36. def fast_ica(image, components):
  37. """Reconstruct an image from Fast ICA compression using specific number of components to use
  38. Args:
  39. image: PIL Image, Numpy array or path of 3D image
  40. components: Number of components used for reconstruction
  41. Returns:
  42. Reconstructed image
  43. Example:
  44. >>> from PIL import Image
  45. >>> import numpy as np
  46. >>> from ipfml.processing import reconstruction
  47. >>> image_values = Image.open('./images/test_img.png')
  48. >>> reconstructed_image = reconstruction.fast_ica(image_values, 25)
  49. >>> reconstructed_image.shape
  50. (200, 200)
  51. """
  52. lab_img = transform.get_LAB_L(image)
  53. lab_img = np.array(lab_img, 'uint8')
  54. ica = FastICA(n_components=50)
  55. # run ICA on image
  56. ica.fit(lab_img)
  57. # reconstruct image with independent components
  58. image_ica = ica.fit_transform(lab_img)
  59. restored_image = ica.inverse_transform(image_ica)
  60. return restored_image
  61. def ipca(image, components, _batch_size=25):
  62. """Reconstruct an image from IPCA compression using specific number of components to use and batch size
  63. Args:
  64. image: PIL Image, Numpy array or path of 3D image
  65. components: Number of components used for reconstruction
  66. batch_size: Batch size used for learn (default 25)
  67. Returns:
  68. Reconstructed image
  69. Example:
  70. >>> from PIL import Image
  71. >>> import numpy as np
  72. >>> from ipfml.processing import reconstruction
  73. >>> image_values = Image.open('./images/test_img.png')
  74. >>> reconstructed_image = reconstruction.ipca(image_values, 20)
  75. >>> reconstructed_image.shape
  76. (200, 200)
  77. """
  78. lab_img = transform.get_LAB_L(image)
  79. lab_img = np.array(lab_img, 'uint8')
  80. transformer = IncrementalPCA(
  81. n_components=components, batch_size=_batch_size)
  82. transformed_image = transformer.fit_transform(lab_img)
  83. restored_image = transformer.inverse_transform(transformed_image)
  84. return restored_image