Repository navigation
Expand file tree
/
Copy pathCreateOccupancyGridFromFreeSpaceEstimate.m
More file actions
79 lines (60 loc) · 2.83 KB
/
Copy pathCreateOccupancyGridFromFreeSpaceEstimate.m
File metadata and controls
79 lines (60 loc) · 2.83 KB
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
function occupancyGrid = CreateOccupancyGridFromFreeSpaceEstimate(...
freeSpaceBEV,birdsEyeConfig,gridX,gridY,cellSize)
% Return an occupancy grid that contains the occupancy probability over
% a uniform 2-D grid.
% Number of cells in occupancy grid.
numCellsX = ceil(gridX / cellSize);
numCellsY = ceil(gridY / cellSize);
% Generate a set of (X,Y) points for each grid cell. These points are in
% the vehicle's coordinate system. Start by defining the edges of each grid
% cell.
% Define the edges of each grid cell in vehicle coordinates.
XEdges = linspace(0,gridX,numCellsX);
YEdges = linspace(-gridY/2,gridY/2,numCellsY);
% Next, specify the number of sample points to generate along each
% dimension within a grid cell. Use these to compute the step size in the
% X and Y direction. The step size will be used to shift the edge values of
% each grid to produce points that cover the entire area of a grid cell at
% the desired resolution.
% Sample 20 points from each grid cell. Sampling more points may produce
% smoother estimates at the cost of additional computation.
numSamplePoints = 20;
% Step size needed to sample number of desired points.
XStep = (XEdges(2)-XEdges(1)) / (numSamplePoints-1);
YStep = (YEdges(2)-YEdges(1)) / (numSamplePoints-1);
% Finally, slide the set of points across both dimensions of the grid
% cells. Sample the occupancy probability along the way using
% griddedInterpolant.
% Create griddedInterpolant for sampling occupancy probability. Use 1
% minus the free space confidence to represent the probability of occupancy.
occupancyProb = 1 - freeSpaceBEV;
sz = size(occupancyProb);
[y,x] = ndgrid(1:sz(1),1:sz(2));
F = griddedInterpolant(y,x,occupancyProb);
% Initialize the occupancy grid to zero.
occupancyGrid = zeros(numCellsY*numCellsX,1);
% Slide the set of points XEdges and YEdges across both dimensions of the
% grid cell.
for j = 1:numSamplePoints
% Increment sample points in the X-direction
X = XEdges + (j-1)*XStep;
for i = 1:numSamplePoints
% Increment sample points in the Y-direction
Y = YEdges + (i-1)*YStep;
% Generate a grid of sample points in bird's-eye-view vehicle coordinates
[XGrid,YGrid] = meshgrid(X,Y);
% Transform grid of sample points to image coordinates
xy = vehicleToImage(birdsEyeConfig,[XGrid(:) YGrid(:)]);
% Clip sample points to lie within image boundaries
xy = max(xy,1);
xq = min(xy(:,1),sz(2));
yq = min(xy(:,2),sz(1));
% Sample occupancy probabilities using griddedInterpolant and keep
% a running sum.
occupancyGrid = occupancyGrid + F(yq,xq);
end
end
% Determine mean occupancy probability.
occupancyGrid = occupancyGrid / numSamplePoints^2;
occupancyGrid = reshape(occupancyGrid,numCellsY,numCellsX);
end