uiannotate
R2026bDescription
The uiannotate function enables you to display one or more
static ROI annotations in a Viewer object, which includes images displayed
using the imageshow
function and volumes displayed using volshow. For
best performance, specify all the ROIs for your viewer together in a single function call.
A single ROI object that contains all the static annotations renders more efficiently than
separate objects. You can specify the initial appearance of the ROIs by using name-value
arguments. After you create the ROIs, you can use the properties of the corresponding
object to further customize the ROIs. To create interactive ROI annotations in a viewer,
see uidraw.
Examples
Read a grayscale image of rice and binarize it to segment the rice grains from the background.
I = imread("rice.png");
bw = imbinarize(I);Extract bounding box position data for each detected region.
props = regionprops(bw,"BoundingBox");
position = vertcat(props.BoundingBox);Display the image using imageshow and overlay all bounding box annotations in a single uiannotate call. The function returns one Rectangles object, roi. When working with many annotations, displaying the annotations using one call improves performance compared to making separate calls for each shape.
viewer = imageshow(I);
roi = uiannotate(viewer,"rectangle",position);
Read an image of nickels and dimes and use imfindcircles to detect the coins. The function returns the coin centers and radii.
I = imread("coins.png");
[centers,radii] = imfindcircles(I,[15 40],Sensitivity=0.9);Combine the centers and radii into one position variable in the format that the uiannotate function expects.
circlePos = [centers radii];
To assign each coin a different color, create an n-by-3 array of RGB values sampled from a colormap.
colors = lines(numel(radii));
Display the image using imageshow and overlay the detected circles in a single uiannotate call, specifying the Color name-value argument value as the RGB color array.
viewer = imageshow(I);
roi = uiannotate(viewer,"circle",circlePos,Color=colors);
Optionally, you can update the appearance of the annotations by setting properties of the ROI object returned by uiannotate. The object properties enable more customization options, such as the opacity of the circle faces.
roi.FaceAlpha = 0.3;

Read a grayscale image of household objects and threshold it to create a binary mask.
RGB = imread("pillsetc.png");
gray = im2gray(RGB);
bw = imbinarize(gray);Get the boundary coordinates of the objects in the binary mask.
B = bwboundaries(bw,"noholes");Define the object positions in the format the uiannotate function expects for freehand shapes. Concatenate the boundary cell arrays into a single matrix, inserting a [NaN NaN] row between each boundary to separate them. Flip columns from the [row col] format returned by bwboundaries to the [x y] format uiannotate expects.
positionArray = []; for k = 1:numel(B) boundary = B{k}; positionArray = [positionArray; boundary(:,[2 1]); NaN NaN]; end
Display the image using imageshow and overlay all boundaries in a single uiannotate call.
viewer = imageshow(RGB);
roi = uiannotate(viewer,"polyline",positionArray);
Load a volume containing spheres of various sizes.
load("spheresVol.mat")Binarize the volume and use regionprops3 to extract bounding boxes for each sphere.
bw = imbinarize(V); stats = regionprops3(bw,"BoundingBox","EquivDiameter"); bbox = stats.BoundingBox;
Identify spheres with an equivalent diameter less than twenty pixels, and extract their bounding box coordinates.
idx = find(stats.EquivDiameter < 20); position = bbox(idx,:);
Display the volume with volshow and overlay the bounding boxes for the small spheres as cuboid annotations using uiannotate.
viewer = volshow(V); roi = uiannotate(viewer,"cuboid",position,Color="red");

Input Arguments
Viewer to draw the ROI in, specified as a Viewer object or a
child of a Viewer object. For example, you can specify a
Viewer object created using the viewer2d or viewer3d function. Examples of child objects of a viewer include
the Image object returned by imageshow and
the Volume object returned by
volshow.
This argument sets the Parent property of the output ROI
object, and adds the ROI object to the Annotations property of the specified
Viewer object. If you specify a child of a
Viewer object, then the function adds the ROI object to the
parent Viewer object itself.
Shape of the ROI to draw, specified as one of the values in the table.
| Value | Description |
|---|---|
"arrow"
| Create an Arrows object |
"circle"
| Create a Circles object |
"cuboid"
| Create a Cuboids object |
"cylinder" | Create a Cylinders object |
"ellipse" | Create an Ellipses object |
"ellipsoid" | Create an Ellipsoids object |
"line" | Create a Lines object |
"plus"
| Create a Plusses object |
"point"
| Create a Points object |
"polyline"
| Create a Polylines object |
"rectangle"
| Create a Rectangles object |
"sphere"
| Create a Spheres object |
Data Types: char | string
Position for the ROI, specified as a numeric vector or numeric matrix. You can
use the Position argument to specify multiple annotations of
the same shape within a single ROI object.
You can specify each position using one of the options listed in this table.
For all positions, n is the number of shapes in the ROI. Values
that omit z place the ROI in the default 2-D image plane, with
z equal to 1.
| Shape | Position |
|---|---|
"arrow" | For arrows defined by endpoints, specify an
n-by-4 matrix or an
n-by-6 matrix. Each row specifies an arrow in
the form For arrows
defined by normal vectors and magnitudes, specify an
n-by-5 matrix or an
n-by-7 matrix. Each row specifies an arrow in
the form |
"cuboid" | Axis-aligned cuboids:
Rotated cuboids:
|
"cylinder" | Specify an n-by-5 matrix or an
n-by-8 matrix. Each row specifies a
cylinder in the format |
"ellipse" | Specify an n-by-4 matrix or an
n-by-5 matrix. Each row specifies an
ellipse in the format |
"ellipsoid" | Specify an n-by-6 matrix or an
n-by-9 matrix. Each row specifies an
ellipsoid in the format |
"line" | Specify an n-by-4 or
n-by-6 matrix. Each row specifies the
endpoints of one line in the form |
"plus" | Specify an n-by-2 or
n-by-3 matrix. Each row specifies the
center coordinates of one plus shape in the form |
"point" | Specify an n-by-2 or
n-by-3 matrix. Each row specifies the
coordinates of one point in the form |
"polyline" | Specify an n-by-2 or n-by-3 matrix, where each row specifies a vertex or a polyline separator.
|
"rectangle"
| Axis-aligned rectangles:
Rotated rectangles:
|
"sphere" | Specify an n-by-4 numeric matrix. Each
row specifies a sphere in the format |
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.
Example:
uiannotate(viewer,"rectangle",[1 1 10 20; 4 18 10 20],Color="red")
creates a Rectangles object consisting of two red rectangle
shapes.
ROI color, specified as an RGB triplet, a hexadecimal color code, a color
name, or a short color name. If you specify multiple ROIs by using the
Position argument, you can specify one color to apply
to all of the ROIs, or separate colors for each ROI. Specify colors for
n ROIs as an n-by-3 numeric matrix of
RGB
triplets.
This table shows the default colors for static ROIs.
| RGB Triplet | Appearance | Default Use |
|---|---|---|
[0 0.5610 1] |
| This is the default color when creating a static ROI in dark mode. |
[0.8660 0.3290 0] |
| This is the default color when creating a static ROI in light mode. |
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range
[0,1], for example,[0.4 0.6 0.7].A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#) followed by three or six hexadecimal digits, which can range from0toF. The values are not case sensitive. Therefore, the color codes"#FF8800","#ff8800","#F80", and"#f80"are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.
| Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan"
| "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
This table lists the default color palettes for plots in the light and dark themes.
| Palette | Palette Colors |
|---|---|
Before R2025a: Most plots use these colors by default. |
|
|
|
You can get the RGB triplets and hexadecimal color codes for these palettes
using the orderedcolors and rgb2hex functions. For example, this code gets the RGB triplets
for the "gem" palette and converts them to hexadecimal color
codes.
RGB = orderedcolors("gem");
H = rgb2hex(RGB);Example: Color="r" specifies that all shapes of the ROI
are colored red.
Example: Color=[0 0.447 0.741] specifies that all shapes
of the ROI have a custom color define by the RGB triplet [0 0.447
0.741].
Example: Color=parula(5) specifies that each of five
shapes within an ROI have a different color from the parula
colormap.
Opacity of the ROI, specified as a number in the range [0, 1]. When the value is
1, the ROI is completely opaque. When the value is
0, the ROI is completely transparent.
ROI visibility, specified as "on" or "off", or as
a numeric or
logical 0 (false) or 1
(true). A value of "on" is equivalent to
true, and "off" is equivalent to
false. The value is stored as an on/off logical value of type
OnOffSwitchState.
| Value | Description |
|---|---|
"on" | Display the ROI. |
"off" | Hide the ROI without deleting it. You can still access the properties of an invisible ROI. |
Output Arguments
ROI, returned as an ROI object of the type specified by
shape. For example, if you specify
shape as "circle", then the function
creates a Circles object.
Version History
Introduced in R2026b
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)