Create occupancy grid with binary values
BinaryOccupancyGrid creates a 2-D occupancy
grid object, which you can use to represent and visualize a robot
workspace, including obstacles. The integration of sensor data and
position estimates create a spatial representation of the approximate
locations of the obstacles.
Occupancy grids are used in robotics algorithms such as path planning. They are also used in mapping applications, such as for finding collision-free paths, performing collision avoidance, and calculating localization. You can modify your occupancy grid to fit your specific application.
Each cell in the occupancy grid has a value representing the
occupancy status of that cell. An occupied location is represented
true (1) and a free location is represented
The two coordinate systems supported are world and grid coordinates.
The world coordinates origin is defined by
which defines the bottom-left corner of the map. The number and size
of grid locations are defined by the
Also, the first grid location with index
in the top-left corner of the grid.
map = robotics.BinaryOccupancyGrid(rows,cols,
creates a 2-D binary occupancy grid of size
map = robotics.BinaryOccupancyGrid( creates
a grid from the values in matrix
p. The size
of the grid matches the size of the matrix, with each cell value interpreted
from its location in the matrix.
p contains any
numeric or logical type with zeros (0) and ones (1).
width— Map width
Map width, specified as a double in meters.
height— Map height
Map height, specified as a double in meters.
resolution— Grid resolution
Grid resolution, specified as a double in cells per meter.
p— Input occupancy grid
Input occupancy grid, specified as a matrix of ones and zeros.
The size of the grid matches the size of the matrix. Each matrix element
corresponds to an occupied location (
1) or free
GridSize— Number of rows and columns in grid
Number of rows and columns in grid, stored as a two-element
horizontal vector of the form
[rows cols]. This
value is read only.
Resolution— Grid resolution
Grid resolution, stored as a scalar in cells per meter. This value is read only.
XWorldLimits— Minimum and maximum values of x-coordinates
Minimum and maximum values of x-coordinates,
stored as a two-element horizontal vector of the form
max]. These values indicate the world range of the x-coordinates
in the grid. This value is read only.
YWorldLimits— Minimum and maximum values of y-coordinates
Minimum and maximum values of y-coordinates,
stored as a two-element vector of the form
These values indicate the world range of the y-coordinates
in the grid. This value is read only.
[x,y]world coordinates of grid
[x,y] world coordinates of the bottom-left
corner of the grid, specified as a two-element vector.
|copy||Copy array of handle objects|
|getOccupancy||Get occupancy value for one or more positions|
|grid2world||Convert grid indices to world coordinates|
|inflate||Inflate each occupied grid location|
|occupancyMatrix||Convert occupancy grid to matrix|
|setOccupancy||Set occupancy value for one or more positions|
|show||Show occupancy grid values|
|world2grid||Convert world coordinates to grid indices|
Create a 10m x 10m empty map.
map = robotics.BinaryOccupancyGrid(10,10,10);
Set occupancy of world locations and show map.
map = robotics.BinaryOccupancyGrid(10,10,10); x = [1.2; 2.3; 3.4; 4.5; 5.6]; y = [5.0; 4.0; 3.0; 2.0; 1.0]; setOccupancy(map, [x y], ones(5,1)) figure show(map)
Inflate occupied locations by a given radius.
inflate(map, 0.5) figure show(map)
Get grid locations from world locations.
ij = world2grid(map, [x y]);
Set grid locations to free locations.
setOccupancy(map, ij, zeros(5,1), 'grid') figure show(map)
This example shows how to convert an image to a binary occupancy grid for using with the Robotics System Toolbox®
% Import Image filepath = fullfile(matlabroot,'examples','robotics','imageMap.png'); image = imread(filepath); % Convert to grayscale and then black and white image based on arbitrary % threshold. grayimage = rgb2gray(image); bwimage = grayimage < 0.5; % Use black and white image as matrix input for binary occupancy grid grid = robotics.BinaryOccupancyGrid(bwimage); show(grid)
This example shows how to convert a
.pgm file which contains a ROS map into a
BinaryOccupancyGrid map for use in MATLAB.
Import image using
imread. The image is quite large and should be cropped to the relevant area.
image = imread('playpen_map.pgm'); imageCropped = image(750:1250,750:1250); imshow(imageCropped)
Unknown areas (gray) should be removed and treated as free space. Create a logical matrix based on a threshold. Depending on your image, this value could be different. Occupied space should be set as 1 (white in image).
imageBW = imageCropped < 100; imshow(imageBW)
BinaryOccupancyGrid object using adjusted map image.
map = robotics.BinaryOccupancyGrid(imageBW); show(map)