-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFixpos2Densemap.py
More file actions
96 lines (80 loc) · 2.69 KB
/
Fixpos2Densemap.py
File metadata and controls
96 lines (80 loc) · 2.69 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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 18 21:55:03 2018
@author: user
"""
import cv2
import numpy as np
from tqdm import tqdm
def GaussianMask(sizex,sizey, sigma=33, center=None,fix=1):
"""
sizex : mask width
sizey : mask height
sigma : gaussian Sd
center : gaussian mean
fix : gaussian max
return gaussian mask
"""
x = np.arange(0, sizex, 1, float)
y = np.arange(0, sizey, 1, float)
x, y = np.meshgrid(x,y)
if center is None:
x0 = sizex // 2
y0 = sizey // 2
else:
if np.isnan(center[0])==False and np.isnan(center[1])==False:
x0 = center[0]
y0 = center[1]
else:
return np.zeros((sizey,sizex))
return fix*np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / sigma**2)
def Fixpos2Densemap(fix_arr, width, height, imgfile, alpha=0.5, threshold=10):
"""
fix_arr : fixation array number of subjects x 3(x,y,fixation)
width : output image width
height : output image height
imgfile : image file (optional)
alpha : marge rate imgfile and heatmap (optional)
threshold : heatmap threshold(0~255)
return heatmap
"""
heatmap = np.zeros((H,W), np.float32)
for n_subject in tqdm(range(fix_arr.shape[0])):
heatmap += GaussianMask(W, H, 33, (fix_arr[n_subject,0],fix_arr[n_subject,1]),
fix_arr[n_subject,2])
# Normalization
heatmap = heatmap/np.amax(heatmap)
heatmap = heatmap*255
heatmap = heatmap.astype("uint8")
if imgfile.any():
# Resize heatmap to imgfile shape
h, w, _ = imgfile.shape
heatmap = cv2.resize(heatmap, (w, h))
heatmap_color = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
# Create mask
mask = np.where(heatmap<=threshold, 1, 0)
mask = np.reshape(mask, (h, w, 1))
mask = np.repeat(mask, 3, axis=2)
# Marge images
marge = imgfile*mask + heatmap_color*(1-mask)
marge = marge.astype("uint8")
marge = cv2.addWeighted(imgfile, 1-alpha, marge,alpha,0)
return marge
else:
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
return heatmap
if __name__ == '__main__':
# Load image file
img = cv2.imread('sample.png')
# Generate toy fixation data
# when you use, replace here with your data
num_subjects = 40
H, W, _ = img.shape
fix_arr = np.random.randn(num_subjects,3)
fix_arr -= fix_arr.min()
fix_arr /= fix_arr.max()
fix_arr[:,0] *= W
fix_arr[:,1] *= H
# Create heatmap
heatmap = Fixpos2Densemap(fix_arr, W, H, img, 0.7, 5)
cv2.imwrite("output.png",heatmap)