-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
181 lines (138 loc) · 5.71 KB
/
utilities.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from __future__ import division
import cv2
import numpy as np
import scipy.io
import scipy.ndimage
def padding(img, shape_r=240, shape_c=320, channels=3):
img_padded = np.zeros((shape_r, shape_c, channels), dtype=np.uint8)
if channels == 1:
img_padded = np.zeros((shape_r, shape_c), dtype=np.uint8)
original_shape = img.shape
rows_rate = original_shape[0]/shape_r
cols_rate = original_shape[1]/shape_c
if rows_rate > cols_rate:
new_cols = (original_shape[1] * shape_r) // original_shape[0]
img = cv2.resize(img, (new_cols, shape_r))
if new_cols > shape_c:
new_cols = shape_c
img_padded[:, ((img_padded.shape[1] - new_cols) // 2):((img_padded.shape[1] - new_cols) // 2 + new_cols)] = img
else:
new_rows = (original_shape[0] * shape_c) // original_shape[1]
img = cv2.resize(img, (shape_c, new_rows))
if new_rows > shape_r:
new_rows = shape_r
img_padded[((img_padded.shape[0] - new_rows) // 2):((img_padded.shape[0] - new_rows) // 2 + new_rows), :] = img
return img_padded
def resize_fixation(img, rows=480, cols=640):
out = np.zeros((rows, cols))
factor_scale_r = rows / img.shape[0]
factor_scale_c = cols / img.shape[1]
coords = np.argwhere(img)
for coord in coords:
r = int(np.round(coord[0]*factor_scale_r))
c = int(np.round(coord[1]*factor_scale_c))
if r == rows:
r -= 1
if c == cols:
c -= 1
out[r, c] = 1
return out
def padding_fixation(img, shape_r=480, shape_c=640):
img_padded = np.zeros((shape_r, shape_c))
original_shape = img.shape
rows_rate = original_shape[0]/shape_r
cols_rate = original_shape[1]/shape_c
if rows_rate > cols_rate:
new_cols = (original_shape[1] * shape_r) // original_shape[0]
img = resize_fixation(img, rows=shape_r, cols=new_cols)
if new_cols > shape_c:
new_cols = shape_c
img_padded[:, ((img_padded.shape[1] - new_cols) // 2):((img_padded.shape[1] - new_cols) // 2 + new_cols)] = img
else:
new_rows = (original_shape[0] * shape_c) // original_shape[1]
img = resize_fixation(img, rows=new_rows, cols=shape_c)
if new_rows > shape_r:
new_rows = shape_r
img_padded[((img_padded.shape[0] - new_rows) // 2):((img_padded.shape[0] - new_rows) // 2 + new_rows), :] = img
return img_padded
def preprocess_images(paths, shape_r, shape_c, data_format='channels_first'):
ims = np.zeros((len(paths), shape_r, shape_c, 3))
for i, path in enumerate(paths):
original_image = cv2.imread(path)
padded_image = padding(original_image, shape_r, shape_c, 3)
ims[i] = padded_image
ims[:, :, :, 0] -= 103.939
ims[:, :, :, 1] -= 116.779
ims[:, :, :, 2] -= 123.68
if data_format == 'channels_first':
ims = ims.transpose((0, 3, 1, 2))
return ims
def preprocess_maps(paths, shape_r, shape_c):
ims = np.zeros((len(paths), 1, shape_r, shape_c))
for i, path in enumerate(paths):
original_map = cv2.imread(path, 0)
padded_map = padding(original_map, shape_r, shape_c, 1)
ims[i, 0] = padded_map.astype(np.float32)
ims[i, 0] /= 255.0
ims = ims.transpose((0, 2, 3, 1))
return ims
def preprocess_fixmaps(paths, shape_r, shape_c):
ims = np.zeros((len(paths), 1, shape_r, shape_c))
for i, path in enumerate(paths):
fix_map = scipy.io.loadmat(path)["im"]
ims[i, 0] = padding_fixation(fix_map, shape_r=shape_r, shape_c=shape_c)
ims = ims.transpose((0, 2, 3, 1))
return ims
def standarization(map):
[w,h,_] = np.shape(map)
map_flatten = np.reshape(map, (w*h))
m = np.mean(map_flatten)
s = np.std(map_flatten,ddof=1)
map = (map-m)/s
return map
def get_percentile(map, percent=0.5):
[w,h,_] = np.shape(map)
map_flatten = np.reshape(map, (w*h))
top_k = round(w*h*percent)
top_k_idx = map_flatten.argsort()[::-1][top_k]
threshold = map_flatten[top_k_idx]
return threshold
def dof_mask(map_ASD, map_TD):
mask = []
for map1, map2 in zip(map_ASD, map_TD):
dof = standarization(map1) - standarization(map2)
threshold = get_percentile(dof, percent=0.3)
mask.append(dof>threshold)
return mask
def preprocess_dof(path_ASD, path_TD, shape_r, shape_c):
map_ASD = preprocess_maps(path_ASD, shape_r, shape_c)
map_TD = preprocess_maps(path_TD, shape_r, shape_c)
mask = dof_mask(map_ASD, map_TD)
res = np.zeros_like(map_ASD)
i=0
for m1,m2 in zip(map_ASD, mask):
m1 = np.squeeze(m1)
m2 = np.squeeze(m2)
m1[~m2] = 0 # m2[i]==1 denotes the value of i-th pixel in dof map is larger than top-k
m1 = np.expand_dims(m1, axis=-1)
res[i] = m1
i += 1
return res
def postprocess_predictions(pred, shape_r, shape_c):
predictions_shape = pred.shape
rows_rate = shape_r / predictions_shape[0]
cols_rate = shape_c / predictions_shape[1]
pred = pred / np.max(pred) * 255
if rows_rate > cols_rate:
new_cols = (predictions_shape[1] * shape_r) // predictions_shape[0]
pred = cv2.resize(pred, (new_cols, shape_r))
img = pred[:, ((pred.shape[1] - shape_c) // 2):((pred.shape[1] - shape_c) // 2 + shape_c)]
else:
new_rows = (predictions_shape[0] * shape_c) // predictions_shape[1]
pred = cv2.resize(pred, (shape_c, new_rows))
img = pred[((pred.shape[0] - shape_r) // 2):((pred.shape[0] - shape_r) // 2 + shape_r), :]
img = scipy.ndimage.filters.gaussian_filter(img, sigma=7)
img = scipy.ndimage.filters.gaussian_filter(img, sigma=7)
img = img / np.max(img) * 255
# img = (img -np.min(img))/ (np.max(img)-np.min(img)) * 255
return img