-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_star.py
270 lines (206 loc) · 6.2 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
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
import time
start=time.time()
import numpy as nu;
import cv2;
import math;
import random;
# clear cell marked with O
# waitingList cells marked with -
class cell:
def __init__(self,r,c):
self.i=r
self.j=c
self.ngbList=[]
self.f=0
self.g=0
self.h=0
self.parent=None
self.block=False
#to fill matrix with appropriate symbols
# if(random.random()<0.1):
# self.block=True
def fillValue(self):
value[self.i,self.j]='O'
if self in waitingList:
#print("found this object in waiting list")
value[self.i,self.j]='-'
Result[self.i,self.j]=255, 120, 0
elif self in visited:
#print("found this object in visited list {} {}".format(self.i,self.j))
value[self.i,self.j]='*'
Result[self.i,self.j]=0, 127, 255
#print(value)
if self in path:
value[self.i,self.j]='P'
Result[self.i,self.j]= 127, 0, 0
if self.block == True:
value[self.i,self.j]='X'
#function to calculate neighbours
def Ngb(self):
#print("Calculating neighbours")
#print(self)
if(self.i < row-1):
#print("bottom")
self.ngbList.append(grid[self.i+1,self.j])
if(self.j < col-1):
#print("left")
self.ngbList.append(grid[self.i,self.j+1])
if(self.i > 0):
#print("top")
self.ngbList.append(grid[self.i-1,self.j])
if(self.j>0):
#print("right")
self.ngbList.append(grid[self.i,self.j-1])
def heuristic(a,b):
hueV=math.sqrt((a.i-b.i)*(a.i-b.i)+(a.j-b.j)*(a.j-b.j))
return hueV
#generating matrix from Img
img=cv2.imread('Task_1_Low.png')
a,b,c=nu.shape(img)
imgU=nu.zeros((10*a,10*b,c), nu.uint8)
Result=nu.copy(img)
ResultU=nu.copy(imgU)
# for i in range(10*a):
# for j in range(10*b):
# I=int(i/10)
# J=int(j/10)
# imgU[i,j]=img[I,J]
# Result=nu.copy(imgU)
row=a
col=b
grid=nu.empty((row,col),dtype=cell)
value=nu.empty((row,col),dtype='U')
f_value=nu.empty((row,col))
waitingList=[]
visited=[]
path=[]
path_found=False
for i in range(row):
for j in range(col):
grid[i,j]=cell(i,j)
#grid[i,j].fillValue()
#assigning the beginnning node and end node
beginning=grid[0,0]
destination=grid[row-1,col-1]
#print(img[10,10])
for p in range(row):
for q in range(col):
if img[p,q,0] == 113 and img[p,q,1] == 204 and img[p,q,2] == 45:
beginning=grid[p,q]
#img[p,q] = 255, 0, 0
if img[p,q,0] == 60 and img[p,q,1] == 76 and img[p,q,2] == 231:
destination=grid[p,q]
#img[p,q] = 3, 190, 252
if img[p,q,0] == 255 and img[p,q,1] == 255 and img[p,q,2] == 255:
grid[p,q].block=True
#img[p,q] = 252, 3, 252
#added starting point to the waiting list
waitingList.append(beginning)
#Computing neighbours
for i in range(row):
for j in range(col):
grid[i,j].Ngb()
grid[i,j].fillValue()
#print("First fill value executed")
# loop to find path and explore nodes
# grid[4,3].block=True
# grid[3,3].block=True
# grid[3,4].block=True
while(len(waitingList)>0):
#finding the optimal neighbour
current=waitingList[0]
for x in waitingList:
if x.f<current.f:
current=x
if(current==destination):
#path is found
path_found=True
print("Path found -- Reached End successfully")
print("The cost of Path was {}".format(destination.g))
#deducing the path
prev=current
path.append(prev)
while(prev.parent!=None):
path.append(prev.parent)
prev=prev.parent
break
#removing items from waiting list and adding it to visited list
waitingList.remove(current)
visited.append(current)
current.fillValue()
#print(value)
#print("I passed {} {}".format(current.i,current.j))
# current=waitingList[0]
# visited.append(waitingList[0])
# waitingList.pop(0)
# cNgb=current.ngbList
Cngb=current.ngbList
for x in Cngb:
tG=0
if x not in visited and x.block == False :
tG = current.g + 1
#if node has been explored but not closed
if x in waitingList:
if tG < x.g:
x.g=tG
else:
#if node is still unexplored
x.g=tG
waitingList.append(x)
x.h=heuristic(x,destination)
x.f=x.g+x.h
x.parent=current
#print("While loop fill value executed")
if path_found==False:
print("We couldn't find a path")
for i in range(row):
for j in range(col):
grid[i,j].fillValue()
#print("last and final fill value executed")
#Upscaling Image
for i in range(10*a):
for j in range(10*b):
I=int(i/10)
J=int(j/10)
ResultU[i,j]=Result[I,J]
end=time.time()
print("Time = {}".format((end-start)))
cv2.imshow('Identified things ',ResultU)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('Result.jpg',ResultU)
# grid[0,0].Ngb()
# grid[1,1].Ngb()
#Highlighting neighbours of cell 1
#lst=grid[1,1].ngbList
# lst1=grid[0,0].ngbList
# for x in lst:
# value[x.i,x.j]='*'
# grid[0,0].f=2
# print(grid)
# print(waitingList)
#print(value)
#print(len(lst1))
#print(len(lst))
# print(grid[0,0].f)
# print(grid[1,1].f)
# a,b=grid.shape
# print(a)
# print(b)
# img=cv2.imread('Task_1_Low.png')
# a,b,c=nu.shape(img)
# imgU=nu.zeros((10*a,10*b,c), nu.uint8)
# for i in range(10*a):
# for j in range(10*b):
# I=int(i/10)
# J=int(j/10)
# imgU[i,j]=img[I,J]
# print("The dimensions of upscaled image is")
# print(nu.shape(imgU))
# cv2.imshow('Image', img)
# cv2.imshow('Upscaled Image',imgU)
# cv2.imwrite('Result.jpg',imgU)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# while(len(waitingList)>0):
# break