-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgaussian.py
147 lines (113 loc) · 3.21 KB
/
gaussian.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
import numpy as np
from skimage.filters import gaussian
sigmas = np.array([.26, .25, .25, .35, .35, .79, .79, .72, .72, .62, .62, 1.07, 1.07, .87, .87, .89, .89] * 100)
def multivariate_gaussian(N, sigma=2):
t = 4
X = np.linspace(-t, t, N)
Y = np.linspace(-t, t, N)
X, Y = np.meshgrid(X, Y)
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
mu = np.array([0., 0.])
sigma = np.array([[sigma, 0], [0, sigma]])
n = mu.shape[0]
Sigma_det = np.linalg.det(sigma)
Sigma_inv = np.linalg.inv(sigma)
N = np.sqrt((2 * np.pi) ** n * Sigma_det)
fac = np.einsum('...k,kl,...l->...', pos - mu, Sigma_inv, pos - mu)
return np.exp(-fac / 2) / N
def crop_paste(img, c, N=13, sigma=2):
Z = multivariate_gaussian(N, sigma)
H = img.shape[1]
W = img.shape[0]
h = (Z.shape[0] - 1) / 2
N = Z.shape[0]
x1 = (c[0] - h)
y1 = (c[1] - h)
x2 = (c[0] + h) + 1
y2 = (c[1] + h) + 1
zx1 = 0
zy1 = 0
zx2 = N + 1
zy2 = N + 1
if x1 < 0:
x1 = 0
zx1 = 0 - (c[0] - h)
if y1 < 0:
y1 = 0
zy1 = 0 - (c[1] - h)
if x2 > W - 1:
x2 = W - 1
zx2 = x2 - x1 + 1
x2 = W
if y2 > H - 1:
y2 = H - 1
zy2 = y2 - y1 + 1
y2 = H
img[x1:x2, y1:y2] = np.maximum(Z[zx1:zx2, zy1:zy2], img[x1:x2, y1:y2])
'''
def gaussian(img, N = 13, sigma=2):
cs = np.where(img==1)
img = np.zeros_like(img)
for c in zip(cs[0], cs[1]):
crop_paste(img, c, N, sigma)
return img
'''
def gaussian_multi_input_mp(inp):
'''
:param inp: Multi person ground truth heatmap input (17 ch) Each channel contains multiple joints.
:return: out: Gaussian augmented output. Values are between 0. and 1.
'''
h, w, ch = inp.shape
out = np.zeros_like(inp)
for i in range(ch):
layer = inp[:, :, i]
ind = np.argwhere(layer == 1)
b = []
if len(ind) > 0:
for j in ind:
t = np.zeros((h, w))
t[j[0], j[1]] = 1
t = gaussian(t, sigma=2, mode='constant')
t = t * (1 / t.max())
b.append(t)
out[:, :, i] = np.maximum.reduce(b)
else:
out[:, :, i] = np.zeros((h, w))
return out
def gaussian_multi_output(inp):
'''
:param inp: Single person ground truth heatmap input (17 ch) Each channel contains one joint.
:return: out: Gaussian augmented output. Values are between 0. and 1.
'''
h, w, ch = inp.shape
out = np.zeros_like(inp)
for i in range(ch):
j = np.argwhere(inp[:, :, i] == 1)
if len(j) == 0:
out[:, :, i] = np.zeros((h, w))
continue
j = j[0]
t = np.zeros((h, w))
t[j[0], j[1]] = 1
t = gaussian(t, sigma=5, mode='constant')
out[:, :, i] = t * (1 / t.max())
return out
def crop(img, c, N=13):
H = img.shape[1]
W = img.shape[0]
h = (N - 1) / 2
x1 = int(c[0] - h)
y1 = int(c[1] - h)
x2 = int(c[0] + h) + 1
y2 = int(c[1] + h) + 1
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
if x2 > W - 1:
x2 = W
if y2 > H - 1:
y2 = H
return img[x1:x2, y1:y2]