processing.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. """
  2. Functions to quickly extract reduced information from image
  3. """
  4. from PIL import Image
  5. from matplotlib import cm
  6. import random
  7. from skimage import color
  8. import numpy as np
  9. import ipfml.metrics as metrics
  10. import cv2
  11. from scipy import signal
  12. def get_LAB_L_SVD(image):
  13. """Returns Singular values from LAB L Image information
  14. Args:
  15. image: PIL Image or Numpy array
  16. Returns:
  17. U, s, V information obtained from SVD compression using Lab
  18. Example:
  19. >>> from PIL import Image
  20. >>> from ipfml import processing
  21. >>> img = Image.open('./images/test_img.png')
  22. >>> U, s, V = processing.get_LAB_L_SVD(img)
  23. >>> U.shape
  24. (200, 200)
  25. >>> len(s)
  26. 200
  27. >>> V.shape
  28. (200, 200)
  29. """
  30. L = metrics.get_LAB_L(image)
  31. return metrics.get_SVD(L)
  32. def get_LAB_L_SVD_s(image):
  33. """Returns s (Singular values) SVD from L of LAB Image information
  34. Args:
  35. image: PIL Image or Numpy array
  36. Returns:
  37. vector of singular values
  38. Example:
  39. >>> from PIL import Image
  40. >>> from ipfml import processing
  41. >>> img = Image.open('./images/test_img.png')
  42. >>> s = processing.get_LAB_L_SVD_s(img)
  43. >>> len(s)
  44. 200
  45. """
  46. L = metrics.get_LAB_L(image)
  47. return metrics.get_SVD_s(L)
  48. def get_LAB_L_SVD_U(image):
  49. """Returns U SVD from L of LAB Image information
  50. Args:
  51. image: PIL Image or Numpy array
  52. Returns:
  53. U matrix of SVD compression
  54. Example:
  55. >>> from PIL import Image
  56. >>> from ipfml import processing
  57. >>> img = Image.open('./images/test_img.png')
  58. >>> U = processing.get_LAB_L_SVD_U(img)
  59. >>> U.shape
  60. (200, 200)
  61. """
  62. L = metrics.get_LAB_L(image)
  63. return metrics.get_SVD_U(L)
  64. def get_LAB_L_SVD_V(image):
  65. """Returns V SVD from L of LAB Image information
  66. Args:
  67. image: PIL Image or Numpy array
  68. Returns:
  69. V matrix of SVD compression
  70. Example:
  71. >>> from PIL import Image
  72. >>> from ipfml import processing
  73. >>> img = Image.open('./images/test_img.png')
  74. >>> V = processing.get_LAB_L_SVD_V(img)
  75. >>> V.shape
  76. (200, 200)
  77. """
  78. L = metrics.get_LAB_L(image)
  79. return metrics.get_SVD_V(L)
  80. def divide_in_blocks(image, block_size, pil=True):
  81. '''Divide image into equal size blocks
  82. Args:
  83. image: PIL Image or Numpy array
  84. block: tuple (width, height) representing the size of each dimension of the block
  85. pil: block type returned (default True)
  86. Returns:
  87. list containing all 2D Numpy blocks (in RGB or not)
  88. Raises:
  89. ValueError: If `image_width` or `image_heigt` are not compatible to produce correct block sizes
  90. Example:
  91. >>> import numpy as np
  92. >>> from PIL import Image
  93. >>> from ipfml import processing
  94. >>> from ipfml import metrics
  95. >>> image_values = np.random.randint(255, size=(800, 800, 3))
  96. >>> blocks = divide_in_blocks(image_values, (20, 20))
  97. >>> len(blocks)
  98. 1600
  99. >>> blocks[0].width
  100. 20
  101. >>> blocks[0].height
  102. 20
  103. >>> img_l = Image.open('./images/test_img.png')
  104. >>> L = metrics.get_LAB_L(img_l)
  105. >>> blocks_L = divide_in_blocks(L, (100, 100))
  106. >>> len(blocks_L)
  107. 4
  108. >>> blocks_L[0].width
  109. 100
  110. '''
  111. blocks = []
  112. mode = 'RGB'
  113. # convert in Numpy array
  114. image_array = np.array(image)
  115. # check dimension of input image
  116. if image_array.ndim != 3:
  117. mode = 'L'
  118. image_width, image_height = image_array.shape
  119. else:
  120. image_width, image_height, _ = image_array.shape
  121. # check size compatibility
  122. width, height = block_size
  123. if (image_width % width != 0):
  124. raise ValueError("Width size issue, block size not compatible")
  125. if (image_height % height != 0):
  126. raise ValueError("Height size issue, block size not compatible")
  127. nb_block_width = image_width / width
  128. nb_block_height = image_height / height
  129. for i in range(int(nb_block_width)):
  130. begin_x = i * width
  131. for j in range(int(nb_block_height)):
  132. begin_y = j * height
  133. # getting sub block information
  134. current_block = image_array[begin_x:(begin_x + width), begin_y:(
  135. begin_y + height)]
  136. if pil:
  137. blocks.append(
  138. Image.fromarray(current_block.astype('uint8'), mode))
  139. else:
  140. blocks.append(current_block)
  141. return blocks
  142. def rgb_to_mscn(image):
  143. """Convert RGB Image into Mean Subtracted Contrast Normalized (MSCN)
  144. Args:
  145. image: 3D RGB image Numpy array or PIL RGB image
  146. Returns:
  147. 2D Numpy array with MSCN information
  148. Example:
  149. >>> from PIL import Image
  150. >>> from ipfml import processing
  151. >>> img = Image.open('./images/test_img.png')
  152. >>> img_mscn = processing.rgb_to_mscn(img)
  153. >>> img_mscn.shape
  154. (200, 200)
  155. """
  156. # check if PIL image or not
  157. img_arr = np.array(image)
  158. # convert rgb image to gray
  159. im = np.array(color.rgb2gray(img_arr) * 255, 'uint8')
  160. return metrics.gray_to_mscn(im)
  161. def rgb_to_grey_low_bits(image, nb_bits=4):
  162. """Convert RGB Image into grey image using only 4 low bits values
  163. Args:
  164. image: 3D RGB image Numpy array or PIL RGB image
  165. nb_bits: optional parameter which indicates the number of bits to keep (default 4)
  166. Returns:
  167. 2D Numpy array with low bits information kept
  168. Example:
  169. >>> from PIL import Image
  170. >>> from ipfml import processing
  171. >>> img = Image.open('./images/test_img.png')
  172. >>> low_bits_grey_img = processing.rgb_to_grey_low_bits(img, 5)
  173. >>> low_bits_grey_img.shape
  174. (200, 200)
  175. """
  176. img_arr = np.array(image)
  177. grey_block = np.array(color.rgb2gray(img_arr) * 255, 'uint8')
  178. return metrics.get_low_bits_img(grey_block, nb_bits)
  179. def rgb_to_LAB_L_low_bits(image, nb_bits=4):
  180. """Convert RGB Image into Lab L channel image using only 4 low bits values
  181. Args:
  182. image: 3D RGB image Numpy array or PIL RGB image
  183. nb_bits: optional parameter which indicates the number of bits to keep (default 4)
  184. Returns:
  185. 2D Numpy array with low bits information kept
  186. Example:
  187. >>> from PIL import Image
  188. >>> from ipfml import processing
  189. >>> img = Image.open('./images/test_img.png')
  190. >>> low_bits_Lab_l_img = processing.rgb_to_LAB_L_low_bits(img, 5)
  191. >>> low_bits_Lab_l_img.shape
  192. (200, 200)
  193. """
  194. L_block = np.asarray(metrics.get_LAB_L(image), 'uint8')
  195. return metrics.get_low_bits_img(L_block, nb_bits)
  196. def rgb_to_LAB_L_bits(image, interval):
  197. """Returns only bits from LAB L canal specified into the interval
  198. Args:
  199. image: image to convert using this interval of bits value to keep
  200. interval: (begin, end) of bits values
  201. Returns:
  202. 2D Numpy array with reduced values
  203. >>> from PIL import Image
  204. >>> from ipfml import processing
  205. >>> img = Image.open('./images/test_img.png')
  206. >>> bits_Lab_l_img = processing.rgb_to_LAB_L_bits(img, (2, 6))
  207. >>> bits_Lab_l_img.shape
  208. (200, 200)
  209. """
  210. L_block = np.asarray(metrics.get_LAB_L(image), 'uint8')
  211. return metrics.get_bits_img(L_block, interval)