-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetDensityMap.py
111 lines (73 loc) · 3.24 KB
/
GetDensityMap.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
import glob
import os
import ast
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import skimage.io as io
import matplotlib.pyplot as plt
def createDensityMap(shape,name, points):
"""
if (name == "1109-0805/frame000397_1_2.jpg"):
print(points)
"""
# creates an array of only zero values based on the shape of given array.
dens_map = np.zeros(shape=[shape[0], shape[1]])
# Take each coordinates from the list "points" and put 255 on that position.
for point in points:
dens_map[point[1]][point[0]] = 255
# Normalizing the array so that the sum over the whole array equals to the number of elements in the list "points".
if ((np.max(dens_map) - np.min(dens_map)) == 0):
normalized = (dens_map - np.min(dens_map))
else :
normalized = (dens_map - np.min(dens_map)) / (np.max(dens_map) - np.min(dens_map))
# Changed the sigmoid following the advice from microscopic cell image.
# sigmadots = 7
sigmadots = 7
dot_anno = gaussian_filter(normalized, sigmadots)
# Following the advice from microscopic cell image.
#dot_anno = dot_anno * 100
dot_anno.astype(np.float32)
return dot_anno
def get_all_coordinates(inputpath):
# Remove _success.txt file from the directory.
for folders in glob.glob(inputpath + "/*"):
for ascii_txt in glob.glob(folders+"/*"):
remove_file = ascii_txt.split("/")[-1]
if (remove_file == "_SUCCESS"):
os.remove(ascii_txt)
all_coordintes = []
for folders in glob.glob(inputpath + "/*"):
for each_folder in glob.glob(folders + "/*"):
try:
with open(each_folder, 'r') as file_obj:
file_contents = file_obj.readlines()
for each_line in file_contents:
each_line = each_line.strip()
all_coordintes.append(each_line)
except FileNotFoundError:
msg = each_folder + " does not exist."
print(msg)
return all_coordintes
if __name__== '__main__':
gt_coordinate_path = "/home/mrc689/Sampled_Dataset_GT/coordinates/manual"
gt_numpy_save_path = "/home/mrc689/Sampled_Dataset_GT/density_map/manual"
#gt_numpy_save_path = "/home/mrc689/Sampled_Dataset_GT/density_map/xavier"
#gt_coordinate_path = "/home/mrc689/Sampled_Dataset_GT/coordinates/xavier"
if not os.path.exists(gt_numpy_save_path):
os.makedirs(gt_numpy_save_path)
all_coordinates = get_all_coordinates(gt_coordinate_path)
#print(all_coordinates[0])
tmp = []
for each in all_coordinates:
shape = [224,224]
# Converting String dictionary to python dictionary
d = ast.literal_eval(each)
img_name = d['image_name']
gt_arr_name = img_name.split("/")[-1]
gt_arr_name = gt_arr_name.split(".")[0]
arr = createDensityMap(shape=shape,name=img_name,points = d['coordinates'])
output_np_path = gt_numpy_save_path + "/" + ('/'.join(img_name.split('/')[-2:-1])) + "/"
#print(output_np_path)
if not os.path.exists(output_np_path):
os.makedirs(output_np_path)
np.save(output_np_path+ gt_arr_name, arr)