├── maps.png ├── informap.png ├── README.md └── A*.py /maps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isyiming/Astar-search-algorithm/HEAD/maps.png -------------------------------------------------------------------------------- /informap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isyiming/Astar-search-algorithm/HEAD/informap.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Astar-search-algorithm 2 | search the shortest road in an image by A* algorithm 3 | 4 | If we have a map in the form of image or 2D-array and we want to search the shortese road, 5 | one of the based way is A*, although we need a precondition is that we have abstract our map to many nodes. 6 | but if we view every pixel as a nodes , A* still could be used straightly. 7 | 8 | This code will read a image:"maps.png". in this image, zero gray value represent this pixel is a barrier. 9 | Our result will be save as a image "informap.png". I have drawn the road in it. 10 | 11 | ![Alt text](https://github.com/yimingstyle/Astar-search-algorithm/blob/master/informap.png) 12 | 13 | -------------------------------------------------------------------------------- /A*.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Mon Dec 18 12:38:58 2017 5 | 6 | @author: ming 7 | """ 8 | 9 | 10 | import cv2 11 | import numpy as np 12 | from skimage import io 13 | 14 | if __name__ == "__main__": 15 | 16 | maps=cv2.imread("maps.png",cv2.IMREAD_GRAYSCALE)#读取地图图像,灰度读入。灰度为0表示障碍物 17 | informap=cv2.imread("maps.png")#用于画路径 18 | maps_size=np.array(maps)#获取图像行和列大小 19 | hight=maps_size.shape[0]#行数->y 20 | width=maps_size.shape[1]#列数->x 21 | 22 | star={'位置':(50,50),'代价':700,'父节点':(50,50)}#起点 23 | end={'位置':(400,400),'代价':0,'父节点':(400,400)}#终点 24 | 25 | openlist=[]#open列表,存储可能路径 26 | closelist=[star]#close列表,已走过路径 27 | step_size=3#搜索步长。 28 | #步长太小,搜索速度就太慢。步长太大,可能直接跳过障碍,得到错误的路径 29 | #步长大小要大于图像中最小障碍物宽度 30 | while 1: 31 | s_point=closelist[-1]['位置']#获取close列表最后一个点位置,S点 32 | add=([0,step_size],[0,-step_size],[step_size,0],[-step_size,0])#可能运动的四个方向增量 33 | for i in range(len(add)): 34 | x=s_point[0]+add[i][0]#检索超出图像大小范围则跳过 35 | if x<0 or x>=width: 36 | continue 37 | y=s_point[1]+add[i][1] 38 | if y<0 or y>=hight:#检索超出图像大小范围则跳过 39 | continue 40 | G=abs(x-star['位置'][0])+abs(y-star['位置'][1])#计算代价 41 | H=abs(x-end['位置'][0])+abs(y-end['位置'][1])#计算代价 42 | F=G+H 43 | if H<20:#当逐渐靠近终点时,搜索的步长变小 44 | step_size=1 45 | addpoint={'位置':(x,y),'代价':F,'父节点' :s_point}#更新位置 46 | count=0 47 | for i in openlist: 48 | if i['位置']==addpoint['位置']: 49 | count+=1 50 | for i in closelist: 51 | if i['位置']==addpoint['位置']: 52 | count+=1 53 | if count==0:#新增点不在open和close列表中 54 | if maps[y,x]!=0:#非障碍物 55 | openlist.append(addpoint) 56 | t_point={'位置':(50,50),'代价':10000,'父节点':(50,50)} 57 | for j in range(len(openlist)):#寻找代价最小点 58 | if openlist[j]['代价']