Go to the documentation of this file.00001
00002
00003
00004
00005 from copy import copy
00006 import rospy
00007 from nav_msgs.msg import OccupancyGrid
00008
00009 import numpy as np
00010 import cv2
00011
00012
00013
00014 def getfrontier(mapData):
00015 data=mapData.data
00016 w=mapData.info.width
00017 h=mapData.info.height
00018 resolution=mapData.info.resolution
00019 Xstartx=mapData.info.origin.position.x
00020 Xstarty=mapData.info.origin.position.y
00021
00022 img = np.zeros((h, w, 1), np.uint8)
00023
00024 for i in range(0,h):
00025 for j in range(0,w):
00026 if data[i*w+j]==100:
00027 img[i,j]=0
00028 elif data[i*w+j]==0:
00029 img[i,j]=255
00030 elif data[i*w+j]==-1:
00031 img[i,j]=205
00032
00033
00034 o=cv2.inRange(img,0,1)
00035 edges = cv2.Canny(img,0,255)
00036 im2, contours, hierarchy = cv2.findContours(o,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
00037 cv2.drawContours(o, contours, -1, (255,255,255), 5)
00038 o=cv2.bitwise_not(o)
00039 res = cv2.bitwise_and(o,edges)
00040
00041
00042 frontier=copy(res)
00043 im2, contours, hierarchy = cv2.findContours(frontier,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
00044 cv2.drawContours(frontier, contours, -1, (255,255,255), 2)
00045
00046 im2, contours, hierarchy = cv2.findContours(frontier,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
00047 all_pts=[]
00048 if len(contours)>0:
00049 upto=len(contours)-1
00050 i=0
00051 maxx=0
00052 maxind=0
00053
00054 for i in range(0,len(contours)):
00055 cnt = contours[i]
00056 M = cv2.moments(cnt)
00057 cx = int(M['m10']/M['m00'])
00058 cy = int(M['m01']/M['m00'])
00059 xr=cx*resolution+Xstartx
00060 yr=cy*resolution+Xstarty
00061 pt=[np.array([xr,yr])]
00062 if len(all_pts)>0:
00063 all_pts=np.vstack([all_pts,pt])
00064 else:
00065
00066 all_pts=pt
00067
00068 return all_pts
00069
00070