YaTharThShaRma999 commited on
Commit
d15226d
·
verified ·
1 Parent(s): cfb3a93

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +42 -47
README.md CHANGED
@@ -1,48 +1,43 @@
1
- from itertools import product, chain
2
- import math
3
- import matplotlib.pyplot as plt
4
  import numpy as np
5
- import networkx as nx
6
- import cv2
7
- im = cv2.imread("/kaggle/working/black.png")
8
- im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
9
-
10
- h, w = im.shape # Get height and width of image
11
-
12
- # Add only those nodes which are black to graph
13
- nodes = [(i, j) for (i, j) in product(range(h), range(w)) if im[i, j] == 0]
14
- g = nx.Graph(nodes)
15
-
16
- # For each node there can be 8 neighbours, if you consider diagonal as well.
17
- def get_neighbors(node):
18
- box_coords = product([-1, 0, 1], [-1, 0, 1])
19
- nns = []
20
- for coord in box_coords:
21
- if coord[0] != coord[1]:
22
- nn = (node[0] - coord[0], node[1] - coord[1])
23
- nns.append(nn)
24
- return nns
25
-
26
- # A point will be a neighbour if it is black as well and is in image bounds
27
- neighbors = list(chain.from_iterable([[(node, ng_node, 1) for ng_node in get_neighbors(node) if (im[node] == 0) and (0 < ng_node[0] < h) and (0 < ng_node[1] , w)] for node in nodes]))
28
-
29
- g.add_weighted_edges_from(neighbors)
30
-
31
- # In image loaded above (0, 0) is top left point. To keep things little more generic. I select point closest to (0, 0) as start and furthest as end.
32
-
33
- min_pt = min(nodes, key=lambda x: math.hypot(x[0], x[1]))
34
- max_pt = max(nodes, key=lambda x: math.hypot(x[0], x[1]))
35
-
36
- # Now we can just use networkx to find path between two points
37
- path = nx.shortest_path(g, source=min_pt, target=max_pt)
38
-
39
- # Get new image with only shortest path
40
- im2 = 255*np.ones_like(im)
41
- # Draw the path on the original image before saving
42
- # Draw the path on the original image with white color
43
- for i in range(len(path)-1):
44
- cv2.line(im, path[i], path[i+1], color=(255), thickness=1)
45
-
46
- cv2.imwrite('image_with_path.png', im)
47
- plt.figure(figsize=(10, 10))
48
- plt.imshow(im, cmap='gray')
 
 
 
 
1
  import numpy as np
2
+ import heapq
3
+
4
+ def heuristic(a, b):
5
+ return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
6
+
7
+ def astar(array, start, goal):
8
+ neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
9
+ close_set = set()
10
+ came_from = {}
11
+ gscore = {start: 0}
12
+ fscore = {start: heuristic(start, goal)}
13
+ open_heap = [(fscore[start], start)]
14
+ array_shape = array.shape
15
+ array_get = array.__getitem__
16
+
17
+ while open_heap:
18
+ current_fscore, current = heapq.heappop(open_heap)
19
+
20
+ if current == goal:
21
+ path = []
22
+ while current in came_from:
23
+ path.append(current)
24
+ current = came_from[current]
25
+ return path[::-1]
26
+
27
+ close_set.add(current)
28
+ for i, j in neighbors:
29
+ neighbor = current[0] + i, current[1] + j
30
+
31
+ if 0 <= neighbor[0] < array_shape[0] and 0 <= neighbor[1] < array_shape[1]:
32
+ if array_get(neighbor) == 1 or neighbor in close_set:
33
+ continue # Skip obstacles and already visited nodes
34
+
35
+ tentative_g_score = gscore[current] + heuristic(current, neighbor)
36
+
37
+ if tentative_g_score < gscore.get(neighbor, float('inf')):
38
+ came_from[neighbor] = current
39
+ gscore[neighbor] = tentative_g_score
40
+ fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal)
41
+ heapq.heappush(open_heap, (fscore[neighbor], neighbor))
42
+
43
+ return None # No path found