-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicSaliencyLib.py
522 lines (429 loc) · 16.6 KB
/
DynamicSaliencyLib.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import os
import time
import numpy as np
from cv2 import cv2
import pandas as pd
import matplotlib.pyplot as plt
from multiprocessing import Process
from multiprocessing import Pipe
from MotionDetectionModule import MotionDetection
def get_intensity(image):
"""
Get intensity map. Values range from 0 to 1.
"""
# convert scale of array elements
src = np.float32(image) * 1./255
# extract intensity
intensity_map = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
return intensity_map
def get_gaussian_pyramid(image):
"""
Get gaussian pyramid with 8 levels of downsampling.
"""
pyr = list()
pyr.append(image)
for i in range(1, 9):
next_layer = cv2.pyrDown(pyr[i-1])
pyr.append(next_layer)
return pyr
def center_surround_diff(gauss_pyr):
maps = list()
for c in range(2, 5):
center = gauss_pyr[c]
size = (center.shape[1], center.shape[0])
for s in range(3, 5):
surround = cv2.resize(
gauss_pyr[c+s], size, interpolation=cv2.INTER_LINEAR)
cs_difference_map = cv2.absdiff(center, surround)
maps.append(cs_difference_map)
return maps
def simple_normalization(image, M=1):
img_min, img_max, _, _ = cv2.minMaxLoc(image)
if img_min != img_max:
normalized = image/(img_max-img_min) - img_min/(img_max-img_min)
if M != 1:
normalized = normalized*M
else:
normalized = image - img_min
return normalized
def compute_average_local_maxima(feature_map, stepsize=30):
# NOTE: I compute local maxima taking into account last slices of the matrix
# 30 corresponds to ~1 degree of visual angle [depends on the dataset, may need to be chancged]
width = feature_map.shape[1]
height = feature_map.shape[0]
avg_size = stepsize
if(avg_size > height-1):
avg_size = height-1
if(avg_size > width-1):
avg_size = width-1
# find local maxima
num_maxima = 0
sum_all_maxima = 0
for y in range(0, height-avg_size, avg_size):
for x in range(0, width-avg_size, avg_size):
local_img = feature_map[y:y+avg_size, x:x+avg_size]
_, loc_max, _, _ = cv2.minMaxLoc(local_img)
sum_all_maxima += loc_max
num_maxima += 1
last_x = x+avg_size
local_img = feature_map[y:y+avg_size, last_x:(width)]
_, loc_max, _, _ = cv2.minMaxLoc(local_img)
sum_all_maxima += loc_max
num_maxima += 1
last_y = y+avg_size
for x in range(0, width-avg_size, avg_size):
local_img = feature_map[last_y:height, x:x+avg_size]
_, loc_max, _, _ = cv2.minMaxLoc(local_img)
sum_all_maxima += loc_max
num_maxima += 1
last_x = x+avg_size
local_img = feature_map[last_y:height, last_x:(width)]
_, loc_max, _, _ = cv2.minMaxLoc(local_img)
sum_all_maxima += loc_max
num_maxima += 1
# averaging over all the local regions
return sum_all_maxima / num_maxima
def normalize_map(feature_map):
"""
This function implements the particular normalization operator N
described in Itti 1998.
"""
# normalize in range [0...M], choice M=1
M = 1
simply_normalized = simple_normalization(feature_map, M)
# get average local maximum
avg_local_maximum = compute_average_local_maxima(
simply_normalized)
# normalize feature map as from paper
coeff_normalization = (M-avg_local_maximum)**2
itti_normalized = simply_normalized * coeff_normalization
return itti_normalized
# INTENSITY
def get_intensity_conspicuity_map(intensity):
shape = intensity.shape
# get Gaussian pyramid for intensity
intensity_gauss_pyr = get_gaussian_pyramid(intensity)
# compute 6 feature maps at different scales
feature_maps = center_surround_diff(intensity_gauss_pyr)
# normalize feature maps
norm_feature_maps = [normalize_map(m) for m in feature_maps]
# get conspicuity map from normalized maps
# NOTE: in 1998 paper they use scale 4 for the conspicuity maps, here scale 0
norm_feature_maps = [
cv2.resize(m, shape[::-1],
interpolation=cv2.INTER_LINEAR)
for m in norm_feature_maps]
# sum all c-s diff maps into conspicuity map
intensity_conspicuity = np.zeros_like(intensity)
for cs_index in range(6):
intensity_conspicuity += norm_feature_maps[cs_index]
return normalize_map(intensity_conspicuity)
# COLOR
def get_color_conspicuity_map(image):
"""
Single core version.
"""
# get color channels
(b, g, r) = get_color_channels(image)
shape = b.shape
# build 4 broadly-tuned color channels
R_ch = (r-(g+b))/2
G_ch = (g-(r+b))/2
B_ch = (b-(r+g))/2
Y_ch = ((r+g)/2 - abs(r-g)/2 - b)
# set to 0 negative values
R_ch[R_ch < 0] = 0
G_ch[G_ch < 0] = 0
B_ch[B_ch < 0] = 0
Y_ch[Y_ch < 0] = 0
# create gaussian pyramids
red_gauss_pyr = get_gaussian_pyramid(R_ch)
green_gauss_pyr = get_gaussian_pyramid(G_ch)
blue_gauss_pyr = get_gaussian_pyramid(B_ch)
yellow_gauss_pyr = get_gaussian_pyramid(Y_ch)
# get feature maps for different color spaces
rg_color_space_maps = get_color_space_feature_maps(
red_gauss_pyr, green_gauss_pyr)
by_color_space_maps = get_color_space_feature_maps(
blue_gauss_pyr, yellow_gauss_pyr)
# normalize feature maps
rg_color_space_maps = [normalize_map(
m) for m in rg_color_space_maps]
by_color_space_maps = [normalize_map(
m) for m in by_color_space_maps]
# get conspicuity map
color_conspicuity_map = get_conspicuity_from_color_spaces(
rg_color_space_maps, by_color_space_maps, shape)
return normalize_map(color_conspicuity_map)
def get_color_channels(image):
intensity = np.float32(np.sum(image, axis=2))
_, max_int, _, _ = cv2.minMaxLoc(intensity)
min_intensity_admitted = max_int/10
for y in range(image.shape[0]):
for x in range(image.shape[1]):
if intensity[y][x] < min_intensity_admitted:
image[y][x] = [0, 0, 0]
src = np.zeros_like(image, dtype=np.float32)
for y in range(image.shape[0]):
for x in range(image.shape[1]):
if image[y][x].any() > 0:
src[y][x] = np.float32(np.float32(
image[y][x])/np.float32(max_int))
return cv2.split(src)
def get_color_space_feature_maps(c1_pyr, c2_pyr):
# here we compute the feature maps for the double opponent color system
feature_maps = list()
for c in range(2, 5):
center = c1_pyr[c]-c2_pyr[c]
size = (center.shape[1], center.shape[0])
for s in range(3, 5):
surround = c2_pyr[c+s] - c1_pyr[c+s]
res_surround = center - \
cv2.resize(surround, size, interpolation=cv2.INTER_LINEAR)
cs_diff = cv2.absdiff(center, res_surround)
feature_maps.append(cs_diff)
return feature_maps
def get_conspicuity_from_color_spaces(rg_space, by_space, shape):
conspicuity_map = np.zeros(shape)
# resizing normalized feature maps
# NOTE: in 1998 paper they use scale 4 for the conspicuity maps, here scale 0
rg_space = [cv2.resize(m, shape[::-1],
interpolation=cv2.INTER_LINEAR) for m in rg_space]
by_space = [cv2.resize(m, shape[::-1],
interpolation=cv2.INTER_LINEAR) for m in by_space]
for cs_index in range(6):
conspicuity_map += (rg_space[cs_index] + by_space[cs_index])
return conspicuity_map
def rg_color_space(send, recv):
"""
Processing rg color space for multiprocessing approach.
"""
while True:
(b, g, r) = recv.recv()
# build 4 broadly-tuned color channels
R_ch = (r-(g+b))/2
G_ch = (g-(r+b))/2
# set to 0 negative values
R_ch[R_ch < 0] = 0
G_ch[G_ch < 0] = 0
# create gaussian pyramids
red_gauss_pyr = get_gaussian_pyramid(R_ch)
green_gauss_pyr = get_gaussian_pyramid(G_ch)
# get feature maps for different color spaces
rg_color_space_maps = get_color_space_feature_maps(
red_gauss_pyr, green_gauss_pyr)
# normalize feature maps
rg_color_space_maps = [normalize_map(
m) for m in rg_color_space_maps]
# TODO: what if I die?
send.send(rg_color_space_maps)
def by_color_space(send, recv):
"""
Processing by color space for multiprocessing approach.
"""
while True:
# get color channels
(b, g, r) = recv.recv()
# build 4 broadly-tuned color channels
B_ch = (b-(r+g))/2
Y_ch = ((r+g)/2 - abs(r-g)/2 - b)
# set to 0 negative values
B_ch[B_ch < 0] = 0
Y_ch[Y_ch < 0] = 0
# create gaussian pyramids
blue_gauss_pyr = get_gaussian_pyramid(B_ch)
yellow_gauss_pyr = get_gaussian_pyramid(Y_ch)
# get feature maps for different color spaces
by_color_space_maps = get_color_space_feature_maps(
blue_gauss_pyr, yellow_gauss_pyr)
# normalize feature maps
by_color_space_maps = [normalize_map(
m) for m in by_color_space_maps]
send.send(by_color_space_maps)
# ORIENTATION
def get_gabor_kernels(shape):
# NOTE: Gabor Kernels are chosen by a trial-end-error mean
kernel_list = list()
kernel_size = int(min(shape)/42)
lamb = kernel_size
sigma = lamb*.56
gamma = 0.5
psi = 0
orientations = [0, (np.pi/4), (np.pi/2), (np.pi*3/4)]
for theta in orientations:
gabor_kernel = cv2.getGaborKernel(
(kernel_size, kernel_size), sigma, theta, lamb, gamma, psi, ktype=cv2.CV_32F)
gabor_kernel = gabor_kernel/gabor_kernel.sum()
kernel_list.append(gabor_kernel)
return kernel_list
def get_conspicuity_from_orienation_feature_maps(orient_feature_maps, shape, debug=False):
conspicuity_map = np.zeros(shape)
i = 0
for orientation in orient_feature_maps:
i += 1
orientation_conspicuity = np.zeros(shape)
for feature_map in orientation:
normalized = normalize_map(feature_map)
resized = cv2.resize(
normalized, shape[::-1], interpolation=cv2.INTER_LINEAR)
orientation_conspicuity += resized
if debug:
cv2.imshow(f'Orientation {i}', orientation_conspicuity)
cv2.waitKey(0)
conspicuity_map += normalize_map(orientation_conspicuity)
return conspicuity_map
def get_orientation_conspicuity_map(intensity, debug=False):
shape = intensity.shape
# get gabor filters
filters = get_gabor_kernels(shape)
# print(filters[0].shape)
# convolve intensity, obtain 4 preferred orientations
oriented_intensities = [
cv2.filter2D(intensity, cv2.CV_32F, kernel) for kernel in filters
]
for i in range(len(oriented_intensities)):
intensity = oriented_intensities[i]
kernel = filters[i]
if debug:
cv2.imshow(f'Intensity {i}', intensity)
cv2.imshow(f'Kernel {i}', kernel)
if debug:
cv2.waitKey(0)
# get a gaussian pyramid for each orientation
intensity_pyramids = [
get_gaussian_pyramid(oriented_intensity) for oriented_intensity in oriented_intensities
]
# get center surround differences
cs_differences = [
center_surround_diff(oriented_pyr) for oriented_pyr in intensity_pyramids
]
# compute conspicuity map for orientation
orientation_conspicuity_map = get_conspicuity_from_orienation_feature_maps(
cs_differences, shape, debug)
return normalize_map(orientation_conspicuity_map)
# MOTION
def get_motion_conspicuity_map(frames, debug=False):
if frames[1] is None:
if debug:
print('no motion contributre to the first frame')
return
# get b/w image
frame = frames[0]
prev_frame = frames[1]
shape = frame.shape
# get motion map
md = MotionDetection()
motion_map = md.diamond_search_motion_estimation(
prev_frame, frame)
motion_map = simple_normalization(motion_map.astype('float32'))
# get Gaussian pyramid for motion
motion_gauss_pyr = get_gaussian_pyramid(motion_map)
# compute 6 feature maps at different scales
feature_maps = center_surround_diff(motion_gauss_pyr)
# normalize feature maps
norm_feature_maps = [normalize_map(m) for m in feature_maps]
# get conspicuity map from normalized maps
# NOTE: in 1998 paper they use scale 4 for the conspicuity maps
norm_feature_maps = [
cv2.resize(m, shape[::-1],
interpolation=cv2.INTER_LINEAR)
for m in norm_feature_maps]
# sum all c-s diff maps into conspicuity map
motion_conspicuity = np.zeros(shape)
for cs_index in range(6):
motion_conspicuity += norm_feature_maps[cs_index]
prev_frame = frame
return normalize_map(motion_conspicuity)
# MULTIPROCESSING
def intensity(send_conn, recieve_conn):
while True:
image = recieve_conn.recv()
imap = get_intensity_conspicuity_map(image)
send_conn.send(imap)
def orientation(send_conn, recieve_conn):
while True:
image = recieve_conn.recv()
omap = get_orientation_conspicuity_map(image)
send_conn.send(omap)
def motion(send_conn, recieve_conn):
while True:
image = recieve_conn.recv()
mmap = get_motion_conspicuity_map(image)
send_conn.send(mmap)
def color(send_conn, recieve_conn):
while True:
image = recieve_conn.recv()
cmap = get_color_conspicuity_map(image)
send_conn.send(cmap)
# COMPLETE
def get_spatial_saliency_map(image):
intensity = get_intensity(image)
intensity_conspicuity = get_intensity_conspicuity_map(intensity)
color_conspicuity = get_color_conspicuity_map(image)
orientation_conspicuity = get_orientation_conspicuity_map(intensity)
n_intensity = normalize_map(intensity_conspicuity)
c_intensity = normalize_map(color_conspicuity)
o_intensity = normalize_map(orientation_conspicuity)
saliency_map = (n_intensity+c_intensity+o_intensity)*(1/3)
return saliency_map
def get_spatiotemporal_saliency_maps(video_name, map_width=None, map_height=256, debug=False):
frames = read_video_stream(video_name)
print(f'read {len(frames)} frames, of size {frames[0].shape}')
# cv2.imshow(f"frame 0 groso", frames[0])
# cv2.waitKey(0)
frames = resize_frames(frames, map_height, map_width)
print(f'read {len(frames)} frames, of size {frames[0].shape}')
# cv2.imshow(f"frame 0 picolo", frames[0])
# cv2.waitKey(0)
if debug:
print(f'read {len(frames)} frames, of size {frames[0].shape}')
if not os.path.exists('./saliency maps/'):
os.makedirs('./saliency maps/')
for i in range(len(frames)):
if debug:
print(f"frame {i}")
intensity = get_intensity(frames[i])
intensity_conspicuity = get_intensity_conspicuity_map(intensity)
n_intensity = normalize_map(intensity_conspicuity)
color_conspicuity = get_color_conspicuity_map(frames[i])
n_color = normalize_map(color_conspicuity)
orientation_conspicuity = get_orientation_conspicuity_map(intensity)
n_orientation = normalize_map(orientation_conspicuity)
if i > 0:
prev_intensity = get_intensity(frames[i-1])
motion_conspicuity = get_motion_conspicuity_map(
(intensity, prev_intensity))
n_motion = normalize_map(motion_conspicuity)
saliency_map = (n_intensity+n_color+n_orientation+n_motion)*(1/4)
else:
saliency_map = (n_intensity+n_color+n_orientation)*(1/3)
print(f"saliency shape: {saliency_map.shape}")
plt.subplot(1, 2, 1), plt.imshow(
cv2.cvtColor(frames[i], cv2.COLOR_BGR2RGB))
plt.title(f'Original {i}')
plt.subplot(1, 2, 2), plt.imshow(saliency_map, 'gray')
plt.title(f'Saliency map {i+1}')
plt.savefig('./saliency maps/frames'+str(i)+'-'+str(i+1)+'.png')
def read_video_stream(name):
frames = list()
cap = cv2.VideoCapture(name)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
frames.append(frame)
cv2.imshow(f"frame catturato", frame)
cv2.waitKey(0)
cap.release()
return frames
def resize_frames(frames, height, width=None):
resized = []
if width is None:
old_shape = frames[0].shape
new_shape = (height, int(old_shape[1]/(old_shape[0]/height)))
else:
new_shape = (height, width)
for frame in frames:
res = cv2.resize(frame, new_shape[::-1], interpolation=cv2.INTER_LINEAR)
resized.append(res)
return resized