The toolbox provides a set of classes to create regions of interest of various shapes, including circles, ellipses, polygons, lines, polylines, points, and rectangles. You can also draw freehand shapes and freehand shapes that snap to edges in the image. In this figure, you see a polygonal ROI drawn over an image.
The toolbox includes ten classes that support ROI creation. You use these classes to create ROIs of certain shapes on an axes. These classes have properties, methods, and events that provide flexibility to ROI creation. Using these classes, you can draw ROIs interactively, using the mouse, or programmatically by specifying information about the size and shape of the ROI. Once you create the ROI object, you can perform operations on the ROI such as creating a mask. You can also control aspects of its behavior and appearance and respond to events that occur in the ROI, such as mouse clicks and movement.
The basic workflow when using the ROI classes interactively is to instantiate an
object of the class and then call the
Read an image into the MATLAB workspace and display it in a figure.
I = imread('pears.png'); imshow(I)
Instantiate an ROI object. The example creates an elliptical ROI on the
image, using the
StripeColor parameter to customize the
look of the edge.
h = images.roi.Ellipse('StripeColor','m');
draw method of the
object to draw the ROI on the axes. When you move the mouse pointer over the
image, the cursor changes to a crosshairs. Click and move the cursor over
the image to create the elliptical shape. You can move the ellipse and
change the shape of the ellipse interactively.
If you draw the ROI programmatically, you do not need to call the
draw method. For more information, look at the class
reference page for the ROI shape you want to create, such as,
As a convenience, the toolbox provides ten functions that let you draw ROIs without directly calling the ROI classes. These functions create instances of the ROI classes.
The basic workflow when using these ROI convenience functions interactively is to
call the function. The function instantiates the class and calls the
draw method to start interactive placement of the ROI. You
can specify the value of many different properties when you create the object using
convenience functions, such as
You can use ROIs in apps created with App Designer. However, you must use a
Axes object in your app. You cannot parent an ROI in a
UIAxes. In addition, you must explicitly specify the axes
when calling the ROI creation function, as an input argument or using the
'Parent' name/value pair. There are a few limitations when
using ROIs in apps in this way:
The mouse cursor does not update when you hover over the ROI. The cursor is always an arrow.
The ROI does not change color when you hover over it.
The ROI right-click menu (
UIContextMenu) is not
The following code, while not a typical app-creation workflow, shows how to
specify an ROI in an
Axes in an app
fig = uifigure;
Axes in the
can only create ROIs in an
Axes object, not a
ax = axes(fig);
Create the ROI in the
Axes. Call any of the ROI
creation functions, such as
drawcircle, specifying the
Axes as an argument. Move the cursor over the axes
and click and drag the mouse to draw the ROI.
h = drawcircle(ax);
You can also use any of the ROI creation classes, such as
images.roi.Circle, to create the ROI. If you use the
classes, you must also call the
draw function specifying
the ROI object as an argument.