www.gusucode.com > robotics 案例源码程序 matlab代码 > robotics/ConvertPGMImageToOccMapExample.m

    %% Convert PGM Image to Map
% Convert a portable graymap (|.pgm|) file containing a ROS map
% into an |OccupancyGrid| map for use in MATLAB.

%%
% Import the image using |imread|. Crop the image to the relevant area.
image = imread(fullfile(matlabroot,'examples','robotics','playpen_map.pgm'));
imageCropped = image(750:1250,750:1250);
imshow(imageCropped)


%%
% PGM values are expressed from 0 to 255 as |uint8|. Normalize these values by  
% converting the cropped image to |double| and dividing each cell by 255. 
% This image shows obstacles as values close to 0. 
% Subtract the normalized image from 1 to get occupancy values 
% with 1 representing occupied space.
imageNorm = double(imageCropped)/255;
imageOccupancy = 1 - imageNorm;

%%
% Create the |OccupancyGrid| object using an adjusted map image. The imported map
% resolution is 20 cells per meter.
map = robotics.OccupancyGrid(imageOccupancy,20);
show(map)