-
Notifications
You must be signed in to change notification settings - Fork 9
/
grid_map.py
204 lines (143 loc) · 3.97 KB
/
grid_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
import numpy as np
import sys
import cv2
SCRIPTS_PATH = '/home/maestro/catkin_ws/src/grid_mapping/scripts'
sys.path.insert(0, SCRIPTS_PATH)
from bresenham import *
TRESHOLD_P_FREE = 0.3
TRESHOLD_P_OCC = 0.6
def log_odds(p):
"""
Log odds ratio of p(x):
p(x)
l(x) = log ----------
1 - p(x)
"""
return np.log(p / (1 - p))
def retrieve_p(l):
"""
Retrieve p(x) from log odds ratio:
1
p(x) = 1 - ---------------
1 + exp(l(x))
"""
return 1 - 1 / (1 + np.exp(l))
class GridMap:
"""
Grid map
"""
def __init__(self, X_lim, Y_lim, resolution, p):
self.X_lim = X_lim
self.Y_lim = Y_lim
self.resolution = resolution
x = np.arange(start = X_lim[0], stop = X_lim[1] + resolution, step = resolution)
y = np.arange(start = Y_lim[0], stop = Y_lim[1] + resolution, step = resolution)
# probability matrix in log-odds scale:
self.l = np.full(shape = (len(x), len(y)), fill_value = log_odds(p))
def get_shape(self):
"""
Get dimensions
"""
return np.shape(self.l)
def calc_MLE(self):
"""
Calculate Maximum Likelihood estimate of the map
"""
for x in range(self.l.shape[0]):
for y in range(self.l.shape[1]):
# cell is free
if self.l[x][y] < log_odds(TRESHOLD_P_FREE):
self.l[x][y] = log_odds(0.01)
# cell is occupied
elif self.l[x][y] > log_odds(TRESHOLD_P_OCC):
self.l[x][y] = log_odds(0.99)
# cell state uncertain
else:
self.l[x][y] = log_odds(0.5)
def to_BGR_image(self):
"""
Transformation to BGR image format
"""
# grayscale image
gray_image = 1 - retrieve_p(self.l)
# repeat values of grayscale image among 3 axis to get BGR image
rgb_image = np.repeat(a = gray_image[:,:,np.newaxis],
repeats = 3,
axis = 2)
return rgb_image
def to_grayscale_image(self):
"""
Transformation to GRAYSCALE image format
"""
return 1 - retrieve_p(self.l)
def discretize(self, x_cont, y_cont):
"""
Discretize continious x and y
"""
x = int((x_cont - self.X_lim[0]) / self.resolution)
y = int((y_cont - self.Y_lim[0]) / self.resolution)
return (x,y)
def update(self, x, y, p):
"""
Update x and y coordinates in discretized grid map
"""
# update probability matrix using inverse sensor model
self.l[x][y] += log_odds(p)
def check_pixel(self, x, y):
"""
Check if pixel (x,y) is within the map bounds
"""
if x >= 0 and x < self.get_shape()[0] and y >= 0 and y < self.get_shape()[1]:
return True
else:
return False
def find_neighbours(self, x, y):
"""
Find neighbouring pixels to pixel (x,y)
"""
X_neighbours = []
Y_neighbours = []
if self.check_pixel(x + 1, y):
X_neighbours.append(x + 1)
Y_neighbours.append(y)
if self.check_pixel(x + 1, y + 1):
X_neighbours.append(x + 1)
Y_neighbours.append(y + 1)
if self.check_pixel(x + 1, y - 1):
X_neighbours.append(x + 1)
Y_neighbours.append(y - 1)
if self.check_pixel(x, y + 1):
X_neighbours.append(x)
Y_neighbours.append(y + 1)
if self.check_pixel(x, y - 1):
X_neighbours.append(x)
Y_neighbours.append(y - 1)
if self.check_pixel(x - 1, y):
X_neighbours.append(x - 1)
Y_neighbours.append(y)
if self.check_pixel(x - 1, y + 1):
X_neighbours.append(x - 1)
Y_neighbours.append(y + 1)
if self.check_pixel(x - 1, y - 1):
X_neighbours.append(x - 1)
Y_neighbours.append(y - 1)
return zip(X_neighbours, Y_neighbours)
def set_pixel_color(bgr_image, x, y, color):
"""
Set 'color' to the given pixel (x,y) on 'bgr_image'
"""
if x < 0 or y < 0 or x >= bgr_image.shape[0] or y >= bgr_image.shape[1]:
return
if color == 'BLUE':
bgr_image[x, y, 0] = 1.0
bgr_image[x, y, 1] = 0.0
bgr_image[x, y, 2] = 0.0
elif color == 'GREEN':
bgr_image[x, y, 0] = 0.0
bgr_image[x, y, 1] = 1.0
bgr_image[x, y, 2] = 0.0
elif color == 'RED':
bgr_image[x, y, 0] = 0.0
bgr_image[x, y, 1] = 0.0
bgr_image[x, y, 2] = 1.0