-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_attributes.py
165 lines (108 loc) · 4.55 KB
/
data_attributes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# main imports
import numpy as np
import sys
# image transform imports
from PIL import Image
from skimage import color, restoration
from sklearn.decomposition import FastICA
from sklearn.decomposition import IncrementalPCA
from sklearn.decomposition import TruncatedSVD
from numpy.linalg import svd as lin_svd
from scipy.signal import medfilt2d, wiener, cwt
import pywt
import cv2
import gzip
from ipfml.processing import transform, compression, segmentation
from ipfml import utils
# modules and config imports
sys.path.insert(0, '') # trick to enable import of main folder module
import custom_config as cfg
from modules.utils import data as dt
def get_image_features(data_type, block):
"""
Method which returns the data type expected
"""
if 'filters_statistics' in data_type:
img_width, img_height = 200, 200
lab_img = transform.get_LAB_L(block)
arr = np.array(lab_img)
# compute all filters statistics
def get_stats(arr, I_filter):
e1 = np.abs(arr - I_filter)
L = np.array(e1)
mu0 = np.mean(L)
A = L - mu0
H = A * A
E = np.sum(H) / (img_width * img_height)
P = np.sqrt(E)
return mu0, P
# return np.mean(I_filter), np.std(I_filter)
stats = []
kernel = np.ones((3,3),np.float32)/9
stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
kernel = np.ones((5,5),np.float32)/25
stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 0.5)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1.5)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 0.5)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1)))
stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1.5)))
stats.append(get_stats(arr, medfilt2d(arr, [3, 3])))
stats.append(get_stats(arr, medfilt2d(arr, [5, 5])))
stats.append(get_stats(arr, wiener(arr, [3, 3])))
stats.append(get_stats(arr, wiener(arr, [5, 5])))
wave = w2d(arr, 'db1', 2)
stats.append(get_stats(arr, np.array(wave, 'float64')))
data = []
for stat in stats:
data.append(stat[0])
for stat in stats:
data.append(stat[1])
data = np.array(data)
if 'statistics_extended' in data_type:
data = get_image_features('filters_statistics', block)
# add kolmogorov complexity
bytes_data = np.array(block).tobytes()
compress_data = gzip.compress(bytes_data)
mo_size = sys.getsizeof(compress_data) / 1024.
go_size = mo_size / 1024.
data = np.append(data, go_size)
lab_img = transform.get_LAB_L(block)
arr = np.array(lab_img)
# add of svd entropy
svd_entropy = utils.get_entropy(compression.get_SVD_s(arr))
data = np.append(data, svd_entropy)
# add sobel complexity (kernel size of 3)
sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1,ksize=3)
sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
data = np.append(data, np.std(sobel_mag))
# add sobel complexity (kernel size of 5)
sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1,ksize=5)
sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8') # magnitude
data = np.append(data, np.std(sobel_mag))
if 'lab' in data_type:
data = transform.get_LAB_L_SVD_s(block)
return data
def w2d(arr, mode='haar', level=1):
#convert to float
imArray = arr
sigma = restoration.estimate_sigma(imArray, average_sigmas=True, multichannel=False)
imArray_H = restoration.denoise_wavelet(imArray, sigma=sigma, wavelet='db1', mode='hard',
wavelet_levels=2,
multichannel=False,
convert2ycbcr=False,
method='VisuShrink',
rescale_sigma=True)
# imArray_H *= 100
return imArray_H
def _get_mscn_variance(block, sub_block_size=(50, 50)):
blocks = segmentation.divide_in_blocks(block, sub_block_size)
data = []
for block in blocks:
mscn_coefficients = transform.get_mscn_coefficients(block)
flat_coeff = mscn_coefficients.flatten()
data.append(np.var(flat_coeff))
return np.sort(data)