Display shapes on image, video, or point cloud
showShape(___,
specifies options using one or more namevalue pair arguments. For example,
'Name,Value
)Color'
,'yellow
' specifies the color of the
displayed shapes as yellow.
Read an image into the workspace.
I = imread('visionteam1.jpg');
Create an aggregate channel features (ACF) people detector.
detector = peopleDetectorACF()
detector = acfObjectDetector with properties: ModelName: 'inria100x41' ObjectTrainingSize: [100 41] NumWeakLearners: 2048
Detect people in the image.
[bboxes,scores] = detect(detector,I);
Display the image with a labeled rectangle, including the associated detection score, around each detected person.
figure imshow(I) labels = "person:"+scores; showShape('rectangle',bboxes,'Label',labels)
Read point cloud data into the workspace.
ptCloud = pcread('teapot.ply');
Display the point cloud data.
figure pcshow(ptCloud) xlabel('X') ylabel('Y') zlabel('Z')
Define a cuboid and display it in green with an opacity of 0.5
.
pos = [0.3753 0 1.65 6 4 3 0 0 0]; showShape('cuboid',pos,'Color','green','Opacity',0.5)
Read point cloud data into the workspace.
ptCloud = pcread('teapot.ply');
Define a rotation matrix and 3D transform to rotate the point cloud, and an associated cuboid, by 5 degrees.
rot = 5; R = [ cosd(rot) sind(rot) 0 0; ... sind(rot) cosd(rot) 0 0; ... 0 0 1 0; ... 0 0 0 1]; tform = affine3d(R);
Compute the x and y rendered limits of the point cloud to ensure that the point cloud is not clipped during rotation.
pcLimits = abs([ptCloud.XLimits ptCloud.YLimits]); maxLimit = max(pcLimits);
Add an additional margin to the plot to prevent the cuboid from being clipped during rotation.
margin = 1; maxLimit = maxLimit + margin; xlimits = [maxLimit maxLimit]; ylimits = [maxLimit maxLimit]; zlimits = ptCloud.ZLimits;
Create a player for visualizing the point cloud.
player = pcplayer(xlimits,ylimits,zlimits);
Customize the player axis labels.
xlabel(player.Axes,'X (m)'); ylabel(player.Axes,'Y (m)'); zlabel(player.Axes,'Z (m)');
Define a cuboid around the point cloud.
cuboidPosition = [0.3753 0 1.65 6 4 3 0 0 0];
Define the output view for a cuboid rotation. Use the same limits as the player so that the cuboid is not clipped. Then, display the rotated point cloud and cuboid.
gridSize = [1 1 1]; ref = imref3d(gridSize,xlimits,ylimits,zlimits); for i = 1:round((360/rot)) % Rotate point cloud. ptCloud = pctransform(ptCloud,tform); % Rotate the cuboid. cuboidPosition = bboxwarp(cuboidPosition,tform,ref); % Show rotated point cloud data. view(player,ptCloud) % Show rotated cuboid. showShape('cuboid',cuboidPosition, ... 'Parent',player.Axes, ... 'Color','green', ... 'Opacity',0.5) % Use drawnow to synchronize point cloud and shape visualization. drawnow end
shape
— Type of shape'line'
 'rectangle'
 'cuboid'
 'polygon'
 'circle'
Type of shape, specified as a character vector. The vector can be
'line'
, 'rectangle'
, 'cuboid
,
'polygon'
, or 'circle'
.
Data Types: string
position
— Positions and sizes of shapesPositions and sizes of shapes, specified according to the type of shapes, described in this table.
Bounding Box  Description 

Axisaligned rectangle 
Defined in pixel coordinates as an Mby4 numeric matrix with rows of the form [x y w h], where:

Rotated rectangle 
Defined in spatial coordinates as an Mby5 numeric matrix with rows of the form [xctr yctr xlen ylen yaw], where:

Cuboid 
Defined in spatial coordinates as an Mby9 numeric matrix with rows of the form [xctr yctr zctr xlen ylen zlen xrot yrot zrot], where:
The figure shows how these values determine the position of a cuboid. 
Circle  Defined in spatial coordinates as an Mby3 numeric matrix with rows of the form [xctr yctr radius], where:

 Defined in spatial coordinates as a Pby2 matrix in which each row is an [x y] endpoint or a 1by2P vector of consecutive endpoints of the from [x_{1} y_{1} x_{2} y_{2} … x_{P} x_{P}].
To specify several lines that contain different numbers of endpoints, you can use the cell array format described for a polygon. 
Polygon  Defined in spatial coordinates as an Mby1 cell array in which each cell contains an Lby2 matrix of [x y] vertex locations or a 1by2L vector of consecutive vertex locations of the form [x_{1}, y_{1}, x_{2},y_{2}, … x_{L},y_{L} ], where:

Specify optional
commaseparated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
Color'
,'yellow
' specifies the color of
the displayed shapes as yellow.'Label'
— Shape label[]
(default)  scalar  Melement vector  Melement cell array of character vectorsShape label, specified as the commaseparated pair consisting of
'Label'
and either a scalar, Melement
vector, or an Melement cell array of character vectors.
If the input is a scalar value, then the function applies that same value as a label to each shape. If the input is an Melement vector or cell array, then the function applies each value to the corresponding shape, using the order in which the shapes are specified to the function. M is the number of shapes specified to the function.
'Color'
— Shape colorlines(1)
(default)  one or more RGB triplets  one or more color names or short color namesShape color, specified as the commaseparated pair consisting of
'Color'
and one or more RGB triplets, or one or more color names
or short color names. To use the same color for all shapes, specify a single (MATLAB
ColorSpec
) color name or a single RGB triplet.
To use a different color for each shape, specify an Melement vector or cell array of color names or an Mby3 numeric matrix in which each row is an RGB triplet. M is the number of shapes specified to the function.
'Opacity'
— Opacity of shape fill0
(default)  Melement vector of values in the range [0 1]Opacity of the shape fill, specified as the commaseparated pair consisting of
'Opacity'
and an Melement vector of numeric
values in the range [0 1]. To use the same opacity for all shapes, specify a scalar
opacity value. For a fully opaque shape fill, set 'Opacity'
to
1
.
'Parent'
— Output axesgca
(default)  Axes
graphics objectOutput axes, specified as the commaseparated pair consisting of
'Parent'
and an Axes Properties graphics object.
'LineColor'
— Border line color'auto'
(default)  one or more RGB triplets  one or more color names or short color namesBorder line color, specified as the commaseparated pair consisting of
'Color'
and one or more RGB triplets, or one or more color names
or short color names. To use the same color for all border lines, specify a single
(MATLAB ColorSpec
) color name or a single RGB triplet.
To use a different color for each shape, specify an Melement vector or cell array of color names or an Mby3 numeric matrix in which each row is an RGB triplet. M is the number of shapes specified to the function.
'LineWidth'
— Border line width'auto'
(default)  positive scalar integer  Melement vector of positive valuesBorder line width in pixels, specified as the commaseparated pair consisting of
'LineWidth'
and a positive scalar integer or an
Melement vector of positive scalar integers. To use the same
line width for all shapes, specify a positive scalar integer. Otherwise, specify an
Melement vector of positive integers, where M
is the number of shapes specified to the function.
'LineOpacity'
— Border line opacity of shape fill0
(default)  Melement vector of values in the range [0 1]Border line opacity of the shape fill, specified as the commaseparated pair
consisting of 'Opacity'
and an Melement vector
of numeric values in the range [0 1]. To use the same opacity for the border line of
all shapes, specify a scalar opacity value. For a fully opaque border line, set
'Opacity'
to 1
.
'LabelTextColor'
— Label text color'black
(default)  one or more RGB triplets  one or more color names or short color namesLabel text color, specified as the commaseparated pair consisting of
'Color'
and one or more RGB triplets, or one or more color names
or short color names. To use the same color for the text of all labels, specify a
single (MATLAB ColorSpec
) color name or a single RGB
triplet.
To use a different color for text of each label, specify an Melement vector or cell array of color names or an Mby3 numeric matrix in which each row is an RGB triplet. M is the number of labels specified to the function.
'LabelOpacity'
— Label opacity0
(default)  Melement vector of values in the range [0 1]Label opacity, specified as the commaseparated pair consisting of
'Opacity'
and an Melement vector of numeric
values in the range [0 1]. To use the same opacity for all labels, specify a scalar
opacity value. For a fully opaque label, set 'Opacity'
to
1
.
'LabelFont'
— Label font'Helvetica'
(default)  'FixedWidth'
 supported fontLabel font, specified as the commaseparated pair consisting of
'LabelFont'
and 'FixedWidth'
or a font that
your system supports. To display and print text properly, you must select a font that
your system supports. If you select an unsupported font, the function returns an
error. If you specify 'FixedWidth'
, the function uses the font
specified by the FixedWidthFont
property of the Root Properties object. The fixedwidth font relies on the value of
get(0,'FixedWidthFontName')
.
'LabelFontSize'
— Label font size12
(default)  positive scalarLabel font size in point units, specified as the commaseparated pair consisting
of 'LabelFontSize'
and a positive scalar.
imshow
 insertMarker
 insertObjectAnnotation
 insertObjectMask
 insertShape
 insertText
 pcplayer
 pcshow
You have a modified version of this example. Do you want to open this example with your edits?
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.