getfrontier.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 #--------Include modules---------------
5 from copy import copy
6 import rospy
7 from nav_msgs.msg import OccupancyGrid
8 
9 import numpy as np
10 import cv2
11 
12 #-----------------------------------------------------
13 
14 def getfrontier(mapData):
15  data=mapData.data
16  w=mapData.info.width
17  h=mapData.info.height
18  resolution=mapData.info.resolution
19  Xstartx=mapData.info.origin.position.x
20  Xstarty=mapData.info.origin.position.y
21 
22  img = np.zeros((h, w, 1), np.uint8)
23 
24  for i in range(0,h):
25  for j in range(0,w):
26  if data[i*w+j]==100:
27  img[i,j]=0
28  elif data[i*w+j]==0:
29  img[i,j]=255
30  elif data[i*w+j]==-1:
31  img[i,j]=205
32 
33 
34  o=cv2.inRange(img,0,1)
35  edges = cv2.Canny(img,0,255)
36  im2, contours, hierarchy = cv2.findContours(o,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
37  cv2.drawContours(o, contours, -1, (255,255,255), 5)
38  o=cv2.bitwise_not(o)
39  res = cv2.bitwise_and(o,edges)
40  #------------------------------
41 
42  frontier=copy(res)
43  im2, contours, hierarchy = cv2.findContours(frontier,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
44  cv2.drawContours(frontier, contours, -1, (255,255,255), 2)
45 
46  im2, contours, hierarchy = cv2.findContours(frontier,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
47  all_pts=[]
48  if len(contours)>0:
49  upto=len(contours)-1
50  i=0
51  maxx=0
52  maxind=0
53 
54  for i in range(0,len(contours)):
55  cnt = contours[i]
56  M = cv2.moments(cnt)
57  cx = int(M['m10']/M['m00'])
58  cy = int(M['m01']/M['m00'])
59  xr=cx*resolution+Xstartx
60  yr=cy*resolution+Xstarty
61  pt=[np.array([xr,yr])]
62  if len(all_pts)>0:
63  all_pts=np.vstack([all_pts,pt])
64  else:
65 
66  all_pts=pt
67 
68  return all_pts
69 
70 
def getfrontier(mapData)
Definition: getfrontier.py:14


rrt_exploration
Author(s): Hassan Umari
autogenerated on Mon Jun 10 2019 14:57:44