-
Notifications
You must be signed in to change notification settings - Fork 28
/
A-star-potential-field-hybrid-type-1_1.py
531 lines (445 loc) · 15.4 KB
/
A-star-potential-field-hybrid-type-1_1.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
'''
In this method , we are trying to find offline path first using A* algorithm, then we use Artificial potential method
to reach local goals on A* planned path.
Suppose, a1, a2, a3, ....... an be path we get from A*, then to potential field we give following as consecutive goals:
a(i), a(i+4), a(i+8), a(i+12), ..... a(i+n)
Hence this way we are using power of both A-star and potential field :)
'''
import cv2
import numpy as np
from time import sleep
import copy
import glob
import math
import time
import Queue as Q
def printx(x):
#print x
pass
'''
function definition from A-star
start
'''
class pixel1(object):
def __init__(self, penalty, pointx, pointy, parent, h): # parent is that pixel from which this current pixel is generated
self.penalty = penalty
self.pointx = int(pointx)
self.pointy = int(pointy)
self.parent = parent
self.h = h #heuristic
def __cmp__(self, other): # comparable which will return self.penalty<other.penalty
return cmp(self.penalty+self.h, other.penalty+other.h)
def feasibility(nx, ny, img): # function to check if pixel lies in obstacle
if img[nx, ny, 0] == 255:
return False
else:
return True
def penalty1(clearance):
alpha = 10000
sigma_sqr = 1000
return alpha*math.exp((-1)*clearance*clearance/sigma_sqr)
def cost(ox, oy, nx, ny, penalty, clearance): #ox, oy:- old points nx, ny :- new points
return penalty + math.sqrt((ox-nx)*(ox-nx)+ (oy-ny)*(oy-ny))*(1+penalty1(clearance))
def heuristic(nx, ny,dx, dy): #ox, oy:- old points nx, ny :- new points
return math.sqrt((nx-dx)*(nx-dx)+ (ny-dy)*(ny-dy))
def check_boundaries1(ex, ey, nx, ny): #ex, ey :- end points of frame
if nx > -1 and ny > -1 and nx < ex and ny < ey:
return True
else:
return False
def bfs(arr, sx, sy, dx, dy, final_contours): # sx, sy :- source coordinates dx, dy :- destination coordinates
q = Q.PriorityQueue()
temp1 = True
temp2 = True
for cnt in final_contours:
if cv2.pointPolygonTest(cnt, (sx, sy), False) > -1:
temp1 = False
for cnt in final_contours:
if cv2.pointPolygonTest(cnt, (dx, dy), False) > -1:
temp2 = False
if temp1 == False or temp2 == False:
return []
actions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]
solution = []
ex, ey, ez = arr.shape
#visit = [[False for x in range(ey)] for x in range(ex)]
dist = [[10000 for x in range(ey)] for x in range(ex)]
distplusHeuristic = [[10000 for x in range(ey)] for x in range(ex)]
q.put(pixel1(0, sx, sy, None, heuristic(sx, sy, dx, dy)))
dist[sx][sy] = 0
distplusHeuristic[sx][sy] = dist[sx][sy]+heuristic(sx, sy, dx, dy)
s = time.clock()
cnt = 0
cntq = 0
while not q.empty():
p = q.get()
x = int(p.pointx)
y = int(p.pointy)
pen = p.penalty
h = p.h
cnt = cnt+1
if dist[x][y] < pen:
continue
if x == dx and y == dy:
while p is not None:
solution.append([p.pointx, p.pointy])
p = p.parent
#print 'time : ', time.clock()-s
#print cnt, cntq
return solution
for i in range(len(actions)):
nx = int(actions[i][0] + x)
ny = int(actions[i][1] + y)
if check_boundaries1(ex, ey, nx, ny) == True:
#if arr.item(nx, ny, 0) == 0 and arr.item(nx, ny, 1) == 0 and arr.item(nx, ny, 2) == 0:
pen = dist[x][y]
pen_new = cost(x, y, nx, ny, pen, arr[nx][ny][0])
h_new = heuristic(nx, ny, dx, dy)
if dist[nx][ny] > pen_new :
dist[nx][ny] = pen_new
nx = int(nx)
ny = int(ny)
if distplusHeuristic[nx][ny] > dist[nx][ny]+h_new :
distplusHeuristic[nx][ny] = dist[nx][ny] + h_new
cntq = cntq+1
q.put(pixel1(pen_new, nx, ny, p, h_new))
#print 'time : ', time.clock()-s
return []
'''
function definition from A-star
end
'''
'''
function definition from Clearance-feasibility
start
'''
class pixel(object):
def __init__(self, penalty, pointx, pointy): # parent is that pixel from which this current pixel is generated
self.penalty = penalty
self.pointx = int(pointx)
self.pointy = int(pointy)
def __cmp__(self, other): # comparable which will return self.penalty<other.penalty
return cmp(self.penalty, other.penalty)
images = glob.glob('*.jpg')
def penalty(ox, oy, nx, ny, penalty): #ox, oy:- old points nx, ny :- new points
return penalty + math.sqrt((ox-nx)*(ox-nx)+ (oy-ny)*(oy-ny))
def check_boundaries(ex, ey, nx, ny): #ex, ey :- end points of frame
if nx > -1 and ny > -1 and nx < ex and ny < ey:
return True
else:
return False
def fill_clearance(arr,cmax, final_contours): # sx, sy :- source coordinates dx, dy :- destination coordinates
q = Q.PriorityQueue()
actions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]
ex, ey, ez = arr.shape
#print ex, ey, ez
min_cost = [[100000 for x in range(ey)] for x in range(ex)]
for cnt in final_contours:
for pts in cnt:
q.put(pixel(0, pts[0, 1], pts[0, 0]))
cnt = 0
cntq = 0
while not q.empty():
p = q.get()
x = int(p.pointx)
y = int(p.pointy)
pen = p.penalty
if p.penalty > cmax:
continue
if min_cost[x][y] <= p.penalty:
continue
min_cost[x][y] = p.penalty
for i in range(len(actions)):
nx = int(actions[i][0] + x)
ny = int(actions[i][1] + y)
if check_boundaries(ex, ey, nx, ny) == True:
if arr.item(nx, ny, 0) == 0 and arr.item(nx, ny, 1) == 0 and arr.item(nx, ny, 2) == 0:
if min_cost[nx][ny] > penalty(x, y, nx, ny, pen):
q.put(pixel(penalty(x,y,nx,ny,pen), nx, ny))
return min_cost
'''
function definition from Clearance-feasibility
end
'''
'''
function definition from Artificial Potential potential starts
'''
def check_obstacles(arr, ansx, ansy): #function to check whether a given point is on obstacle or not
if arr[ansx][ansy][0] == 255:
return True
else:
return False
def feasible(arr, x, y): #function to check if a point is feasible or not
ex, ey, ez = arr.shape
x = int(x)
y = int(y)
if check_boundaries(ex, ey, x, y):
return not check_obstacles(arr, x, y)
else:
return False
def dist(sx, sy, x, y, theta, arr, q_star): #distance of obstacle in direction theta in radians
ansx = sx
ansy = sy
flag = True
count = 1
while True:
if count > q_star:
return (-1, -1)
ansx = sx + count*math.sin(theta)
ansy = sy + count*math.cos(theta)
if check_boundaries(x, y, ansx, ansy) == False:
break
else:
if check_obstacles(arr, ansx, ansy) == True:
break
count += 1
return (ansx-sx,ansy- sy)
def obstacle_force(arr, sx, sy, q_star, theta1): #sx,sy :- source dx, dy:- destination q-star:- threshold distance of obstacles
forcex = 0
forcey = 0
neta = 30000000000000
x, y , z= arr.shape
for i in range(-8, 9):
(ox,oy) = dist(sx, sy, x, y, (theta1 + i*math.pi/16 + 2*math.pi)%(2*math.pi), arr, q_star)
theta = (theta1 + i*math.pi/16 + 2*math.pi)%(2*math.pi)
fx = 0
fy = 0
#print 'ox ', ox, 'oy ', oy
if ox == -1 or oy == -1:
fx = 0
fy = 0
else:
ox = math.fabs(ox)
oy = math.fabs(oy)
d = math.hypot(ox, oy)
if d == 0:
d = 1
f = (neta*(1.0/q_star- 1.0/d))/(d*d)
fx = f*math.sin(theta)
fy = f*math.cos(theta)
forcex += fx
forcey += fy
thet = math.atan2(forcex, forcey)
arr1 = arr
cv2.line(arr1, (sy, sx), (int(sy + 10*math.cos(thet)), int(sx + math.sin(thet))), (0, 255, 255), 1)
cv2.imshow('arr', arr1)
k = cv2.waitKey(20)
return (forcex, forcey)
def goal_force(arr, sx, sy, dx, dy, d_star): # sx, sy :- source dx, dy:- destination d_star:- threshold distance from goal
forcex = 0
forcey = 0
tau = 100000000 #constant
printx('10')
d = math.sqrt((dx-sx)*(dx-sx) + (dy-sy)*(dy-sy))
if d > d_star:
forcex += ((d_star*tau*math.sin(math.atan2(dx-sx, dy-sy))))
forcey += ((d_star*tau*math.cos(math.atan2(dx-sx, dy-sy))))
else:
forcex += ((dx-sx)*tau)
forcey += ((dy-sy)*tau)
printx('11')
return (forcex, forcey)
def path_planning(arr, sx1, sy1, dx, dy, theta):
'''
:param arr: input map
:param sx1: source x
:param sy1: source y
:param dx: destination x
:param dy: destination y
:return: path
'''
#Parameters Declaration
flx = 10000 #maximum total force in x
fly = 10000 #maximum total force in y
v = 1 #velocity magnitude
t = 1 #time lapse
#theta = 0 #initial angle
x,y,z = arr.shape
theta_const = math.pi*30/180 #maximum allowed turn angle
q_star = 10
d_star = 2
if arr[sx1][sy1][0] == 255 or arr[dx][dy][0] == 255:
return []
sx = sx1
sy = sy1
sol = []
sol.append((sx, sy))
sx += int(v*math.sin(theta))
sy += int(v*math.cos(theta))
sol.append((sx, sy))
'''
if Q and P are two vectors and @ is angle between them
resultant ,R = (P^2 + R^2 + 2*P*Q cos @)^(1/2)
resultant, theta = atan((Q*sin @)/(P+Q*cos @))
'''
#count = 0
while True:
#count += 1
(fx, fy) = obstacle_force(arr, sx, sy, q_star, theta)
(gx, gy) = goal_force(arr, sx, sy, dx, dy, d_star)
tx = gx+fx
ty = gy+fy
if(tx < 0):
tx = max(tx, -flx)
else:
tx = min(tx, flx)
if(ty < 0):
ty = max(ty, -fly)
else:
ty = min(ty, fly)
theta1 = math.atan2(tx, ty)
if arr[sx][sy][0] == 255:
print gx, gy, fx, fy
print 'tx ', tx, ' ty ', ty, 'sx ', sx, ' sy ', sy
print theta1*180/math.pi, theta*180/math.pi
sleep(10)
P = v
angle = theta1-theta #angle between velocity and force vector
Q = math.sqrt(tx*tx + ty*ty)
theta2 = math.atan2((Q*math.sin(angle)),((P + Q*math.cos(angle)))) #resultant angle with velocity
if theta2 < 0:
theta2 = max(theta2, -theta_const)
else:
theta2 = min(theta2, theta_const)
theta += theta2
theta = (theta + 2*math.pi)%(2*math.pi)
sx = sx + v*math.sin(theta)
sy = sy + v*math.cos(theta)
sx = int(sx)
sy = int(sy)
if not check_boundaries(x, y, sx, sy):
print 'out of boundaries' , sx, sy
return sol
sol.append((sx, sy))
if sx < dx+ 2 and sx > dx - 2 and sy < dy+2 and sy > dy-2:
break
sol.append((theta))
return sol
'''
function definition from Artificial Potential potential starts
'''
def print_path_to_file(sol):
with open('path.txt', 'w') as f:
for i in range(len(sol)):
f.write(`sol[i][0]` + ' ' + `sol[i][1]` + '\n')
print sol[i][0]
def read_path_from_file():
sol = []
with open('path.txt', 'r') as f:
data = f.readlines()
for line in data:
s = []
words = line.split()
for j in words:
s.append(int(j))
sol.append(s)
return sol
def final_path(sol, arr):
l = len(sol)
print l
div = 150
arr1 = copy.copy(arr)
start = 0
end = div
solution = []
theta = 0
while start < l-1:
print sol[start][0], sol[start][1], sol[end][0], sol[end][1]
ret = path_planning(arr, sol[start][0], sol[start][1], sol[end][0], sol[end][1], theta)
for i in range(len(ret)-1):
solution.append(ret[i])
l1 = len(ret)-1
theta= ret[-1]
'''
if l1 > 2:
x1 = ret[l1-1][0]
x2 = ret[l1-2][0]
y1 = ret[l1-1][1]
y2 = ret[l1-2][1]
theta = math.atan2(x1-x2, y1-y2)
cv2.line(arr1, (int(y1), int(x1)), (int(y2), int(x2)), (255, 0, 0), 1)
cv2.imshow('img', arr1)
k = cv2.waitKey(0)
'''
start = end
end += div
if end > l-1:
end = l-1
for i in solution:
print i
return solution
def main():
counter = 1
for im in images:
img = cv2.imread(im)
cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img2 = cv2.medianBlur(cimg,13)
ret,thresh1 = cv2.threshold(cimg,100,120,cv2.THRESH_BINARY)
t2 = copy.copy(thresh1)
x, y = thresh1.shape
arr = np.zeros((x, y, 3), np.uint8)
arr1 = np.zeros((x, y, 3), np.uint8)
final_contours= []
image, contours, hierarchy = cv2.findContours(t2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
cnt = contours[i]
if cv2.contourArea(cnt) > 1000 and cv2.contourArea(cnt) < 15000 :
cv2.drawContours(img, [cnt],-1, [0, 255, 255])
cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
cv2.fillConvexPoly(arr1, cnt, [255, 255, 255])
final_contours.append(cnt)
cmax = 50
start = time.clock()
min_cost = fill_clearance(arr,cmax, final_contours)
print 'time: ', time.clock()-start
'''
for i in xrange(x):
for j in xrange(y):
if min_cost[i][j] == 100000:
min_cost[i][j] = 0;
'''
for i in xrange(x):
for j in xrange(y):
pix_val = int(5*min_cost[i][j])
if(min_cost[i][j] > 10000):
pix_val = 255
arr[i, j] = (pix_val, pix_val, pix_val)
for cnt in final_contours:
cv2.fillConvexPoly(arr, cnt, [0, 0, 0])
'''
Code from A-star.py
'''
sx = 50 # raw_input("Enter source and destination Coordinates")
sy = 50 # raw_input()
dx = 450 # raw_input()
dy = 900 # raw_input()
sol = bfs(arr, sx, sy, dx, dy, final_contours)
#print_path_to_file(sol)
#sol = read_path_from_file()
#print sol
solution = final_path(sol, arr1)
#print solution
if len(solution) == 0:
print 'No solution from source to destination'
else:
for i in range(len(solution)):
start = (solution[i][1], solution[i][0])
cv2.circle(arr,start, 1, [255, 255, 255])
cv2.circle(img, start, 1, [255, 255, 255])
for i in range(len(sol)):
start = (sol[i][1], sol[i][0])
cv2.circle(arr,start, 1, [255, 0, 0])
cv2.circle(img, start, 1, [255, 0, 0])
cv2.circle(arr, (sy, sx), 2, [0, 255, 0])
cv2.circle(arr, (dy, dx), 2, [0, 255, 0])
cv2.circle(img, (sy, sx), 2, [0, 255, 0])
cv2.circle(img, (dy, dx), 2, [0, 255, 0])
output = "output1/"+`counter`
output += ".jpg"
cv2.imwrite(output, img)
counter += 1
cv2.imshow('image', img)
cv2.imshow('arr', arr)
cv2.waitKey(0)
cv2.destroyAllWindows()
main()