data_attributes.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # main imports
  2. import numpy as np
  3. import sys
  4. # image transform imports
  5. from PIL import Image
  6. from skimage import color, restoration
  7. from sklearn.decomposition import FastICA
  8. from sklearn.decomposition import IncrementalPCA
  9. from sklearn.decomposition import TruncatedSVD
  10. from numpy.linalg import svd as lin_svd
  11. from scipy.signal import medfilt2d, wiener, cwt
  12. import pywt
  13. import cv2
  14. import gzip
  15. from ipfml.processing import transform, compression, segmentation
  16. from ipfml import utils
  17. # modules and config imports
  18. sys.path.insert(0, '') # trick to enable import of main folder module
  19. import custom_config as cfg
  20. from modules.utils import data as dt
  21. def get_image_features(data_type, block):
  22. """
  23. Method which returns the data type expected
  24. """
  25. if 'filters_statistics' in data_type:
  26. img_width, img_height = 200, 200
  27. lab_img = transform.get_LAB_L(block)
  28. arr = np.array(lab_img)
  29. # compute all filters statistics
  30. def get_stats(arr, I_filter):
  31. e1 = np.abs(arr - I_filter)
  32. L = np.array(e1)
  33. mu0 = np.mean(L)
  34. A = L - mu0
  35. H = A * A
  36. E = np.sum(H) / (img_width * img_height)
  37. P = np.sqrt(E)
  38. return mu0, P
  39. # return np.mean(I_filter), np.std(I_filter)
  40. stats = []
  41. kernel = np.ones((3,3),np.float32)/9
  42. stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
  43. kernel = np.ones((5,5),np.float32)/25
  44. stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
  45. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 0.5)))
  46. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1)))
  47. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1.5)))
  48. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 0.5)))
  49. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1)))
  50. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1.5)))
  51. stats.append(get_stats(arr, medfilt2d(arr, [3, 3])))
  52. stats.append(get_stats(arr, medfilt2d(arr, [5, 5])))
  53. stats.append(get_stats(arr, wiener(arr, [3, 3])))
  54. stats.append(get_stats(arr, wiener(arr, [5, 5])))
  55. wave = w2d(arr, 'db1', 2)
  56. stats.append(get_stats(arr, np.array(wave, 'float64')))
  57. data = []
  58. for stat in stats:
  59. data.append(stat[0])
  60. for stat in stats:
  61. data.append(stat[1])
  62. data = np.array(data)
  63. if 'statistics_extended' in data_type:
  64. data = get_image_features('filters_statistics', block)
  65. # add kolmogorov complexity
  66. bytes_data = np.array(block).tobytes()
  67. compress_data = gzip.compress(bytes_data)
  68. mo_size = sys.getsizeof(compress_data) / 1024.
  69. go_size = mo_size / 1024.
  70. data = np.append(data, go_size)
  71. lab_img = transform.get_LAB_L(block)
  72. arr = np.array(lab_img)
  73. # add of svd entropy
  74. svd_entropy = utils.get_entropy(compression.get_SVD_s(arr))
  75. data = np.append(data, svd_entropy)
  76. # add sobel complexity (kernel size of 3)
  77. sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=3)
  78. sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1,ksize=3)
  79. sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
  80. data = np.append(data, np.std(sobel_mag))
  81. # add sobel complexity (kernel size of 5)
  82. sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=5)
  83. sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1,ksize=5)
  84. sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
  85. data = np.append(data, np.std(sobel_mag))
  86. if 'lab' in data_type:
  87. data = transform.get_LAB_L_SVD_s(block)
  88. return data
  89. def w2d(arr, mode='haar', level=1):
  90. #convert to float
  91. imArray = arr
  92. sigma = restoration.estimate_sigma(imArray, average_sigmas=True, multichannel=False)
  93. imArray_H = restoration.denoise_wavelet(imArray, sigma=sigma, wavelet='db1', mode='hard',
  94. wavelet_levels=2,
  95. multichannel=False,
  96. convert2ycbcr=False,
  97. method='VisuShrink',
  98. rescale_sigma=True)
  99. # imArray_H *= 100
  100. return imArray_H
  101. def _get_mscn_variance(block, sub_block_size=(50, 50)):
  102. blocks = segmentation.divide_in_blocks(block, sub_block_size)
  103. data = []
  104. for block in blocks:
  105. mscn_coefficients = transform.get_mscn_coefficients(block)
  106. flat_coeff = mscn_coefficients.flatten()
  107. data.append(np.var(flat_coeff))
  108. return np.sort(data)