Main Content

insertShape

Insert shapes in image or video

Description

example

RGB= insertShape(I,shape,position)returns a truecolor image withshapeinserted. 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)specifies options using one or more name-value arguments in addition to the previous syntax. For example,insertShape(I,"circle",[150 280 35],LineWidth=5)sets the line width value to5.

Examples

collapse all

Read an image into the workspace.

I = imread("peppers.png");

Draw a circle on the image with a border line width of 5.

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

画一个三角形和一个充满了六边形image.

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,"filled-polygon",{pos_triangle,pos_hexagon},...Color=["white","green"],Opacity=0.7);

Display the resulting image.

imshow(RGB);

Figure contains an axes object. The axes object contains an object of type image.

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“矩形”,"filled-rectangle","line","polygon","filled-polygon","circle", or"filled-circle","projected-cuboid".

Data Types:char

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

Shape Position Example
“矩形”
"filled-rectangle"
For one or more rectangles, specifyM-by-4 matrix where each row specifies a rectangle as [ x y w i d t h h e i g h t ] .

[ x 1 y 1 w i d t h 1 h e i g h t 1 x 2 y 2 w i d t h 2 h e i g h t 2 x M y M w i d t h M h e i g h t M ]

Two rectangles,M=2

"line"

"polygon"

"filled-polygon"

For one or more disconnected lines, specify anM-by-4 matrix, where each four-element vector, [ x 1 y 1 x 2 y 2 ] , describes a line with endpoints.

[ x 11 y 11 x 12 y 12 x 21 y 21 x 22 y 22 x M 1 y M 1 x M 2 x M 2 ]

The polyline always contains (L-1) number of segments because the first and last vertex points do not connect.

Two lines,M=2

For one or more polylines or polygons with the same number of vertices, specify anM-by-2Lmatrix, where each row is a vector, [ x 1 y 1 x 2 y 2 ... x L y L ] , of consecutive vertex locations, representing a polyline or a polygon withLnumber of vertices.

[ x 11 y 11 x 12 y 12 x 1 L y 1 L x 21 y 21 x 22 y 22 x 2 L y 2 L x M 1 y M 1 x M 2 y M 2 x M L y M L ]

Two polygons with equal number of vertices,M=2,L=5

For one or more polylines or polygons with unequal number of vertices, specify anM-by-1 cell array, where each cell contains anL-by-2 matrix of [x,y] vertices, or a 1-by-2Lvector, [ x 1 y 1 x 2 y 2 ... x L y L ] , of consecutive vertex locations.

The value ofLcan be different for each cell element. For example,

{ [ x 1 y 1 x 2 y 2 ] , [ x 1 y 1 x 2 y 2 x 3 y 3 ] }

Two polygons with unequal number of vertices,M=2two polygons, one with 5 vertices and one with 4 vertices

"circle"
"filled-circle"
AnM3矩阵, where each row is a vector specifying a circle as [ x y r a d i u s ] . The [ x y ] coordinates represent the center of the circle.

[ x 1 y 1 r a d i u s 1 x 2 y 2 r a d i u s 2 x M y M r a d i u s M ]

"projected-cuboid"

An 8-by-2-by-Marray or anM-by-8 matrix, whereMspecifies a projected cuboid.

When specified as an 8-by-2-by-Marray, each row must contain the [ x y ] location of a projected cuboid vertex. The vertices are connected to form a cuboid with six faces. The order of the input vertices must match the order shown in the diagram.

When specified as anM-by-8 matrix, each row specifies the front-facing and rear-facing sides of a projected cuboid in the form,

[ x 1 y 1 w 1 h 1 x 2 y 2 w 2 h 2 ]

where, [x1 y1] and [x2 y2] specify the upper-left coordinates of the front-facing and back-facing sides, respectively. [w1 h1] and [w2 h2] specify the corresponding width and height.

Cuboid showing numbered vertices. The number starts with 1 assigned to the top right corner of the front facing rectangle. Going counter-clockwise 1-4 for the top face of cuboid, then 5-8 for the bottom face. The positive Z-axis goes up, the positive Y-axis goes to the right and the positive X-axis faces forward.

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

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:Color="yellow"sets the shape color to yellow.

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 a character vector, cell array of character vectors, vector, or anM3矩阵. You can specify a different color for each shape or one color for all shapes. Color values must be specified in the range[0,255]. Values that have a range of[0,1]must be scaled by a value of 255 before using it with this function. For example,[255 255 255].*colorvalue.

Supported colors are:"blue","green","red","cyan","magenta","yellow","black", and"white".

Color Format Example
Specify one color for all shapes

String or character color name

"r"

"red"

1-by-3 vector (RGB triplet)

[255 0 0]1-by-3 grid, with columns labeled r,g,b respectively.

Specify a color for each shape M-element vector

["red","yellow","blue"]

M3矩阵, as a list of RGB values

255 0 0 255 0 0 0 255 255
M-by-3 grid, with columns labeled r,g,b respectively.

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

Opacity of filled shape, specified as a scalar value in the range [0 1]. TheOpacityproperty applies to the"filled-rectangle","filled-polygon", and the"filled-circle"shapes.

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

Smooth shape edges, specified as a logical value of1(true) or0(false). Atruevalue 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.

Extended Capabilities

Version History

Introduced in R2014a

expand all