-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
178 lines (153 loc) · 5.37 KB
/
test.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import time
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import distancemap as dm
BENCHMARK = True
EXAMPLE = True
def benchmark():
# 1024 x 1024 with 10 points
input = np.zeros((1024, 1024)).astype(np.bool)
input[256, 256] = True
input[256, 512] = True
input[256, 768] = True
input[512, 512] = True
input[768, 768] = True
input[0, 0] = True
input[0, 200] = True
input[900, 900] = True
input[950, 900] = True
input[1000, 0] = True
# Create CPU load
for i in range(50):
dm.distance_map_from_binary_matrix(input)
time1 = time.time()
for i in range(10):
dm.distance_map_from_binary_matrix(input)
time2 = time.time()
time_dmfbm_2d_1024_1024_10 = (time2 - time1)*1000
points = np.argwhere(input)
time1 = time.time()
for i in range(10):
dm.distance_map(input.shape, points)
time2 = time.time()
time_dm_2d_1024_1024_10 = (time2 - time1)*1000
# 1024 x 1024 with 5 points
input = np.zeros((1024, 1024)).astype(np.bool)
input[512, 512] = True
input[768, 768] = True
input[0, 200] = True
input[900, 900] = True
input[1000, 0] = True
time1 = time.time()
for i in range(10):
dm.distance_map_from_binary_matrix(input)
time2 = time.time()
time_dmfbm_2d_1024_1024_5 = (time2 - time1)*1000
points = np.argwhere(input)
time1 = time.time()
for i in range(10):
dm.distance_map(input.shape, points)
time2 = time.time()
time_dm_2d_1024_1024_5 = (time2 - time1)*1000
print(time_dmfbm_2d_1024_1024_10)
print(time_dm_2d_1024_1024_10)
print(time_dmfbm_2d_1024_1024_5)
print(time_dm_2d_1024_1024_5)
# 128 x 128 x 128 with 10 points
time_dmfbm_3d_256_256_256_25 = 0
time_dm_3d_256_256_256_25 = 0
for i in range(10):
input = np.zeros((128, 128, 128)).astype(np.bool)
for j in range(10):
x = random.randint(0, 128-1)
y = random.randint(0, 128-1)
z = random.randint(0, 128-1)
input[x, y, z] = True
points = np.argwhere(input)
time1 = time.time()
dm.distance_map_from_binary_matrix(input)
time2 = time.time()
time_dmfbm_3d_256_256_256_25 += (time2 - time1)*1000
time1 = time.time()
dm.distance_map(input.shape, points)
time2 = time.time()
time_dm_3d_256_256_256_25 += (time2 - time1)*1000
print(time_dmfbm_3d_256_256_256_25)
print(time_dm_3d_256_256_256_25)
def example():
input = np.zeros((1024, 1024)).astype(np.bool)
input[256, 256] = True
input[256, 512] = True
input[256, 768] = True
input[512, 512] = True
input[768, 768] = True
input[20, 20] = True
input[20, 200] = True
input[900, 900] = True
input[950, 900] = True
input[1000, 20] = True
input_visible = input
"""
points = np.argwhere(input_visible)
for p in points:
input_visible[p[0]-15:p[0]+15, p[1]-15:p[1]+15] = True
"""
plt.figure(1)
plt.subplot(231)
plt.imshow(input_visible, cmap="Greys")
plt.title("original (each square represent a single pixel)")
plt.subplot(232)
plt.imshow(dm.distance_map_from_binary_matrix(input), cmap="Greys", vmin=0, vmax=255)
plt.title("default")
plt.subplot(233)
plt.imshow(dm.distance_map_from_binary_matrix(input, distance="manhattan"), cmap="Greys", vmin=0, vmax=255)
plt.title("manhattan")
plt.subplot(234)
plt.imshow(dm.distance_map_from_binary_matrix(input, alpha="square", omega=255.0), cmap="Greys", vmin=0, vmax=255)
plt.title("alpha square")
plt.subplot(235)
dm.set_a(0.5)
plt.imshow(dm.distance_map_from_binary_matrix(input, alpha="linear"), cmap="Greys", vmin=0, vmax=255)
plt.title("linear 0.5*x")
plt.subplot(236)
plt.imshow(dm.distance_map_from_binary_matrix(input, distance="manhattan", alpha="linear", omega=100.0), cmap="Greys", vmin=0, vmax=255)
plt.title("manhattan linear 0.5*x omega=100")
plt.show()
input = np.zeros((1024, 1024)).astype(np.bool)
for i in range(10):
x = random.randint(20, 1024-20)
y = random.randint(20, 1024-20)
input[x, y] = True
input_visible = input
"""
points = np.argwhere(input_visible)
for p in points:
input_visible[p[0]-15:p[0]+15, p[1]-15:p[1]+15] = True
"""
plt.figure(1)
plt.subplot(231)
plt.imshow(input_visible, cmap="Greys")
plt.title("original (each square represent a single pixel)")
plt.subplot(232)
plt.imshow(dm.distance_map_from_binary_matrix(input), cmap="Greys", vmin=0, vmax=255)
plt.title("default")
plt.subplot(233)
plt.imshow(dm.distance_map_from_binary_matrix(input, distance="manhattan"), cmap="Greys", vmin=0, vmax=255)
plt.title("manhattan")
plt.subplot(234)
plt.imshow(dm.distance_map_from_binary_matrix(input, alpha="square", omega=255.0), cmap="Greys", vmin=0, vmax=255)
plt.title("alpha square")
plt.subplot(235)
dm.set_a(0.5)
plt.imshow(dm.distance_map_from_binary_matrix(input, alpha="linear"), cmap="Greys", vmin=0, vmax=255)
plt.title("linear 0.5*x")
plt.subplot(236)
plt.imshow(dm.distance_map_from_binary_matrix(input, distance="manhattan", alpha="linear", omega=100.0), cmap="Greys", vmin=0, vmax=255)
plt.title("manhattan linear 0.5*x omega=100")
plt.show()
if BENCHMARK:
benchmark()
if EXAMPLE:
example()