-
Notifications
You must be signed in to change notification settings - Fork 3
/
A-star.py
185 lines (160 loc) · 6.19 KB
/
A-star.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
import cv2
import numpy as np
import copy
import glob
import math
import Queue as Q
import time
import matplotlib.pyplot as plt
import scipy as sp
from scipy.interpolate import interp1d
class pixel(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)
images = glob.glob('*.jpg')
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 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 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_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 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(pixel(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_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:
pen = dist[x][y]
pen_new = penalty(x, y, nx, ny, pen)
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(pixel(pen_new, nx, ny, p, h_new))
print 'time : ', time.clock()-s
return []
def main():
counter = 1
for im in images:
img = cv2.imread(im)
cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
output = 'BinaryImages/' + `counter`
output += ".jpg"
cv2.imwrite(output, cimg)
img2 = cv2.medianBlur(cimg,13)
output = 'AfterMedianBlurring/' + `counter`
output += ".jpg"
cv2.imwrite(output, img2)
ret,thresh1 = cv2.threshold(cimg,100,120,cv2.THRESH_BINARY)
output = 'Afterthresholding/' + `counter`
output += ".jpg"
cv2.imwrite(output, thresh1)
t2 = copy.copy(thresh1)
x, y = thresh1.shape
print x, y
arr = np.zeros((x, y, 3), np.uint8)
final_contours= []
image, contours, hierarchy = cv2.findContours(t2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
arr2 = np.zeros((x,y,3), np.uint8)
for i in range(len(contours)):
cnt = contours[i]
cv2.drawContours(arr2, [cnt],-1, [255, 255, 255])
output = 'DetectionOfContours/' + `counter`
output += ".jpg"
cv2.imwrite(output, arr2)
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])
final_contours.append(cnt)
output = 'DetectionOfObstacles/' + `counter`
output += ".jpg"
cv2.imwrite(output, arr)
output = 'beforeplanningwithoutclearance/' + `counter`
output += ".jpg"
cv2.imwrite(output, arr)
sx = 20 # raw_input("Enter source and destination Coordinates")
sy = 20 # raw_input()
dx = 500 # raw_input()
dy = 1000 # raw_input()
# s = time.clock()
solution = bfs(arr, sx, sy, dx, dy, final_contours)
# print 'time: ', time.clock()-s
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, 0, 255])
cv2.circle(img, start, 1, [255, 255, 255])
output = "withoutClearance/"+`counter`
output += ".jpg"
cv2.imwrite(output, img)
counter += 1
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])
cv2.imshow('image', img)
cv2.imshow('arr', arr)
cv2.waitKey(0)
cv2.destroyAllWindows()
main()