metrics.py 7.8 KB

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