metrics.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # module file which contains all image metrics used in project
  2. from numpy.linalg import svd
  3. from scipy import misc
  4. import numpy as np
  5. from sklearn import preprocessing
  6. from skimage import io, color
  7. def get_image_path(image):
  8. """
  9. @brief Returns file path of PIL Image
  10. @param PIL Image
  11. @return image path
  12. >>> from PIL import Image
  13. >>> from ipfml import metrics
  14. >>> img = Image.open('./images/test_img.png')
  15. >>> path = metrics.get_image_path(img)
  16. >>> 'images/test_img.png' in path
  17. True
  18. """
  19. if hasattr(image, 'filename'):
  20. file_path = image.filename
  21. else:
  22. raise Exception("Image provided is not correct, required filename property...")
  23. return file_path
  24. def get_SVD(image):
  25. """
  26. @brief Transforms Image into SVD
  27. @param image to convert
  28. @return U, s, V image decomposition
  29. Usage :
  30. >>> from PIL import Image
  31. >>> from ipfml import metrics
  32. >>> img = Image.open('./images/test_img.png')
  33. >>> U, s, V = metrics.get_SVD(img)
  34. >>> U.shape
  35. (200, 200, 3)
  36. >>> len(s)
  37. 200
  38. >>> V.shape
  39. (200, 3, 3)
  40. """
  41. return svd(image, full_matrices=False)
  42. def get_SVD_s(image):
  43. """
  44. @brief Transforms Image into SVD and returns only 's' part
  45. @param image to convert
  46. @return s
  47. Usage :
  48. >>> from PIL import Image
  49. >>> from ipfml import metrics
  50. >>> img = Image.open('./images/test_img.png')
  51. >>> s = metrics.get_SVD_s(img)
  52. >>> len(s)
  53. 200
  54. """
  55. U, s, V = svd(image, full_matrices=False)
  56. return s
  57. def get_SVD_U(image):
  58. """
  59. @brief Transforms Image into SVD and returns only 'U' part
  60. @param image to convert
  61. @return U
  62. Usage :
  63. >>> from PIL import Image
  64. >>> from ipfml import metrics
  65. >>> img = Image.open('./images/test_img.png')
  66. >>> Lab = metrics.get_LAB(img)
  67. >>> Lab.shape
  68. (200, 200, 3)
  69. """
  70. U, s, V = svd(image, full_matrices=False)
  71. return U
  72. def get_SVD_V(image):
  73. """
  74. @brief Transforms Image into SVD and returns only 'V' part
  75. @param image to convert
  76. @return V
  77. Usage :
  78. >>> from PIL import Image
  79. >>> from ipfml import metrics
  80. >>> img = Image.open('./images/test_img.png')
  81. >>> V = metrics.get_SVD_V(img)
  82. >>> V.shape
  83. (200, 3, 3)
  84. """
  85. U, s, V = svd(image, full_matrices=False)
  86. return V
  87. def get_LAB(image):
  88. """
  89. @brief Transforms PIL RGB Image into LAB
  90. @param image to convert
  91. @return Lab information
  92. Usage :
  93. >>> from PIL import Image
  94. >>> from ipfml import metrics
  95. >>> img = Image.open('./images/test_img.png')
  96. >>> Lab = metrics.get_LAB(img)
  97. >>> Lab.shape
  98. (200, 200, 3)
  99. """
  100. file_path = get_image_path(image)
  101. rgb = io.imread(file_path)
  102. return color.rgb2lab(rgb)
  103. def get_LAB_L(image):
  104. """
  105. @brief Transforms PIL RGB Image into LAB and returns L
  106. @param image to convert
  107. @return Lab information
  108. >>> from PIL import Image
  109. >>> from ipfml import metrics
  110. >>> img = Image.open('./images/test_img.png')
  111. >>> L = metrics.get_LAB_L(img)
  112. >>> L.shape
  113. (200, 200)
  114. """
  115. file_path = get_image_path(image)
  116. rgb = io.imread(file_path)
  117. lab = color.rgb2lab(rgb)
  118. return lab[:, :, 0]
  119. def get_LAB_A(image):
  120. """
  121. @brief Transforms PIL RGB Image into LAB and returns A
  122. @param image to convert
  123. @return Lab information
  124. Usage :
  125. >>> from PIL import Image
  126. >>> from ipfml import metrics
  127. >>> img = Image.open('./images/test_img.png')
  128. >>> A = metrics.get_LAB_A(img)
  129. >>> A.shape
  130. (200, 200)
  131. """
  132. file_path = get_image_path(image)
  133. rgb = io.imread(file_path)
  134. lab = color.rgb2lab(rgb)
  135. return lab[:, :, 1]
  136. def get_LAB_B(image):
  137. """
  138. @brief Transforms PIL RGB Image into LAB and returns B
  139. @param image to convert
  140. @return Lab information
  141. Usage :
  142. >>> from PIL import Image
  143. >>> from ipfml import metrics
  144. >>> img = Image.open('./images/test_img.png')
  145. >>> B = metrics.get_LAB_B(img)
  146. >>> B.shape
  147. (200, 200)
  148. """
  149. file_path = get_image_path(image)
  150. rgb = io.imread(file_path)
  151. lab = color.rgb2lab(rgb)
  152. return lab[:, :, 2]
  153. def get_XYZ(image):
  154. """
  155. @brief Transforms PIL RGB Image into XYZ
  156. @param image to convert
  157. @return Lab information
  158. Usage :
  159. >>> from PIL import Image
  160. >>> from ipfml import metrics
  161. >>> img = Image.open('./images/test_img.png')
  162. >>> metrics.get_XYZ(img).shape
  163. (200, 200, 3)
  164. """
  165. file_path = get_image_path(image)
  166. rgb = io.imread(file_path)
  167. return color.rgb2xyz(rgb)
  168. def get_XYZ_X(image):
  169. """
  170. @brief Transforms PIL RGB Image into XYZ and returns X
  171. @param image to convert
  172. @return Lab information
  173. Usage :
  174. >>> from PIL import Image
  175. >>> from ipfml import metrics
  176. >>> img = Image.open('./images/test_img.png')
  177. >>> x = metrics.get_XYZ_X(img)
  178. >>> x.shape
  179. (200, 200)
  180. """
  181. file_path = get_image_path(image)
  182. rgb = io.imread(file_path)
  183. xyz = color.rgb2xyz(rgb)
  184. return xyz[:, :, 0]
  185. def get_XYZ_Y(image):
  186. """
  187. @brief Transforms PIL RGB Image into XYZ and returns Y
  188. @param image to convert
  189. @return Lab information
  190. Usage :
  191. >>> from PIL import Image
  192. >>> from ipfml import metrics
  193. >>> img = Image.open('./images/test_img.png')
  194. >>> y = metrics.get_XYZ_Y(img)
  195. >>> y.shape
  196. (200, 200)
  197. """
  198. file_path = get_image_path(image)
  199. rgb = io.imread(file_path)
  200. xyz = color.rgb2xyz(rgb)
  201. return xyz[:, :, 1]
  202. def get_XYZ_Z(image):
  203. """
  204. @brief Transforms PIL RGB Image into XYZ and returns Z
  205. @param image to convert
  206. @return Lab information
  207. Usage :
  208. >>> from PIL import Image
  209. >>> from ipfml import metrics
  210. >>> img = Image.open('./images/test_img.png')
  211. >>> z = metrics.get_XYZ_Z(img)
  212. >>> z.shape
  213. (200, 200)
  214. """
  215. file_path = get_image_path(image)
  216. rgb = io.imread(file_path)
  217. xyz = color.rgb2xyz(rgb)
  218. return xyz[:, :, 2]