forked from cosmonautd/Aerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphmap.py
210 lines (168 loc) · 7.13 KB
/
graphmap.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
""" The graphmap module provides methods to generate
traversability graphs from traversability matrices.
Tools for path computation are also be provided.
Class RouteEstimator: defines a route estimator and its configuration.
Method tdi2graph: builds a graph from a traversability matrix
Method route: returns the best route between two regions
Method draw_graph: saves a graph as an image (optionally, draws paths)
"""
import cv2
import numpy
import matplotlib
import graph_tool as graphtool
import graph_tool.draw as draw
import graph_tool.search as search
def coord2(position, columns):
""" Converts two-dimensional indexes to one-dimension coordinate
"""
return position[0]*columns + position[1]
def get_keypoints(image, grid):
"""
"""
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Set up the SimpleBlobdetector with default parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 0
params.maxThreshold = 256
# Filter by Area.
params.filterByArea = True
params.minArea = 20
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.5
# Filter by Inertia
params.filterByInertia =True
params.minInertiaRatio = 0.5
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
reversemask=255-image
keypoints = detector.detect(reversemask)
indexes = list()
for keypoint in keypoints:
x = int(keypoint.pt[0])
y = int(keypoint.pt[1])
size = int(keypoint.size)
for i, (tlx, tly,sqsize) in enumerate(grid):
if tlx <= x and x < tlx + sqsize:
if tly <= y and y < tly + sqsize:
indexes.append(i)
return indexes
def draw_graph(G, filename="traversability-graph.png", path=[]):
G.vp.vfcolor = G.new_vertex_property("vector<double>")
G.ep.ecolor = G.new_edge_property("vector<double>")
G.ep.ewidth = G.new_edge_property("int")
for v in G.vertices():
diff = G.vp.traversability[v]
G.vp.vfcolor[v] = [1/(numpy.sqrt(diff)/100), 1/(numpy.sqrt(diff)/100), 1/(numpy.sqrt(diff)/100), 1.0]
for e in G.edges():
G.ep.ewidth[e] = 6
G.ep.ecolor[e] = [0.179, 0.203, 0.210, 0.8]
for i, v in enumerate(path):
G.vp.vfcolor[v] = [0, 0.640625, 0, 0.9]
if i < len(path) - 1:
for e in v.out_edges():
if e.target() == path[i+1]:
G.ep.ecolor[e] = [0, 0.640625, 0, 1]
G.ep.ewidth[e] = 10
draw.graph_draw(G, pos=G.vp.pos, output_size=(1200, 1200), vertex_color=[0,0,0,1], vertex_fill_color=G.vp.vfcolor,\
edge_color=G.ep.ecolor, edge_pen_width=G.ep.ewidth, output=filename, edge_marker_size=4)
class Visitor(search.DijkstraVisitor):
"""
"""
def __init__(self, target=None):
if target is not None:
self.target = target
def finish_vertex(self, v):
if v == self.target:
raise graphtool.search.StopSearch()
class RouteEstimator:
"""
"""
def __init__(self, c=0.7):
self.c = c
def tm2graph(self, tdmatrix):
G = graphtool.Graph(directed=True)
G.vp.pos = G.new_vertex_property("vector<double>")
G.vp.traversability = G.new_vertex_property("double")
G.vp.cut = G.new_vertex_property("bool")
G.ep.weight = G.new_edge_property("double")
for i, row in enumerate(tdmatrix):
for j, element in enumerate(row):
v = G.add_vertex()
G.vp.pos[v] = [j, i]
if tdmatrix[i][j] == 0:
G.vp.traversability[v] = float('inf')
else:
G.vp.traversability[v] = (100*(1/tdmatrix[i][j]))**2
if G.vp.traversability[v] > (100*(1/self.c))**2:
G.vp.cut[v] = True
else:
G.vp.cut[v] = False
edges = list()
for v in [vv for vv in G.vertices() if not G.vp.cut[vv]]:
(i, j) = G.vp.pos[v][1], G.vp.pos[v][0]
top, bottom, left, right = (i-1, j), (i+1, j), (i, j-1), (i, j+1)
if i-1 > -1:
u = G.vertex(coord2(top, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if i+1 < tdmatrix.shape[0]:
u = G.vertex(coord2(bottom, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if j-1 > -1:
u = G.vertex(coord2(left, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if j+1 < tdmatrix.shape[1]:
u = G.vertex(coord2(right, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
topleft, topright, bottomleft, bottomright = (i-1, j-1), (i-1, j+1), (i+1, j-1), (i+1, j+1)
if i-1 > -1 and j-1 > -1:
u = G.vertex(coord2(topleft, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if i-1 > -1 and j+1 < tdmatrix.shape[1]:
u = G.vertex(coord2(topright, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if i+1 < tdmatrix.shape[0] and j-1 > -1:
u = G.vertex(coord2(bottomleft, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
if i+1 < tdmatrix.shape[0] and j+1 < tdmatrix.shape[1]:
u = G.vertex(coord2(bottomright, tdmatrix.shape[1]))
if not G.vp.cut[u]:
edges.append((v, u, G.vp.traversability[v] + G.vp.traversability[u]))
G.add_edge_list(edges, eprops=[G.ep.weight])
del edges
return G
def map_from_source(self, G, source):
source = G.vertex(source)
dist, pred = search.dijkstra_search(G, G.ep.weight, source, Visitor())
return dist, pred
def route(self, G, source, target, dist=None, pred=None):
source = G.vertex(source)
target = G.vertex(target)
if dist and pred:
pass
else:
dist, pred = search.dijkstra_search(G, G.ep.weight, source, Visitor(target))
path = list()
found = False
v = target
if G.vertex(pred[target]) and dist[target] != float('inf'):
found = True
path.append(target)
while v != source:
v = G.vertex(pred[v])
path.append(v)
if len(path) == 0:
path.append(target)
path.append(source)
return path[::-1], found