metrics.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. import cv2
  8. def get_image_path(image):
  9. """
  10. @brief Returns file path of PIL Image
  11. @param PIL Image
  12. @return image path
  13. >>> from PIL import Image
  14. >>> from ipfml import metrics
  15. >>> img = Image.open('./images/test_img.png')
  16. >>> path = metrics.get_image_path(img)
  17. >>> 'images/test_img.png' in path
  18. True
  19. """
  20. if hasattr(image, 'filename'):
  21. file_path = image.filename
  22. else:
  23. raise Exception("Image provided is not correct, required filename property...")
  24. return file_path
  25. def get_SVD(image):
  26. """
  27. @brief Transforms Image into SVD
  28. @param image to convert
  29. @return U, s, V image decomposition
  30. Usage :
  31. >>> from PIL import Image
  32. >>> from ipfml import metrics
  33. >>> img = Image.open('./images/test_img.png')
  34. >>> U, s, V = metrics.get_SVD(img)
  35. >>> U.shape
  36. (200, 200, 3)
  37. >>> len(s)
  38. 200
  39. >>> V.shape
  40. (200, 3, 3)
  41. """
  42. return svd(image, full_matrices=False)
  43. def get_SVD_s(image):
  44. """
  45. @brief Transforms Image into SVD and returns only 's' part
  46. @param image to convert
  47. @return s
  48. Usage :
  49. >>> from PIL import Image
  50. >>> from ipfml import metrics
  51. >>> img = Image.open('./images/test_img.png')
  52. >>> s = metrics.get_SVD_s(img)
  53. >>> len(s)
  54. 200
  55. """
  56. U, s, V = svd(image, full_matrices=False)
  57. return s
  58. def get_SVD_U(image):
  59. """
  60. @brief Transforms Image into SVD and returns only 'U' part
  61. @param image to convert
  62. @return U
  63. Usage :
  64. >>> from PIL import Image
  65. >>> from ipfml import metrics
  66. >>> img = Image.open('./images/test_img.png')
  67. >>> U = metrics.get_SVD_U(img)
  68. >>> U.shape
  69. (200, 200, 3)
  70. """
  71. U, s, V = svd(image, full_matrices=False)
  72. return U
  73. def get_SVD_V(image):
  74. """
  75. @brief Transforms Image into SVD and returns only 'V' part
  76. @param image to convert
  77. @return V
  78. Usage :
  79. >>> from PIL import Image
  80. >>> from ipfml import metrics
  81. >>> img = Image.open('./images/test_img.png')
  82. >>> V = metrics.get_SVD_V(img)
  83. >>> V.shape
  84. (200, 3, 3)
  85. """
  86. U, s, V = svd(image, full_matrices=False)
  87. return V
  88. def get_LAB(image):
  89. """
  90. @brief Transforms PIL RGB Image into LAB
  91. @param image to convert
  92. @return Lab information
  93. Usage :
  94. >>> from PIL import Image
  95. >>> from ipfml import metrics
  96. >>> img = Image.open('./images/test_img.png')
  97. >>> Lab = metrics.get_LAB(img)
  98. >>> Lab.shape
  99. (200, 200, 3)
  100. """
  101. return color.rgb2lab(image)
  102. def get_LAB_L(image):
  103. """
  104. @brief Transforms PIL RGB Image into LAB and returns L
  105. @param image to convert
  106. @return Lab information
  107. >>> from PIL import Image
  108. >>> from ipfml import metrics
  109. >>> img = Image.open('./images/test_img.png')
  110. >>> L = metrics.get_LAB_L(img)
  111. >>> L.shape
  112. (200, 200)
  113. """
  114. lab = get_LAB(image)
  115. return lab[:, :, 0]
  116. def get_LAB_A(image):
  117. """
  118. @brief Transforms PIL RGB Image into LAB and returns A
  119. @param image to convert
  120. @return Lab information
  121. Usage :
  122. >>> from PIL import Image
  123. >>> from ipfml import metrics
  124. >>> img = Image.open('./images/test_img.png')
  125. >>> A = metrics.get_LAB_A(img)
  126. >>> A.shape
  127. (200, 200)
  128. """
  129. lab = get_LAB(image)
  130. return lab[:, :, 1]
  131. def get_LAB_B(image):
  132. """
  133. @brief Transforms PIL RGB Image into LAB and returns B
  134. @param image to convert
  135. @return Lab information
  136. Usage :
  137. >>> from PIL import Image
  138. >>> from ipfml import metrics
  139. >>> img = Image.open('./images/test_img.png')
  140. >>> B = metrics.get_LAB_B(img)
  141. >>> B.shape
  142. (200, 200)
  143. """
  144. lab = get_LAB(image)
  145. return lab[:, :, 2]
  146. def get_XYZ(image):
  147. """
  148. @brief Transforms PIL RGB Image into XYZ
  149. @param image to convert
  150. @return Lab information
  151. Usage :
  152. >>> from PIL import Image
  153. >>> from ipfml import metrics
  154. >>> img = Image.open('./images/test_img.png')
  155. >>> metrics.get_XYZ(img).shape
  156. (200, 200, 3)
  157. """
  158. return color.rgb2xyz(image)
  159. def get_XYZ_X(image):
  160. """
  161. @brief Transforms PIL RGB Image into XYZ and returns X
  162. @param image to convert
  163. @return Lab information
  164. Usage :
  165. >>> from PIL import Image
  166. >>> from ipfml import metrics
  167. >>> img = Image.open('./images/test_img.png')
  168. >>> x = metrics.get_XYZ_X(img)
  169. >>> x.shape
  170. (200, 200)
  171. """
  172. xyz = color.rgb2xyz(image)
  173. return xyz[:, :, 0]
  174. def get_XYZ_Y(image):
  175. """
  176. @brief Transforms PIL RGB Image into XYZ and returns Y
  177. @param image to convert
  178. @return Lab information
  179. Usage :
  180. >>> from PIL import Image
  181. >>> from ipfml import metrics
  182. >>> img = Image.open('./images/test_img.png')
  183. >>> y = metrics.get_XYZ_Y(img)
  184. >>> y.shape
  185. (200, 200)
  186. """
  187. xyz = color.rgb2xyz(image)
  188. return xyz[:, :, 1]
  189. def get_XYZ_Z(image):
  190. """
  191. @brief Transforms PIL RGB Image into XYZ and returns Z
  192. @param image to convert
  193. @return Lab information
  194. Usage :
  195. >>> from PIL import Image
  196. >>> from ipfml import metrics
  197. >>> img = Image.open('./images/test_img.png')
  198. >>> z = metrics.get_XYZ_Z(img)
  199. >>> z.shape
  200. (200, 200)
  201. """
  202. xyz = color.rgb2xyz(image)
  203. return xyz[:, :, 2]
  204. def get_low_bits_img(image, bind=15):
  205. """
  206. @brief Returns Image or Numpy array with data information reduced using only low bits
  207. @param image to convert
  208. @bind optional : bits to keep using & Bitwise operator
  209. @return Numpy array with reduced values
  210. Usage :
  211. >>> from PIL import Image
  212. >>> from ipfml import metrics
  213. >>> img = Image.open('./images/test_img.png')
  214. >>> low_bits_img = metrics.get_low_bits_img(img)
  215. >>> low_bits_img.shape
  216. (200, 200, 3)
  217. """
  218. img_arr = np.array(image)
  219. return img_arr & bind
  220. def gray_to_mscn(image):
  221. """
  222. @brief Convert Grayscale Image into Mean Subtracted Contrast Normalized (MSCN)
  223. @param grayscale image numpy array or PIL RGB image
  224. Usage :
  225. >>> from PIL import Image
  226. >>> from ipfml import image_processing
  227. >>> img = Image.open('./images/test_img.png')
  228. >>> img_mscn = image_processing.rgb_to_mscn(img)
  229. >>> img_mscn.shape
  230. (200, 200)
  231. """
  232. s = 7/6
  233. blurred = cv2.GaussianBlur(image, (7, 7), s) # apply gaussian blur to the image
  234. blurred_sq = blurred * blurred
  235. sigma = cv2.GaussianBlur(image * image, (7, 7), s)
  236. sigma = abs(sigma - blurred_sq) ** 0.5
  237. sigma = sigma + 1.0/255 # avoid DivideByZero Exception
  238. mscn = (image - blurred)/sigma # MSCN(i, j) image
  239. return mscn