insertShape
Insert shapes in image or video
Description
specifies options using one or more name-value arguments in addition to the previous syntax.
For example,RGB
= insertShape(___,Name=Value
)insertShape(I,"circle",[150 280 35],LineWidth=5)
sets the
line width value to 5
.
Examples
Insert Circle and Filled Shapes on an Image
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);
Draw a filled triangle and a filled hexagon on the 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);
Input Arguments
I
— Input image
M-by-N-by-3 truecolor | M-by-N 2-D grayscale
image
Input image, specified in truecolor or 2-D grayscale.
Data Types: single
| double
| int16
| uint8
| uint16
shape
— Type of shape
"rectangle"
| "filled-rectangle"
| "line"
| "polygon"
| "filled-polygon"
| "circle"
| "filled-circle"
| "projected-cuboid"
Type of shape, specified as "rectangle"
,
"filled-rectangle"
, "line"
,
"polygon"
, "filled-polygon"
,
"circle"
, or "filled-circle"
,
"projected-cuboid"
.
Data Types: char
position
— Position of shape
matrix | vector | cell array
Position of shape, specified according to the type of shape, described in the table.
Shape | Position | Example |
---|---|---|
"rectangle" "filled-rectangle" | For one or more rectangles, specify M-by-4 matrix where each row specifies
a rectangle as .
| Two rectangles, M=2 |
| For one or more disconnected lines, specify an M-by-4 matrix, where each four-element vector, , describes a line with endpoints. 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 an M-by-2L matrix, where each row is a vector, , of consecutive vertex locations, representing a polyline or a polygon with L number of vertices. | Two polygons with equal number of vertices, M=2, L=5
| |
For one or more polylines or polygons with unequal number of vertices, specify an M-by-1 cell array, where each cell contains an L-by-2 matrix of [x,y] vertices, or a 1-by-2L vector, , of consecutive vertex locations. The value of L can be different for each cell element. For example, | Two polygons with unequal number of vertices, M=2 | |
"circle" "filled-circle" | An M-by-3 matrix, where each row is a vector
specifying a circle as .
The coordinates
represent the center of the circle.
|
|
"projected-cuboid" | An 8-by-2-by-M array or an M-by-8 matrix, where M specifies a projected cuboid. When specified as an 8-by-2-by-M array, each row must contain the 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 an M-by-8 matrix, each row specifies the front-facing and rear-facing sides of a projected cuboid in the form, 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. |
|
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is 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 enclose
Name
in quotes.
Example: Color="yellow"
sets the shape color to yellow.
LineWidth
— Shape border line width
1
(default) | positive scalar integer
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
Color
— Shape color
"green"
(default) | character vector | cell array of character vectors | string scalar | string array | M-by-3 matrix
Shape color, specified as a character vector, cell array of character vectors,
vector, or an M-by-3 matrix. 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 |
|
1-by-3 vector (RGB triplet) |
| |
Specify a color for each shape | M-element vector |
|
M-by-3 matrix, as a list of RGB values | 255 0 0 255 0 0 0 255 255 ![]() |
Data Types: cell
| char
| uint8
| uint16
| int16
| double
| single
Opacity
— Opacity of filled shape
0.6 (default) | range of [0
1
]
Opacity of filled shape, specified as a scalar value in the range [0 1]. The
Opacity
property applies to the
"filled-rectangle"
, "filled-polygon"
, and the
"filled-circle"
shapes.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
SmoothEdges
— Smooth shape edges
1
(true
) (default) | 0
(false
)
Smooth shape edges, specified as a logical value of 1
(true
) or 0
(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
RGB
— Output image
M-by-N-by-3 truecolor
Output image, returned as a truecolor image.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
The
Color
andSmoothEdges
name-value arguments must be compile-time constants.
Version History
Introduced in R2014aR2022b: Visualize projected cuboids
Added support to visualize projected cuboids.
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)