Contenuto principale

Create Axes-Based ROIs

R2026b

Axes-based ROIs enable you to draw and edit regions of interest on images displayed using imshow or in MATLAB Graphics axes. Axes-based ROIs offer maximum customization of appearance and behavior, including line width, marker style, and edge color. Use axes-based ROIs for fine-grained control over ROI appearance, integration with existing axes-based apps, or access to the assisted freehand, crosshair, or cuboid ROI types.

You can create axes-based ROIs using these approaches.

  • Using a creation convenience function such as drawcircle, which enables you to interactively draw or programmatically specify an ROI in a single command.

  • Using an object creation function such as images.roi.Circle. Use this approach when you want to specify the appearance and behavior of the ROI before you specify the shape and position of the ROI. After you create the ROI, use the draw function to interactively draw the ROI on an image. The draw function also enables you to redraw an existing ROI, preserving the appearance of the ROI.

The table shows the supported ROIs and their respective creation convenience functions.

DescriptionROI Creation Convenience FunctionObject Creation Function

AssistedFreehand ROI that snaps to edges of existing objects in the image

Assisted Freehand ROI tracing the edge of an object between selected waypoints.

drawassistedimages.roi.AssistedFreehand

Circle ROI

Blue Circle ROI drawn over a round object in an image.

drawcircleimages.roi.Circle

Crosshair ROI that consists of two perpendicular lines

A horizontal line and a vertical line intersect to form a Crosshair ROI.

drawcrosshairimages.roi.Crosshair

3-D Cuboid ROI

Blue Cuboid ROI drawn over a 3-D region in a point cloud.

drawcuboidimages.roi.Cuboid

Ellipse ROI

Blue Ellipse ROI drawn over a round object in an image.

drawellipseimages.roi.Ellipse

Freehand ROI that follows the path of the mouse

Freehand ROI tracing the edge of an object in an image.

drawfreehandimages.roi.Freehand

Line ROI that consists of a single line segment

Blue Line ROI drawn over the widest part of an object in an image.

drawlineimages.roi.Line

Point ROI

Blue Point ROI drawn near the center of an object in an image.

drawpointimages.roi.Point

Polygon ROI that consists of a closed set of line segments

Blue Polygon ROI with 10 vertices approximating the boundary of an object in an image.

drawpolygonimages.roi.Polygon

Polyline ROI that consists of an open set of line segments

Blue Polyline ROI with 12 line segments approximating the boundary of an object in an image.

drawpolylineimages.roi.Polyline

Rectangle ROI

Blue Rectangle ROI encompassing an object in an image.

drawrectangleimages.roi.Rectangle

Create Axes-Based ROIs

This example shows how to create axes-based ROIs.

Create ROI Using Convenience Function

Creation convenience functions enable you to create axes-based ROIs in a single command. You can use a creation function to interactively draw the ROI over an image, or use name-value arguments to define the ROI and skip drawing.

Read and display an image.

I = imread("pears.png");
imshow(I)

Create an Ellipse object by using the drawellipse function. Customize the appearance of the ROI by specifying the StripeColor name-value argument. The function enables an interactive drawing mode in the image display. Click the image and drag to draw the ROI.

roi = drawellipse(StripeColor="y");

To create an ellipse fully programmatically, specify the size and position using name-value arguments. For an ellipse, specify the Center, the SemiAxes, and the RotationAngle values.

roi2 = drawellipse(Center=[150 225],SemiAxes=[80 50],RotationAngle=35);

Create ROI Using Object Creation Function

Object creation functions enable you to specify the appearance and behavior of the ROI before you draw it or specify its position programmatically.

Display the pears image in a new figure.

imshow(I)

Create an Ellipse object, specifying its appearance but not its size or position.

roi3 = images.roi.Ellipse(Color="c",StripeColor="r");

To enable interactive drawing of the ROI, use the draw function. Then, click the image and drag to draw the ROI.

draw(roi3)

To create an ellipse fully programmatically, specify the size and position using name-value arguments. To display the object, you must specify the parent axes as an input argument. Otherwise, the function creates the object but does not display it.

roi4 = images.roi.Ellipse(gca,Center=[90 390],SemiAxes=[80 50],RotationAngle=50);

Create Axes-Based ROIs in Apps

To add axes-based ROIs, parent the ROI object to a UI axes in your app. For example, in the Design View of App Designer, drag an Axes from the Component Library onto the canvas, then add a startupFcn callback by right-clicking the app node from the top of the Component Browser hierarchy and selecting Callbacks > Add StartupFcn callback. Within the callback, specify code to display the image and create a circular ROI. You must specify the UI axes, app.UIAxes, as the parent of the image and the ROI.

function startupFcn(app)
    imshow("pears.png",Parent=app.UIAxes);
    c = drawcircle(app.UIAxes);
end

Click Run to save and run the app. The app opens and displays the image. Click and drag to draw the circle ROI on the image.

App Designer app window with a UIAxes in the top-left corner. The axes contains an image of pears and a circular ROI.

You can also create an axes-based ROI using its object creation function, such as images.roi.Circle. After you create the ROI, call the draw function to define the shape and position of the ROI and add it to a UI axes in your app.

See Also

Topics