Draw Shapes
Draw rectangles, lines, polygons, or circles on images
 Library:
Computer Vision Toolbox / Text & Graphics
Description
The Draw Shapes block draws rectangles, lines, polygons, or circles on images by overwriting pixel values. As a result, the shapes are embedded in the output image. The block uses Bresenham's line drawing algorithm to draw lines, polygons, and rectangles and Bresenham's circle drawing algorithm to draw circles. The output signal is the same size and data type as the inputs to the Image, R, G, and B ports.
You can set the shape fill or border color via the input port or by manually entering the values in the Block Parameters window. Use the color parameters to determine the appearance of the rectangles, lines, polygons, or circles.
Ports
Input
Image
— Input image
MbyN matrix of intensity values  MbyNbyP color
values
Input image, specified as either an MbyN matrix of intensity values or an MbyNbyP matrix of color values with P color planes.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
R
— Red video stream
scalar  vector  matrix
Red video stream, specified as a scalar, vector, or a matrix. The input represents one plane of the RGB video stream. The R, G, and B ports must have the same dimensions and data type.
Dependencies
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
G
— Green video stream
scalar  vector  matrix
Green video stream, specified as a scalar, vector, or a matrix. The input represents one plane of the RGB video stream. The R, G, and B ports must have the same dimensions and data type.
Dependencies
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
B
— Blue video stream
scalar  vector  matrix
Blue video stream, specified as a scalar, vector, or a matrix. The input represents one plane of the RGB video stream. The R, G, and B ports must have the same dimensions and data type.
Dependencies
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
Pts
— Shape coordinates
Mby3 matrix  Mby4 matrix  Mby2L
Shape coordinates. The format of the points depends on the type of shape you specify with the Shape parameter.
Rectangles
— Mby4 matrix of M rectangles. The format for each rectangle must be, [x y width height].Shape Input to the Pts Port Drawn Shape Single Rectangle Fourelement row vector in the format
[x y width height]
, where:x
andy
are the onebased coordinates of the upperleft corner of the rectangle.width
andheight
are the width and height in pixels, of the rectangle. The values ofwidth
andheight
must be greater than 0.
M Rectangles Mby4 matrix in the format
,where each row of the matrix corresponds to a different rectangle and is of the same form as the vector for a single rectangle.$$\left[\begin{array}{cccc}{x}_{1}& {y}_{1}& widt{h}_{1}& heigh{t}_{1}\\ {x}_{2}& {y}_{2}& widt{h}_{2}& heigh{t}_{2}\\ \vdots & \vdots & \vdots & \vdots \\ {x}_{M}& {y}_{M}& widt{h}_{M}& heigh{t}_{M}\end{array}\right]$$
Lines
— Mby2L matrix of M polylines. The format for each polyline must be of the form [x1,y1,x2,y2,…,xL,yL].Shape Input to the Pts Port Drawn Shape Single Line Fourelement row vector in the format [x_{1} y_{1} x_{2} y_{2}]
, where:x_{1}
andy_{1}
are the coordinates of the beginning of the line.x_{2}
andy_{2}
are the coordinates of the end of the line.
M Lines Mby4 matrix in the format
,where each row of the matrix corresponds to a different line and is of the same form as the vector for a single line.$$\left[\begin{array}{cccc}{x}_{11}& {y}_{11}& {x}_{12}& {y}_{12}\\ {x}_{21}& {y}_{21}& {x}_{22}& {y}_{22}\\ \vdots & \vdots & \vdots & \vdots \\ {x}_{M1}& {y}_{M1}& {x}_{M2}& {y}_{M2}\end{array}\right]$$
Single Polyline with (L1) Segments Vector of size 2L, where L is the number of vertices, in the format, [x_{1}, y_{1}, x_{2}, y_{2}, ..., x_{L}, y_{L}
, whereThe polyline always contains (L1) number of segments because the first and last vertex points do not connect. The block produces an error message when the number of rows is less than two or not a multiple of two.x_{1} and y_{1} are the coordinates of the beginning of the first line segment.
x_{2} and y_{2} are the coordinates of the end of the first line segment and the beginning of the second line segment.
x_{L} and y_{L} are the coordinates of the end of the (L1)^{th} line segment.
M Polylines with (L1) Segments Mby2L matrix in the format
$$\left[\begin{array}{ccccccc}{x}_{11}& {y}_{11}& {x}_{12}& {y}_{12}& \cdots & {x}_{1L}& {y}_{1L}\\ {x}_{21}& {y}_{21}& {x}_{22}& {y}_{22}& \cdots & {x}_{2L}& {y}_{2L}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ {x}_{M1}& {y}_{M1}& {x}_{M2}& {y}_{M2}& \cdots & {x}_{ML}& {y}_{ML}\end{array}\right]$$
, where each row of the matrix corresponds to a different polyline and is of the same form as the vector for a single polyline. When you require one polyline to contain less than (L–1) number of segments, fill the matrix by repeating the coordinates of the last vertex.
The block produces an error message if the number of rows is less than two or not a multiple of two.
Polygons
— Mby2L matrix of M polygons. The format for each polygon must be of the form [x1,y1,x2,y2,…,xL,yL]. The points are specified in consecutive order to form a polygon.Shape Input to the Pts Port Drawn Shape Single Polygon with L line segments Row vector of size 2L, where L is the number of vertices, in the format [x_{1} y_{1} x_{2} y_{2} ... x_{L} y_{L}]
, where:The block connectsx_{1} and y_{1} are the coordinates of the beginning of the first line segment.
x_{2} and y_{2} are the coordinates of the end of the first line segment and the beginning of the second line segment.
x_{L} and y_{L} are the coordinates of the end of the (L1)^{th} line segment and the beginning of the L^{th} line segment.
[x_{1} y_{1}]
to[x_{L} y_{L}]
to complete the polygon. The block produces an error if the number of rows is negative or not a multiple of two.M Polygons with the largest number of line segments in any line being L Mby2L matrix in the format
$$\left[\begin{array}{ccccccc}{x}_{11}& {y}_{11}& {x}_{12}& {y}_{12}& \cdots & {x}_{1L}& {y}_{1L}\\ {x}_{21}& {y}_{21}& {x}_{22}& {y}_{22}& \cdots & {x}_{2L}& {y}_{2L}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ {x}_{M1}& {y}_{M1}& {x}_{M2}& {y}_{M2}& \cdots & {x}_{ML}& {y}_{ML}\end{array}\right]$$
, where each row of the matrix corresponds to a different polygon and is of the same form as the vector for a single polygon. If some polygons are shorter than others, repeat the ending coordinates to fill the polygon matrix.
The block produces an error message if the number of rows is less than two or is not a multiple of two.
Circles
— Mby3 matrix of M circles. The format for each circle must be, [x y radius].Shape Input to the Pts Port Drawn Shape Single Circle Threeelement row vector in the format
[x y radius]
, where:x
andy
are coordinates for the center of the circle.radius
is the radius of the circle, which must be greater than 0.
M Circles Mby3 matrix in the format
, where each row of the matrix corresponds to a different circle and is of the same form as the vector for a single circle.$$\left[\begin{array}{ccc}{x}_{1}& {y}_{1}& radiu{s}_{1}\\ {x}_{2}& {y}_{2}& radiu{s}_{2}\\ \vdots & \vdots & \vdots \\ {x}_{M}& {y}_{M}& radiu{s}_{M}\end{array}\right]$$
Dependencies
Doubleprecision floating point is only supported if the input to the Image or R, G, and B ports is floating point.
Singleprecision floating point (only supported if the input to the Image or R, G, and B ports is floating point).
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
ROI
— Region of interest
fourelement vector
Region of interest, specified as a fourelement vector [x y width height]. The region defines a rectangular area to draw shapes. The first two elements represent the onebased coordinates of the upperleft corner of the area. The second two elements represent the width and height of the area.
Dependencies
To enable this port, set the ROI port appears when you set
the Draw shapes in parameter to Specify region of
interest via port
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
Clr
— Color of shape
Pelement vector  MbyP matrix
Color of shape, specified as either a Pelement vector or an MbyP matrix of M number of shapes and P number of color planes. You can specify a color (RGB) for each shape, or specify one color for all shapes.
Dependencies
To enable this port, set the Border color source parameter to Input port.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
Output
Port_1
— Output image
scalar  vector  matrix
Output image, returned as a scalar, vector, or matrix.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
 enumerated
 bus
Parameters
Main Tab
Fill Shapes
— Fill color
off (default)  on
Select Fill Shapes to specify an intensity value or a color.
Note
If you are generating code and you select the Fill shapes check box, the word length of the block input(s) cannot exceed 16 bits.
Fill color source
— Source for fill color
Specify via dialog
(default)  Input port
Specify the source for the fill color value to either Specify via
dialog
or Input port
.
Dependencies
To enable this parameter, select Fill shapes.
Fill color
— Shape fill color
Black
(default)  White
 Userspecified value
Specify the shape fill color.
Dependencies
To enable this parameter, select the Fill shapes parameter.
Border color source
— Border color source
(default) 
Specify the source for the border color value to either Specify via
dialog
or Input port
.
Dependencies
To enable this parameter, select the Fill shapes parameter.
Border color
— Border color
Black
(default)  White
 Userspecified value
Specify the appearance of the shape's border. You can specify
Black
, White
, or
Userspecified value
. This table describes what to enter
for the color value based on the block input and the number of shapes you are
drawing.
Block Input  Color Value(s) for Drawing One Shape or Multiple Shapes with the Same Color  Color Value(s) for Drawing Multiple Shapes with Unique Colors 

Intensity image  Scalar intensity value  Relement vector where R is the number of shapes 
Color image  Pelement vector where P is the number of color planes  RbyP matrix where P is the number of color planes and R is the number of shapes 
For each value in the Color Value(s) parameter, enter a number between the minimum and maximum values that can be represented by the data type of the input image. If you enter a value outside this range, the block produces an error message.
Dependencies
To enable this parameter, clear the Fill shapes parameter.
Color value(s)
— Color value
color value  intensity value
Specify an intensity or color value for the shape's border or fill.
Tunable: Yes
Dependencies
To enable this parameter set the Border color or
Fill color parameter to Userspecified
value
.
Opacity factor (between 0 and 1)
— Opacity factor
value in the range [0,1]  Relement vector
Specify the opacity of the shading inside the shape, where 0
is
transparent and 1
is opaque.
Opacity Factor value for Drawing One Shape or Multiple Shapes with the Same Color  Opacity Factor value for Drawing Multiple Shapes with Unique Colors 

Scalar intensity value  Relement vector where R is the number of shapes 
Dependencies
To enable this parameter, clear the Fill shapes parameter.
Use antialiasing
— Smoothing algorithm
off  on
Perform a smoothing algorithm on the line, polygon, or circle.
Dependencies
To enable this parameter, set the Shape parameter to either
Lines
,Polygons
, orCircles
.
Image Signal
— Image signal
Specify how to input and output a color video signal. Select the color video signal as one of the following:
One multidimensional signal
— The block accepts an MbyNbyP color video signal, where P is the number of color planes, at one port.Separate color signals
— Enables the R,G, and B input ports. Each port accepts one MbyN plane of an RGB video stream.
Data Types Tab
For details on the fixedpoint block parameters, see Specify FixedPoint Attributes for Blocks.
Model Examples
Block Characteristics
Data Types 

Multidimensional Signals 

VariableSize Signals 

Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
FixedPoint Conversion
Design and simulate fixedpoint systems using FixedPoint Designer™.
Version History
See Also
MATLAB Command
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.
Select a Web Site
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: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)