Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

# insertShape

Insert shapes in image or video

## Syntax

``RGB = insertShape(I,shape,position)``
``RGB = insertShape(___,Name,Value)``

## Description

example

````RGB = insertShape(I,shape,position)` returns a truecolor image with `shape` inserted. The input image, `I`, can be either a truecolor or grayscale image. You draw the shapes by overwriting pixel values.```

example

````RGB = insertShape(___,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.```

## Examples

collapse all

Read the image.

`I = imread('peppers.png');`

Draw a circle with a border line width of 5.

`RGB = insertShape(I,'circle',[150 280 35],'LineWidth',5);`

Draw a filled triangle and a filled hexagon.

```pos_triangle = [183 297 302 250 316 297]; pos_hexagon = [340 163 305 186 303 257 334 294 362 255 361 191]; RGB = insertShape(RGB,'FilledPolygon',{pos_triangle,pos_hexagon},... 'Color', {'white','green'},'Opacity',0.7);```

Display the image.

`imshow(RGB);` ## Input Arguments

collapse all

Input image, specified in truecolor or 2-D grayscale.

Data Types: `single` | `double` | `int16` | `uint8` | `uint16`

Type of shape, specified as a character vector. The vector can be, `'Rectangle'`, `'FilledRectangle'`, `'Line'`, `'Polygon'`, `'FilledPolygon'`, `'Circle'`, or `'FilledCircle'`.

Data Types: `char`

Position of shape, specified according to the type of shape, described in the table.

ShapePositionShape Drawn
`'Rectangle'`
`'FilledRectangle'`
M-by-4 matrix where each row specifies a rectangle as $\left[\begin{array}{cccc}x& y& width& height\end{array}\right]$.

`$\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]$` `'Line'`

For one or more disconnected lines, an M-by-4 matrix, where each four-element vector [x1, y1, x2,y2], describe a line with endpoints, [x1 y1] and [x2 y2].  For one or more line segments, an M-by-2L matrix, where each row is a vector representing a polyline with L number of vertices.

`$\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]$`

.

The polyline always contains (L-1) number of segments because the first and last vertex points do not connect. For lines with fewer segments, repeat the ending coordinates to fill the matrix.

You can also specify the shapes as a cell array of M vectors.

{[x11,y11,x12,y12,...,x1p,y1p], [x21,y21,x22,y22,.....,x2q,y2q], ... [xM1,yM1,xM2,yM2,.......,xMr,yMr]}

p, q, and r specify the number of vertices. `'Polygon'`
`'FilledPolygon'`
An M-by-2L matrix, where each row represents a polygon with L number of vertices. Each row of the matrix corresponds to a polygon. For polygons with fewer segments, repeat the ending coordinates to fill the 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]$`

You can also specify the shapes as a cell array of M vectors:

{[x11,y11,x12,y12,...,x1p,y1p], [x21,y21,x22,y22,.....,x2q,y2q], ... [xM1,yM1,xM2,yM2,.......,xMr,yMr]}

p, q, and r specify the number of vertices. `'Circle'`
`'FilledCircle'`
An M-by-3 matrix, where each row is a vector specifying a circle as $\left[\begin{array}{ccc}x& y& radius\end{array}\right]$. The $\left[\begin{array}{cc}x& y\end{array}\right]$ coordinates represent the center of the circle.

`$\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]$` Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### Name-Value Pair Arguments

Specify optional comma-separated 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`.

Example: `'Color'`,`'yellow'` specifies yellow for the shape color.

Shape border line width, specified in pixels, as a positive scalar integer. This property only applies to the `'Rectangle'`, `'Line'`, `'Polygon'`, or `'Circle'` shapes.

Data Types: `uint8` | `uint16` | `int16` | `double` | `single`

Shape color, specified as the comma-separated pair consisting of '`Color`' and either a character vector, cell array of character vector, or matrix. You can specify a different color for each shape, or one color for all shapes.

To specify a color for each shape, set `Color` to a cell array of color character vectors or an M-by-3 matrix of M number of RGB (red, green, and blue) color values.

To specify one color for all shapes, set `Color` to either a color character vector or an [R G B] vector. The [R G B] vector contains the red, green, and blue values.

Supported colors: `'blue'`, `'green'`, `'red'`, `'cyan'`, `'magenta'`, `'black'`,`'black'`, and `'white'`.

Data Types: `cell` | `char` | `uint8` | `uint16` | `int16` | `double` | `single`

Opacity of filled shape, specified as the comma-separated pair consisting of '`Opacity`' and a scalar value in the range [0 1]. The `Opacity` property applies for the `FilledRectangle`, `FilledPolygon`, and `FilledCircle` shapes.

Data Types: `double` | `single` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

Smooth shape edges, specified as the comma-separated pair consisting of '`SmoothEdges`' and a logical value of `true` or `false`. A `true` value enables an anti-aliasing filter to smooth shape edges. This value applies only to nonrectangular shapes. Enabling anti-aliasing requires additional time to draw the shapes.

Data Types: `logical`

## Output Arguments

collapse all

Output image, returned as a truecolor image.

Download ebook