-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstudy3.py
More file actions
executable file
·69 lines (61 loc) · 2.06 KB
/
Copy pathstudy3.py
File metadata and controls
executable file
·69 lines (61 loc) · 2.06 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
from scipy.ndimage import gaussian_filter
import numpy as np
import matplotlib.pyplot as plt
import tqdm
def vis_heatmap(ls_txt):
heatmap = make_heatmap(ls_txt)
plot_heatmap(heatmap)
def make_heatmap(ls_txt):
ls_det = from_txt_to_det(ls_txt)
ls_cir = from_det_to_cir(ls_det)
heatmap = cir_to_heatmap(ls_cir)
return heatmap
def from_txt_to_det(ls_txt):
ls_det = []
for f in tqdm.tqdm(ls_txt, desc="Reading txt files"):
with open(f) as f:
ls_det += [l.strip() for l in f.readlines()]
return ls_det
def from_det_to_cir(ls_det):
ls_cir = []
for det in tqdm.tqdm(ls_det, desc="Parsing detections"):
# split by space
dets = det.split(" ")
x = float(dets[1])
y = float(dets[2])
w = float(dets[3])
h = float(dets[4])
r = (w * h) ** 0.5 / 2 # radius
ls_cir += [(x, y, r)]
return ls_cir
def cir_to_heatmap(ls_cir):
# image size
image_size = (1000, 1000)
# create an empty image grid
heatmap = np.zeros(image_size)
# resize the coordinates and plot circles
for x, y, r in tqdm.tqdm(ls_cir, desc="Creating heatmap"):
x_resized = int(x * image_size[0])
y_resized = int(y * image_size[1])
radius_resized = int(r * image_size[0])
if radius_resized == 0:
radius_resized = 1
# create a grid of the same size as the image
Y, X = np.ogrid[:image_size[0], :image_size[1]]
distance = np.sqrt((X - x_resized) ** 2 + (Y - y_resized) ** 2)
# add intensity to the heatmap
num = -(distance**2)
den = 2 * (radius_resized**2)
heatmap += np.exp(num / den)
heatmap = gaussian_filter(heatmap, sigma=5)
# return
return heatmap
def plot_heatmap(heatmap, max_value=6):
plt.imshow(heatmap, cmap='hot', extent=(0, 1, 0, 1))
plt.clim(0, max_value) # standardize the colorbar and c max
plt.colorbar(label='Intensity')
plt.title('Heatmap of Circle Distribution')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.axis("off")
plt.show()