import sys from random import choice import numpy as np import matplotlib.pyplot as plt from image_loader import load_image from navigation import movement, skeleton # Load the image print "Loading image..." data = load_image(sys.argv[1]) print "Generating skeleton..." skel = skeleton.make_skeleton(data, mindist=10) # Draw! plt.figure() plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest') # Compute the paths print "Finding paths..." start = (1, 132) paths = movement.find_all_paths(start, skel) closest_path = movement.find_closest_path(start, paths) print "Plotting!" for path in paths: color = np.random.rand(3) plt.plot(*path.end, color=color, marker='o') plt.plot(*path.points, color=color, marker=',')
(x, y) = q.pop() if data[y, x] == target_color and not mask[y, x] and distance.euclidean(center, (x, y)) < max_range: mask[y, x] = True q.append((x - 1, y)) q.append((x + 1, y)) q.append((x, y - 1)) q.append((x, y + 1)) return mask masked = np.zeros_like(data) mask = mask_fill_range(data, location, target_color=255, max_range=100) masked = masked | np.where(mask, data, 0) skel = skeleton.make_skeleton(masked, mindist=10) navigator = movement.Navigator(location, skel) # Iterate clock = pygame.time.Clock() while True: clock.tick(60) screen.blit(background, (0, 0)) mask = mask_fill_range(data, location, target_color=255, max_range=100) masked = masked | np.where(mask, data, 0) for (y, x), v in np.ndenumerate(masked): # screen.fill((v, v, v), rect=(x, y, 1, 1)) screen.set_at((x, y), (v, v, v))