-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_gen.py
More file actions
39 lines (38 loc) · 1.6 KB
/
utils_gen.py
File metadata and controls
39 lines (38 loc) · 1.6 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
import numpy as np
import cv2
def gen_density_map_gaussian(im, points, sigma=10):
"""
func: generate the density map
"""
density_map = np.zeros(im.shape[:2], dtype=np.float32)
h, w = density_map.shape[:2]
num_gt = np.squeeze(points).shape[0]
if num_gt == 0:
return density_map
for p in points:
p = np.round(p).astype(int)
p[0], p[1] = min(h-1, p[1]), min(w-1, p[0])
gaussian_radius = sigma * 2 - 1
gaussian_map = np.multiply(
cv2.getGaussianKernel(int(gaussian_radius*2+1), sigma),
cv2.getGaussianKernel(int(gaussian_radius*2+1), sigma).T
)
x_left, x_right, y_up, y_down = 0, gaussian_map.shape[1], 0, gaussian_map.shape[0]
# cut the gaussian kernel
if p[1] < gaussian_radius:
x_left = gaussian_radius - p[1]
if p[0] < gaussian_radius:
y_up = gaussian_radius - p[0]
if p[1] + gaussian_radius >= w:
x_right = gaussian_map.shape[1] - (gaussian_radius + p[1] - w) - 1
if p[0] + gaussian_radius >= h:
y_down = gaussian_map.shape[0] - (gaussian_radius + p[0] - h) - 1
gaussian_map = gaussian_map[y_up:y_down, x_left:x_right]
if np.sum(gaussian_map):
gaussian_map = gaussian_map / np.sum(gaussian_map)
density_map[
max(0, p[0]-gaussian_radius):min(h, p[0]+gaussian_radius+1),
max(0, p[1]-gaussian_radius):min(w, p[1]+gaussian_radius+1)
] += gaussian_map
density_map = density_map / (np.sum(density_map / num_gt))
return density_map