-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_attributes.py
More file actions
209 lines (137 loc) · 5.63 KB
/
Copy pathdata_attributes.py
File metadata and controls
209 lines (137 loc) · 5.63 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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
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 'Constantin2015' 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')
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 'Constantin2016' in data_type:
img_width, img_height = 200, 200
lab_img = transform.get_LAB_L(block)
arr = np.array(lab_img)
stats = []
kernel = np.ones((3,3),np.float32)/9
arr = cv2.filter2D(arr,-1,kernel)
kernel = np.ones((5,5),np.float32)/25
arr = cv2.filter2D(arr,-1,kernel)
arr = cv2.GaussianBlur(arr, (3, 3), 0.5)
arr = cv2.GaussianBlur(arr, (3, 3), 1)
arr = cv2.GaussianBlur(arr, (3, 3), 1.5)
arr = cv2.GaussianBlur(arr, (5, 5), 0.5)
arr = cv2.GaussianBlur(arr, (5, 5), 1)
arr = cv2.GaussianBlur(arr, (5, 5), 1.5)
arr = medfilt2d(arr, [3, 3])
arr = medfilt2d(arr, [5, 5])
arr = wiener(arr, [3, 3])
arr = wiener(arr, [5, 5])
wave = w2d(arr, 'db1')
output = np.array(wave, 'float32')
# compute abs difference between the two images
data = np.abs(np.array(lab_img) - output)
data = np.array(data.flatten())
# if normalization by L channel is required
if 'norm' in data_type:
data /= 100.
if 'lab' in data_type:
data = transform.get_LAB_L_SVD_s(block)
return data
def w2d(arr, mode):
# convert to float
imArray = arr
# np.divide(imArray, 100) # because of lightness channel, use of 100
# compute coefficients
# same to: LL (LH, HL, HH)
# cA, (cH, cV, cD) = pywt.dwt2(imArray, mode)
# cA *= 0 # remove low-low sub-bands data
# reduce noise from the others cofficients
# LH, HL and HH
# ----
# cannot use specific method to predict thresholds...
# use of np.percentile(XX, 5) => remove data under 5 first percentile
# cH = pywt.threshold(cH, np.percentile(cH, 5), mode='soft')
# cV = pywt.threshold(cV, np.percentile(cV, 5), mode='soft')
# cD = pywt.threshold(cD, np.percentile(cD, 5), mode='soft')
# reconstruction
# imArray_H = pywt.idwt2((cA, (cH, cV, cD)), mode)
# print(np.min(imArray_H), np.max(imArray_H), np.mean(imArray_H))
# imArray_H *= 100 # because of lightness channel, use of 100
# imArray_H = np.array(imArray_H)
# coeffs = pywt.wavedec2(imArray, mode, level=2)
# #Process Coefficients
# coeffs_H=list(coeffs)
# coeffs_H[0] *= 0;
# # reconstruction
# imArray_H=pywt.waverec2(coeffs_H, mode)
# print(np.min(imArray_H), np.max(imArray_H), np.mean(imArray_H))
# using skimage
sigma = restoration.estimate_sigma(imArray, average_sigmas=True, multichannel=False)
imArray_H = restoration.denoise_wavelet(imArray, sigma=sigma, wavelet='db1', mode='soft',
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)