-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_finder_finalv1.py
315 lines (230 loc) · 10.5 KB
/
path_finder_finalv1.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
import pygame
import sys
import random
from math import sqrt
import cv2
import numpy as np
from PIL import Image
# Constants
WIDTH, HEIGHT = 750, 750
ROWS, COLS = 250,250
CELL_WIDTH = WIDTH // COLS
CELL_HEIGHT = HEIGHT // ROWS
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (200, 200, 200)
RED = (255,0,0)
YELLOW= (255,255,0)
# Create grid and cellBlocked matrix
grid = [[{'x': row, 'y': col, 'is_blocked': False, 'is_visited': False, 'distance': float('inf') , 'previous' : None}
for col in range(COLS)] for row in range(ROWS)]
#cellBlocked = [[False for _ in range(COLS)] for _ in range(ROWS)] # Initialize all cells as unblocked
#grid[2][3]['is_blocked'] = True
def display_multiline_text(window, text, position, font_size=21, color=BLACK):
font = pygame.font.Font(None, font_size)
lines = text.split("|") # Split text into lines based on a separator character "|"
y_offset = 0
for line in lines:
text_surface = font.render(line, True, color)
window.blit(text_surface, (position[0], position[1] + y_offset))
y_offset += font_size # Adjust the Y offset for each line
#print(grid)
# Pygame setup
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Grid")
font = pygame.font.SysFont(None, 20)
def add_random_blocks(probability):
for row in range(ROWS):
for col in range(COLS):
if random.random() < probability: # Using probability to randomly block cells
grid[row][col]['is_blocked'] = True
#add_random_blocks(0.05)
def draw_grid():
window.fill(WHITE)
for row in range(ROWS):
for col in range(COLS):
if grid[row][col]['is_blocked']:
pygame.draw.rect(window, RED, (col * CELL_WIDTH, row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
else:
pygame.draw.rect(window, GREY, (col * CELL_WIDTH, row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT), 1)
if grid[row][col]['is_visited']:
pygame.draw.rect(window, YELLOW, (col * CELL_WIDTH, row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT), 1)
cell_info = f'({grid[row][col]["x"]}, {grid[row][col]["y"]}) | V: {grid[row][col]["is_visited"]} | D: {grid[row][col]["distance"]}'
#display_multiline_text(window, cell_info, (col * CELL_WIDTH + 5, row * CELL_HEIGHT + 5)) # Adjust position for better alignment
#pygame.display.update()
def draw_cell(x,y,color=(0,255,22)):
pygame.draw.rect(window, color, (y * CELL_WIDTH, x * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
running = True
start = [10,10]
start = [ROWS-1,COLS-1]
start = [ROWS-1,int(COLS/2)]
end = [ROWS-1,COLS-1]
end = [2,2]
end = [0,int(COLS/2)]
#please uncomment this to run properly without mouse clcik
#grid[start[0]][start[1]]['distance']=0
grid[start[0]][start[1]]['is_blocked'] = False
grid[end[0]][end[1]]['is_blocked'] = False
index = start
def convert_img(image_path = "United3.png"):
# Replace 'United3.png' with the path to your image file
# Load the image using OpenCV
image = cv2.imread(image_path)
# Check if the image is loaded successfully
if image is not None:
#cv2.imshow('Image', image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
# Define the road color range#250,236,192#rgba(234,203,138,255)#250,238,192
lower_road_color = np.array([0,0, 240])
upper_road_color = np.array([255, 255, 255])
# Mask the image to extract road pixels
mask = cv2.inRange(image, lower_road_color, upper_road_color)
result = cv2.bitwise_and(image, image, mask=mask)
grayscale_image = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
# Create a kernel for dilation
kernel = np.ones((5, 5), np.uint8)
# Perform dilation on the black and white image
dilated_image = cv2.dilate(grayscale_image, kernel, iterations=1)
cv2.imwrite("dilated_image.png", dilated_image)
return "dilated_image.png"
#return dilated_image
else:
print("Failed to load image") # Add this line to indicate failure in loading the image
return None
#img part start
#image_path = "Untitled5.png"
initial_img = "United3.png"
initial_img = "United7.png"
image_path = convert_img(initial_img)
image = pygame.image.load(image_path)
img_width, img_height = image.get_size()
# Resize the image to match grid dimensions
resized_image = pygame.transform.scale(image, (COLS, ROWS))
resized_image2 = pygame.transform.scale(image, (COLS*CELL_WIDTH, ROWS*CELL_HEIGHT))
image1 = pygame.image.load(initial_img)
resized_image1 = pygame.transform.scale(image1, (COLS*CELL_WIDTH, ROWS*CELL_HEIGHT))
# Convert the resized image to a 2D array of pixel values
def please_delte():
for row in range(ROWS):
for col in range(COLS):
pixel_color = resized_image.get_at((col, row)) # Get pixel color at (x, y)
#if pixel_color == (0, 0, 0, 255): # Check for black (assuming alpha is 255 for opaque)
# grid[row][col]['is_blocked'] = True
if not pixel_color == (255, 255, 255, 255): # Check for black (assuming alpha is 255 for opaque)
grid[row][col]['is_blocked'] = True
color_lower_range = (0, 0, 0) # Example lower range (R, G, B)
color_upper_range = ( 10, 10, 10) # Example upper range (R, G, B)
for row in range(ROWS):
for col in range(COLS):
pixel_color = resized_image.get_at((col, row)) # Get pixel color at (x, y)
# Check if the pixel color is within the defined range
if (color_lower_range[0] <= pixel_color[0] <= color_upper_range[0] and
color_lower_range[1] <= pixel_color[1] <= color_upper_range[1] and
color_lower_range[2] <= pixel_color[2] <= color_upper_range[2]):
grid[row][col]['is_blocked'] = True
#img part end
def update_neghibour_distance(x,y,index_distance):
grid[x][y]['is_visited'] = True
if x > 0 and not grid[x - 1][y]['is_blocked'] and not grid[x - 1][y]['is_visited']:
if index_distance + 1 <grid[x - 1][y]['distance']:
grid[x - 1][y]['distance'] =index_distance + 1
grid[x - 1][y]['previous'] = (x,y)
if x < ROWS - 1 and not grid[x + 1][y]['is_blocked'] and not grid[x + 1][y]['is_visited']:
if index_distance + 1 <grid[x + 1][y]['distance']:
grid[x + 1][y]['distance'] =index_distance + 1
grid[x + 1][y]['previous'] = (x,y)
if y > 0 and not grid[x][y - 1]['is_blocked'] and not grid[x][y - 1]['is_visited']:
if index_distance + 1 < grid[x][y - 1]['distance']:
grid[x][y - 1]['distance'] =index_distance + 1
grid[x][y - 1]['previous'] = (x,y)
if y < COLS - 1 and not grid[x][y + 1]['is_blocked'] and not grid[x][y + 1]['is_visited']:
if index_distance + 1 <grid[x][y + 1]['distance']:
grid[x][y + 1]['distance'] =index_distance + 1
grid[x][y + 1]['previous'] = (x,y)
# Your existing code...
def backline(last,color=(0, 255, 0)):
if grid[last[0]][last[1]]['is_visited']:
path = []
current = last
while current:
path.append(current)
current = grid[current[0]][current[1]]['previous']
# Drawing the shortest path
for node in path:
draw_cell(node[0], node[1], color) # Green color for the shortest path
pygame.display.update()
def distance(a, b):
return sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
skip = False
reached = False
initial_selcted = False
end_selected = False
img_active= 1 #1 or 2 or 3
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
img_active = img_active+1
if img_active>3:
img_active = 1
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
row = mouse_y // CELL_HEIGHT
col = mouse_x // CELL_WIDTH
#print (pygame.mouse.get_pos())
print((row,col))
if not initial_selcted :
#start[0] = row
#start[1] = col
start = (row,col)
grid[start[0]][start[1]]['distance']=0
initial_selcted = True
index = start
print (initial_selcted)
elif not end_selected:
end[0] = row
end[1] = col
end_selected = True
print (end_selected)
#if not (row == start[0] and col == start[1]) and not (row == end[0] and col == end[1]):
# grid[row][col]['is_blocked'] = True
min_distance = float('inf')
min_node = None
# Finding the unvisited node with the minimum distance
ATTRACTION_FACTOR = 1.4
ATTRACTION_FACTOR =5
ATTRACTION_FACTOR = 1
if not skip and initial_selcted==True and end_selected ==True:
for row in range(ROWS):
for col in range(COLS):
if not grid[row][col]['is_visited'] and (grid[row][col]['distance'] + ATTRACTION_FACTOR* distance( (row, col) , (end[0],end[1]))) < min_distance:
min_distance = grid[row][col]['distance'] + ATTRACTION_FACTOR* distance( (row, col) , (end[0],end[1]))
min_node = (row, col)
if row == end[0] and col == end[1]:
skip = True # if skip == true , when it reach the destination it will stop
#skip = false ; i want more try3
print("Reached")
reached= True
if min_node:
index = min_node
update_neghibour_distance(index[0],index[1],grid[index[0]][index[1]]['distance'])
backline(min_node)
else:
t=1
#backline(end)
draw_grid()
if img_active==2:
window.blit(resized_image2, (0, 0))
elif img_active==1:
window.blit(resized_image1, (0, 0))
draw_cell(start[0], start[1])
draw_cell(end[0], end[1])
backline((end[0],end[1]))
pygame.display.update()