-
Notifications
You must be signed in to change notification settings - Fork 5
/
retina_transform.py
129 lines (102 loc) · 3.46 KB
/
retina_transform.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
import cv2
import numpy as np
import sys
def genGaussiankernel(width, sigma):
x = np.arange(-int(width/2), int(width/2)+1, 1, dtype=np.float32)
x2d, y2d = np.meshgrid(x, x)
kernel_2d = np.exp(-(x2d ** 2 + y2d ** 2) / (2 * sigma ** 2))
kernel_2d = kernel_2d / np.sum(kernel_2d)
return kernel_2d
def pyramid(im, sigma=1, prNum=6):
height_ori, width_ori, ch = im.shape
G = im.copy()
pyramids = [G]
# gaussian blur
Gaus_kernel2D = genGaussiankernel(5, sigma)
# downsample
for i in range(1, prNum):
G = cv2.filter2D(G, -1, Gaus_kernel2D)
height, width, _ = G.shape
G = cv2.resize(G, (int(width/2), int(height/2)))
pyramids.append(G)
# upsample
for i in range(1, 6):
curr_im = pyramids[i]
for j in range(i):
if j < i-1:
im_size = (curr_im.shape[1]*2, curr_im.shape[0]*2)
else:
im_size = (width_ori, height_ori)
curr_im = cv2.resize(curr_im, im_size)
curr_im = cv2.filter2D(curr_im, -1, Gaus_kernel2D)
pyramids[i] = curr_im
return pyramids
def foveat_img(im, fixs):
"""
im: input image
fixs: sequences of fixations of form [(x1, y1), (x2, y2), ...]
This function outputs the foveated image with given input image and fixations.
"""
sigma=0.248
prNum = 6
As = pyramid(im, sigma, prNum)
height, width, _ = im.shape
# compute coef
p = 7.5
k = 3
alpha = 2.5
x = np.arange(0, width, 1, dtype=np.float32)
y = np.arange(0, height, 1, dtype=np.float32)
x2d, y2d = np.meshgrid(x, y)
theta = np.sqrt((x2d - fixs[0][0]) ** 2 + (y2d - fixs[0][1]) ** 2) / p
for fix in fixs[1:]:
theta = np.minimum(theta, np.sqrt((x2d - fix[0]) ** 2 + (y2d - fix[1]) ** 2) / p)
R = alpha / (theta + alpha)
Ts = []
for i in range(1, prNum):
Ts.append(np.exp(-((2 ** (i-3)) * R / sigma) ** 2 * k))
Ts.append(np.zeros_like(theta))
# omega
omega = np.zeros(prNum)
for i in range(1, prNum):
omega[i-1] = np.sqrt(np.log(2)/k) / (2**(i-3)) * sigma
omega[omega>1] = 1
# layer index
layer_ind = np.zeros_like(R)
for i in range(1, prNum):
ind = np.logical_and(R >= omega[i], R <= omega[i - 1])
layer_ind[ind] = i
# B
Bs = []
for i in range(1, prNum):
Bs.append((0.5 - Ts[i]) / (Ts[i-1] - Ts[i] + 1e-5))
# M
Ms = np.zeros((prNum, R.shape[0], R.shape[1]))
for i in range(prNum):
ind = layer_ind == i
if np.sum(ind) > 0:
if i == 0:
Ms[i][ind] = 1
else:
Ms[i][ind] = 1 - Bs[i-1][ind]
ind = layer_ind - 1 == i
if np.sum(ind) > 0:
Ms[i][ind] = Bs[i][ind]
print('num of full-res pixel', np.sum(Ms[0] == 1))
# generate periphery image
im_fov = np.zeros_like(As[0], dtype=np.float32)
for M, A in zip(Ms, As):
for i in range(3):
im_fov[:, :, i] += np.multiply(M, A[:, :, i])
im_fov = im_fov.astype(np.uint8)
return im_fov
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Wrong format: python retina_transform.py [image_path]")
exit(-1)
im_path = sys.argv[1]
im = cv2.imread(im_path)
# im = cv2.resize(im, (512, 320), cv2.INTER_CUBIC)
xc, yc = int(im.shape[1]/2), int(im.shape[0]/2)
im = foveat_img(im, [(xc, yc)])
cv2.imwrite(im_path.split('.')[0]+'_RT.jpg', im)