Draw rectangles, lines, polygons, or circles on images
Computer Vision Toolbox / Text & Graphics
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.
Image
— Input imageInput 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
 uint8
 uint16
 uint32
 Boolean
 fixed point
R
— Red video streamRed 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.
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
G
— Green video streamGreen 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.
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
B
— Blue ideo streamBlue 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.
To enable this port, set the Image signal parameter to
Separate color signals
.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Pts
— Shape coordinatesShape 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:


M Rectangles  Mby4 matrix in the format $$\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:


M Lines  Mby4 matrix in the format $$\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} , where


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:
[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:


M Circles  Mby3 matrix in the format $$\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]$$ 

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 interestRegion 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.
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
 uint8
 uint16
 uint32
 Boolean
 fixed point
Clr
— Color of shapeColor 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.
To enable this port, set the Border color source parameter to Input port.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Port_1
— Output imageOutput 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
Main Tab
Fill Shapes
— Fill colorSelect Fill Shapes to specify an intensity value or a color.
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 colorSpecify via dialog
(default)  Input port
Specify the source for the fill color value to either Specify via
dialog
or Input port
.
To enable this parameter, select Fill shapes.
Fill color
— Shape fill colorBlack
(default)  White
 Userspecified value
Specify the shape fill color.
To enable this parameter, select the Fill shapes parameter.
Border color source
— Border color sourceSpecify the source for the border color value to either Specify via
dialog
or Input port
.
To enable this parameter, select the Fill shapes parameter.
Border color
— Border colorBlack
(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.
To enable this parameter, clear the Fill shapes parameter.
Color value(s)
— Color valueSpecify an intensity or color value for the shape's border or fill.
Tunable: Yes
To enable this parameter set the Border color or
Fill color parameter to Userspecified
value
.
Opacity factor (between 0 and 1)
— Opacity factorSpecify 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 
To enable this parameter, clear the Fill shapes parameter.
Use antialiasing
— Smoothing algorithmPerform a smoothing algorithm on the line, polygon, or circle.
To enable this parameter, set the Shape parameter to
either Lines
, Polygons
, or
Circles
.
Image Signal
— Image signalSpecify 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.
Data Types 

Multidimensional Signals 

VariableSize Signals 

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.