This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

robotics.BinaryOccupancyGrid class

Package: robotics

Create occupancy grid with binary values

Description

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 as true (1) and a free location is represented as false (0).

The two coordinate systems supported are world and grid coordinates. The world coordinates origin is defined by GridLocationInWorld, which defines the bottom-left corner of the map. The number and size of grid locations are defined by the Resolution. Also, the first grid location with index (1,1) begins in the top-left corner of the grid.

Construction

example

map = robotics.BinaryOccupancyGrid(width,height) creates a 2-D binary occupancy grid representing a work space of width and height in meters. The default grid resolution is one cell per meter.

map = robotics.BinaryOccupancyGrid(width,height,resolution) creates a grid with resolution specified in cells per meter. The map is in world coordinates by default. You can use any of the arguments from previous syntaxes.

map = robotics.BinaryOccupancyGrid(rows,cols,resolution,"grid") creates a 2-D binary occupancy grid of size (rows,cols).

example

map = robotics.BinaryOccupancyGrid(p) 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).

map = robotics.BinaryOccupancyGrid(p,resolution) creates a BinaryOccupancyGrid object with resolution specified in cells per meter.

Input Arguments

expand all

Map width, specified as a double in meters.

Data Types: double

Map height, specified as a double in meters.

Data Types: double

Grid resolution, specified as a double in cells per meter.

Data Types: double

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 location (0).

Properties

expand all

Number of rows and columns in grid, stored as a two-element horizontal vector of the form [rows cols]. This value is read only.

Grid resolution, stored as a scalar in cells per meter. This value is read only.

Data Types: double

Minimum and maximum values of x-coordinates, stored as a two-element horizontal vector of the form [min max]. These values indicate the world range of the x-coordinates in the grid. This value is read only.

Minimum and maximum values of y-coordinates, stored as a two-element vector of the form [min max]. These values indicate the world range of the y-coordinates in the grid. This value is read only.

[x,y] world coordinates of the bottom-left corner of the grid, specified as a two-element vector.

Data Types: double

Methods

copyCopy array of handle objects
getOccupancyGet occupancy value for one or more positions
grid2worldConvert grid indices to world coordinates
inflateInflate each occupied grid location
occupancyMatrixConvert occupancy grid to matrix
setOccupancySet occupancy value for one or more positions
showShow occupancy grid values
world2gridConvert world coordinates to grid indices

Examples

collapse all

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)

Create BinaryOccupancyGrid object using adjusted map image.

map = robotics.BinaryOccupancyGrid(imageBW);
show(map)

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2015a