Accelerating the pace of engineering and science

# Draw Shapes

Draw rectangles, lines, polygons, or circles on images

Text & Graphics

visiontextngfix

## Description

The Draw Shapes block draws multiple rectangles, lines, polygons, or circles on images by overwriting pixel values. As a result, the shapes are embedded on the output image.

This block uses Bresenham's line drawing algorithm to draw lines, polygons, and rectangles. It uses 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 via the input dialog. Use the color input or color parameter to determine the appearance of the rectangle(s), line(s), polygon(s), or circle(s).

### Port Description

PortInput/OutputSupported Data TypesComplex Values Supported

Image

M-by-N matrix of intensity values or an M-by-N-by-P color values where P is the number of color planes

• Double-precision floating point

• Single-precision floating point

• Fixed point

• Boolean

• 8-, 16-, and 32-bit signed integer

• 8-, 16-, and 32-bit unsigned integer

No

R, G, B

Scalar, vector, or matrix that is one plane of the input RGB video stream. Inputs to the R, G, and B ports must have the same dimensions and data type.

Same as Image port

No

Pts

Use integer values to define one-based shape coordinates. If you enter noninteger values, the block rounds them to the nearest integer.

• Double-precision floating point (only supported if the input to the I or R, G, and B ports is floating point)

• Single-precision floating point (only supported if the input to the I or R, G, and B ports is floating point)

• 8-, 16-, and 32-bit signed integer

• 8-, 16-, and 32-bit unsigned integer

No

ROI

4-element vector of integers [x y width height], that define a rectangular area in which to draw the shapes. The first two elements represent the one-based coordinates of the upper-left corner of the area. The second two elements represent the width and height of the area.

• Double-precision floating point

• Single-precision floating point

• 8-, 16-, and 32-bit signed integer

• 8-, 16-, and 32-bit unsigned integer

No

Clr

This port can be used to dynamically specify shape color.

P-element vector or an M-by-P matrix, where M is the number of shapes, and P, the number of color planes.

You can specify a color (RGB), for each shape, or specify one color for all shapes.

Same as Image port

No

Output

Scalar, vector, or matrix of pixel values that contain the shape(s)

Same as Image port

No

### Drawing Shapes and Lines

Use the Shape parameter and Pts port to draw the following shapes or lines:

#### Drawing Rectangles

The Draw Shapes block lets you draw one or more rectangles. Set the Shape parameter to Rectangles, and then follow the instructions in the table to specify the input to the Pts port to obtain the desired number of rectangles.

ShapeInput to the Pts PortDrawn Shape
Single RectangleFour-element row vector
[x y width height] where
• x and y are the one-based coordinates of the upper-left corner of the rectangle.

• width and height are the width, in pixels, and height, in pixels, of the rectangle. The values of width and height must be greater than 0.

M Rectangles

M-by-4 matrix

$\left[\begin{array}{cccc}{x}_{1}& {y}_{1}& widt{h}_{1}& heigh{t}_{1}\\ {x}_{2}& {y}_{2}& widt{h}_{2}& heigh{t}_{2}\\ ⋮& ⋮& ⋮& ⋮\\ {x}_{M}& {y}_{M}& widt{h}_{M}& heigh{t}_{M}\end{array}\right]$

where each row of the matrix corresponds to a different rectangle and is of the same form as the vector for a single rectangle.

For an example of how to use the Draw Shapes block to draw a rectangle, see Track an Object Using Correlation.

#### Drawing Lines and Polylines

The Draw Shapes block lets you draw either a single line, or one or more polylines. You can draw a polyline with a series of connected line segments. Set the Shape parameter to Lines, and then follow the instructions in the table to specify the input to the Pts port to obtain the desired shape.

ShapeInput to the Pts PortDrawn Shape
Single LineFour-element row vector [x1 y1 x2 y2] where
• x1 and y1 are the coordinates of the beginning of the line.

• x2 and y2 are the coordinates of the end of the line.

M Lines

M-by-4 matrix

$\left[\begin{array}{cccc}{x}_{11}& {y}_{11}& {x}_{12}& {y}_{12}\\ {x}_{21}& {y}_{21}& {x}_{22}& {y}_{22}\\ ⋮& ⋮& ⋮& ⋮\\ {x}_{M1}& {y}_{M1}& {x}_{M2}& {y}_{M2}\end{array}\right]$

where each row of the matrix corresponds to a different line and is of the same form as the vector for a single line.

Single Polyline with (L-1) SegmentsVector of size 2L, where L is the number of vertices, with format, [x1, y1, x2, y2, ..., xL, yL].
• x1 and y1 are the coordinates of the beginning of the first line segment.

• x2 and y2 are the coordinates of the end of the first line segment and the beginning of the second line segment.

• xL and yL are the coordinates of the end of the (L-1)th line segment.

The polyline always contains (L-1) 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.

M Polylines with (L-1) Segments

M-by-2L matrix

$\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}\\ ⋮& ⋮& ⋮& ⋮& \ddots & ⋮& ⋮\\ {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.

If you select the Use antialiasing check box, the block applies an edge smoothing algorithm.

For examples of how to use the Draw Shapes block to draw a line, see Detect Lines in Images and Measure Angle Between Lines.

#### Drawing Polygons

The Draw Shapes block lets you draw one or more polygons. Set the Shape parameter to Polygons, and then follow the instructions in the table to specify the input to the Pts port to obtain the desired number of polygons.

ShapeInput to the Pts PortDrawn Shape
Single Polygon with L line segmentsRow vector of size 2L, where L is the number of vertices, with format, [x1 y1 x2 y2 ... xL yL] where
• x1 and y1 are the coordinates of the beginning of the first line segment.

• x2 and y2 are the coordinates of the end of the first line segment and the beginning of the second line segment.

• xL and yL are the coordinates of the end of the (L-1)th line segment and the beginning of the Lth line segment.

The block connects [x1 y1] to [xL yL] 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

M-by-2L matrix

$\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}\\ ⋮& ⋮& ⋮& ⋮& \ddots & ⋮& ⋮\\ {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.

#### Drawing Circles

The Draw Shapes block lets you draw one or more circles. Set the Shape parameter to Circles, and then follow the instructions in the table to specify the input to the Pts port to obtain the desired number of circles.

ShapeInput to the Pts PortDrawn Shape
Single CircleThree-element row vector
• x and y are coordinates for the center of the circle.

• radius is the radius of the circle, which must be greater than 0.

M Circles

M-by-3 matrix

$\left[\begin{array}{ccc}{x}_{1}& {y}_{1}& radiu{s}_{1}\\ {x}_{2}& {y}_{2}& radiu{s}_{2}\\ ⋮& ⋮& ⋮\\ {x}_{M}& {y}_{M}& radiu{s}_{M}\end{array}\right]$

where each row of the matrix corresponds to a different circle and is of the same form as the vector for a single circle.

## Dialog Box

Shape

Specify the type of shape(s) to draw. Your choices are Rectangles, Lines, Polygons, or Circles.

The block performs a smoothing algorithm when you select the Use antialiasing check box with either Lines, Polygons, or Circles. The block uses an algorithm similar to the poly2mask function to determine which subpixels to draw.

Fill shapes

Fill the shape with an intensity value or a color.

When you select this check box, the Fill color source, Fill color and Opacity factor (between 0 and 1) parameters appear in the dialog box.

 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.

When you do not select the Fill shapes check box, the Border color source, and Border color parameters are available.

Fill color source

Specify source for fill color value to either Specify via dialog or Input port. This parameter appears when you select the Fill shapes check box. When you select Input port, the color input port clr appears on the block.

Fill color

Specify the fill color for shape. You can specify either Black, White, or User-specified value. When you select User-specified value, the Color value(s) parameter appears in the dialog box. This parameter is visible if you select the Fill shapes check box.

Border color source

Specify source for the border color value to either Specify via dialog or Input port. Border color options are visible when the fill shapes options are not selected. This appears when you select the Fill shapes check box. When you select Input port, the color input port clr appears on the block.

Border color

Specify the appearance of the shape's border. You can specify either Black, White, or User-specified value. If you select User-specified value, the Color value(s) parameter appears in the dialog box. This parameter appears when you clear the Fill shapes check box.

Color value(s)

Specify an intensity or color value for the shape's border or fill. This parameter applies when you set the Border color or Fill color parameter to User-specified value. This parameter is tunable.

The following table describes what to enter for the color value based on the block input and the number of shapes you are drawing.

Block InputColor Value(s) for Drawing One Shape or Multiple Shapes with the Same ColorColor Value(s) for Drawing Multiple Shapes with Unique Color
Intensity imageScalar intensity valueR-element vector where R is the number of shapes
Color imageP-element vector where P is the number of color planesR-by-P 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.

Opacity factor (between 0 and 1)

Specify the opacity of the shading inside the shape, where 0 is transparent and 1 is opaque. This parameter is visible if you select the Fill shapes check box.

The following table describes what to enter for this parameter based on the block input and the number of shapes you are drawing. This parameter applies when you select the Filled check box.

Opacity Factor value for Drawing One Shape or Multiple Shapes with the Same ColorOopacity Factor value for Drawing Multiple Shapes with Unique Color
Scalar intensity valueR-element vector where R is the number of shapes

Draw shapes in

Specify the type of area in which to draw shapes. You can define one of the following:

• Entire image, enables you to draw shapes in the entire image.

• Specify region of interest via port. When you select this option, the ROI port appears on the block. Enter a four-element vector of integer values, [x y width height], where [x y] are the coordinates of the upper-left corner of the area.

 Note:   If you specify values that are outside the image, the block sets the values to the image boundaries.

Use antialiasing

Perform a smoothing algorithm on the line, polygon, or circle. This parameter is visible if, for the Shape parameter, you select Lines, Polygons, or Circles.

Image signal

Specify how to input and output a color video signal. Select one of the following:

• One multidimensional signal, the block accepts an M-by-N-by-P color video signal, where P is the number of color planes, at one port.

• Separate color signals, additional ports appear on the block. Each port accepts one M-by-N plane of an RGB video stream.