This project implements the A* pathfinding algorithm on a grid derived from an image. The user can select start and end points on the image, and the program will calculate the optimal path using the A* algorithm. The code uses computer vision techniques to create a grid representation of the image, allowing for pathfinding through this grid.
To run this project, you will need:
- Python 3.7 or higher
- OpenCV
- NumPy
- Matplotlib
Ensure these packages are installed:
pip install opencv-python-headless numpy matplotlib
The code performs the following operations:
- Image Processing and Grid Creation: Loads an image, converts it to a grayscale format, and detects edges to create a grid representation. The grid indicates obstacles based on contours in the image.
- Point Selection: Allows the user to select start and end points on the image using mouse clicks.
- Pathfinding: Implements the A* algorithm to find the shortest path between the selected start and end points.
- Visualization: Displays the original image with the selected points and the calculated path.
- Set Image Path: Change the
image_path
variable to point to the desired image on your system.image_path = r"C:\path\to\your\image.jpg"
- Run the Code: Execute the code. A Matplotlib window will open with the image.
- Select Start and End Points: Click on the image to select a start point (green) and an end point (red). The code requires two points.
- Calculate the Path: After closing the Matplotlib window, the code will calculate the path using the A* algorithm and display the image with the path highlighted in green. If no path is found, an appropriate message is displayed.
- The grid size can be adjusted via the
grid_size
variable. A smaller grid size offers finer granularity but increases computation time. - The image should contain distinguishable contours or edges to create the grid properly.
- If the code does not find a path, consider adjusting the start and end points, or check if the grid correctly represents obstacles.
- No Path Found: If no path is found, it may be due to obstacles completely blocking the way. Re-evaluate the selected start and end points or adjust the grid size.
- Invalid Image Path: Ensure the
image_path
is set to a valid image file. - Dependencies Not Installed: Double-check that you have all the required packages installed in your Python environment.
This code is provided "as-is" with no warranties. Feel free to use, modify, or distribute it as needed.