Main Content

drawrectangle

Create customizable rectangular ROI

Description

The drawrectangle function creates a Rectangle object that specifies the shape and position of a rectangular region of interest (ROI). You can create the ROI interactively by drawing the ROI over an image using the mouse, or programmatically by using name-value arguments. You can also specify the initial appearance and behavior of the ROI.

Blue Rectangle ROI drawn over an object in an image.

After you create the ROI, you can use object properties, object functions, and event notifications to customize the shape, position, appearance, and behavior of the ROI. For more information about using these capabilities, see Tips.

roi = drawrectangle creates a Rectangle object and enables interactive drawing of the ROI on the current axes.

To draw the ROI, position the pointer on the image. The cursor changes to a fleur shape. Click and drag to draw the rectangular ROI. To finish the ROI, release the mouse button. For more information about using the ROI, including keyboard shortcuts and context menu options, see Tips.

example

roi = drawrectangle(ax) creates the ROI in the axes specified by ax.

roi = drawrectangle(___,Name=Value) modifies the appearance and behavior of the ROI using one or more name-value arguments.

Examples

collapse all

Read an image into the workspace and display it.

imshow(imread('baby.jpg'))

Draw a red rectangular ROI with the label 'OuterRectangle'.

r1 = drawrectangle('Label','OuterRectangle','Color',[1 0 0]);

Draw another rectangular ROI, restricting the drawing area to the area inside the first rectangle.

r2 = drawrectangle('Label','InnerRectangle','DrawingArea',r1.Position);

Read image into the workspace and display it.

I = imread('baby.jpg');
figure
imshow(I)

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

Draw a rectangular ROI over the image, using named parameters to specify the location and size of the rectangle. The example also specifies that the edge of the rectangle is a striped line.

h = drawrectangle('Position',[500,500,1000,1000],'StripeColor','r');

Figure contains an axes object. The hidden axes object contains 2 objects of type image, images.roi.rectangle.

Read an image into the workspace.

I = imread('cameraman.tif');

Display the image.

imshow(I); 

Draw a rectangular ROI on the image.

roi = drawrectangle('Color','r');

Set up listeners for ROI moving events. When you move it, the ROI sends notifications of these events and executes the callback function you specify.

addlistener(roi,'MovingROI',@allevents);
addlistener(roi,'ROIMoved',@allevents);

The allevents callback function displays the previous position and the current position of the ROI.

function allevents(src,evt)
    evname = evt.EventName;
    switch(evname)
        case{'MovingROI'}
            disp(['ROI moving previous position: ' mat2str(evt.PreviousPosition)]);
            disp(['ROI moving current position: ' mat2str(evt.CurrentPosition)]);
        case{'ROIMoved'}
            disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
            disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
    end
end

Input Arguments

collapse all

Parent of ROI, specified as an Axes object or a UIAxes object. For information about using an ROI in a UIAxes, including important limitations, see Using ROIs in Apps Created with App Designer.

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: roi = drawrectangle(Color="y") creates a yellow colored Rectangle object.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: roi = drawrectangle("Color","y") creates a yellow colored Rectangle object.

Aspect ratio of the rectangle, specified as a nonnegative number. The aspect ratio is defined as height/width.

ROI color, specified as an RGB triplet, a color name, or a short color name.

You can specify any color using an RGB triplet. An RGB triplet is a 3-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].

You can specify some common colors by name as a string scalar or character vector. This table lists the named color options and the equivalent RGB triplets.

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

A rectangle colored pure red

"green""g"[0 1 0]

A rectangle colored pure green

"blue""b"[0 0 1]

A rectangle colored pure blue

"cyan" "c"[0 1 1]

A rectangle colored pure cyan

"magenta""m"[1 0 1]

A rectangle colored pure magenta

"yellow""y"[1 1 0]

A rectangle colored pure yellow

"black""k"[0 0 0]

A rectangle colored black

"white""w"[1 1 1]

A rectangle colored white

Here are the RGB triplets for the default colors that MATLAB® uses in many types of plots.

RGB TripletAppearance
[0 0.4470 0.7410]

A rectangle colored medium blue

[0.8500 0.3250 0.0980]

A rectangle colored reddish-orange

[0.9290 0.6940 0.1250]

A rectangle colored dark yellow

[0.4940 0.1840 0.5560]

A rectangle colored dark purple

[0.4660 0.6740 0.1880]

A rectangle colored light green

[0.3010 0.7450 0.9330]

A rectangle colored light blue

[0.6350 0.0780 0.1840]

A rectangle colored dark red

Example: "Color","r"

Example: "Color","green"

Example: "Color",[0 0.4470 0.7410]

Context menu that displays when you right-click the ROI, specified as a ContextMenu object. You can create a custom context menu by using the uicontextmenu function and then configuring context menu properties.

Context menu provides an option to delete the ROI, specified as a numeric or logical 1 (true) or 0 (false). When the value is true, you can delete the ROI interactively using the context menu. When the value is false, the context menu option to delete the ROI is disabled.

In both cases, you can delete the ROI outside of the context menu by using the delete function.

Area of the axes in which you can interactively place the ROI, specified as one of the values in this table.

ValueDescription
"auto"The drawing area is the current axes limits (default).
"unlimited"The drawing area has no boundary and ROIs can be drawn or dragged to extend beyond the axes limits.
[x,y,w,h]The drawing area is restricted to a rectangular region beginning at (x,y), and extending to width w and height h.

Transparency of the ROI face, specified as a number in the range [0, 1]. When the value is 1, the ROI face is completely opaque. When the value is 0, the ROI face is completely transparent.

ROI face can capture clicks, specified as a numeric or logical 1 (true) or 0 (false). When true, the ROI face captures mouse clicks. When false, the ROI face does not capture mouse clicks.

Aspect ratio remains constant during interaction, specified as a numeric or logical 0 (false) or 1 (true). When the value is true, the aspect ratio remains constant when you draw or resize the ROI. When the value is false, you can change the aspect ratio when drawing or resizing the ROI.

Visibility of the ROI handle in the Children property of the parent, specified as one of the values in this table.

ValueDescription
"on"The object handle is always visible (default).
"off"The object handle is hidden at all times.
"callback" The object handle is visible from within callbacks or functions invoked by callbacks, but not from within functions invoked from the command line.

Interactivity of the ROI, specified as one of the values in this table.

ValueDescription
"all"The ROI is fully interactable.
"none"The ROI is not interactable, and no drag points are visible.
"translate"The ROI can be translated (moved) within the drawing area.

ROI label, specified as a character vector or string scalar. By default, the ROI has no label ('').

Transparency of the text background, specified as a number in the range [0, 1]. When set to 1, the text background is completely opaque. When set to 0, the text background is completely transparent.

Label text color, specified as an RGB triplet, a color name, or a short color name.

You can specify any color using an RGB triplet. An RGB triplet is a 3-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].

You can specify some common colors by name as a string scalar or character vector. This table lists the named color options and the equivalent RGB triplets.

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

A rectangle colored pure red

"green""g"[0 1 0]

A rectangle colored pure green

"blue""b"[0 0 1]

A rectangle colored pure blue

"cyan" "c"[0 1 1]

A rectangle colored pure cyan

"magenta""m"[1 0 1]

A rectangle colored pure magenta

"yellow""y"[1 1 0]

A rectangle colored pure yellow

"black""k"[0 0 0]

A rectangle colored black

"white""w"[1 1 1]

A rectangle colored white

Here are the RGB triplets for the default colors that MATLAB uses in many types of plots.

RGB TripletAppearance
[0 0.4470 0.7410]

A rectangle colored medium blue

[0.8500 0.3250 0.0980]

A rectangle colored reddish-orange

[0.9290 0.6940 0.1250]

A rectangle colored dark yellow

[0.4940 0.1840 0.5560]

A rectangle colored dark purple

[0.4660 0.6740 0.1880]

A rectangle colored light green

[0.3010 0.7450 0.9330]

A rectangle colored light blue

[0.6350 0.0780 0.1840]

A rectangle colored dark red

Example: "LabelTextColor","r"

Example: "LabelTextColor","green"

Example: "LabelTextColor",[0 0.4470 0.7410]

Visibility of the ROI label, specified as one of these values.

ValueDescription
'on'Label is visible when the ROI is visible, and the Label property is nonempty (default).
'hover'

Label is visible only when the mouse hovers over the ROI.

'inside'Label is visible only when there is adequate space inside the ROI to display it.
'off'Label is not visible.

Width of the ROI border, specified as a positive number in points. The default value is three times the number of points per screen pixel, such that the border is three pixels wide.

Marker size, specified as a positive number in points. The default value is eight times the number of points per screen pixel, such that markers are eight pixels in size.

ROI parent, specified as an Axes or UIAxes object. For information about using an ROI in a UIAxes, including important limitations, see Using ROIs in Apps Created with App Designer.

Position of the ROI, specified as a 1-by-4 numeric vector of the form [xmin, ymin, width, height]. xmin and ymin specify the coordinates of the upper left corner of the rectangle. width and height specify the width and height of the rectangle and must be nonnegative.

Ability of the ROI to be rotated, specified as a numeric or logical 0 (false) or 1 (true). When the value is true, you can rotate the rectangle by clicking near the markers at the corners. When the value is false, you cannot rotate the rectangle.

Rotation angle of the ROI, specified as a number. The angle is measured in degrees in a clockwise direction around the center of the ROI.

Selection state of the ROI, specified as a numeric or logical 0 (false) or 1 (true).

Color of the ROI when selected, specified as an RGB triplet, a color name, a short color name, or "none". If you specify "none", then the value of Color defines the color of the ROI for all states, selected or not.

You can specify any color using an RGB triplet. An RGB triplet is a 3-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].

You can specify some common colors by name as a string scalar or character vector. This table lists the named color options and the equivalent RGB triplets.

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

A rectangle colored pure red

"green""g"[0 1 0]

A rectangle colored pure green

"blue""b"[0 0 1]

A rectangle colored pure blue

"cyan" "c"[0 1 1]

A rectangle colored pure cyan

"magenta""m"[1 0 1]

A rectangle colored pure magenta

"yellow""y"[1 1 0]

A rectangle colored pure yellow

"black""k"[0 0 0]

A rectangle colored black

"white""w"[1 1 1]

A rectangle colored white

Here are the RGB triplets for the default colors that MATLAB uses in many types of plots.

RGB TripletAppearance
[0 0.4470 0.7410]

A rectangle colored medium blue

[0.8500 0.3250 0.0980]

A rectangle colored reddish-orange

[0.9290 0.6940 0.1250]

A rectangle colored dark yellow

[0.4940 0.1840 0.5560]

A rectangle colored dark purple

[0.4660 0.6740 0.1880]

A rectangle colored light green

[0.3010 0.7450 0.9330]

A rectangle colored light blue

[0.6350 0.0780 0.1840]

A rectangle colored dark red

Example: "SelectedColor","r"

Example: "SelectedColor","green"

Example: "SelectedColor",[0 0.4470 0.7410]

Color of the ROI stripe, specified as an RGB triplet, a color name, a short color name, or "none". If you specify "none", then the ROI edge is a solid color specified by Color. Otherwise, the edge of the ROI is striped, with colors alternating between the colors specified by Color and StripeColor.

You can specify any color using an RGB triplet. An RGB triplet is a 3-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].

You can specify some common colors by name as a string scalar or character vector. This table lists the named color options and the equivalent RGB triplets.

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

A rectangle colored pure red

"green""g"[0 1 0]

A rectangle colored pure green

"blue""b"[0 0 1]

A rectangle colored pure blue

"cyan" "c"[0 1 1]

A rectangle colored pure cyan

"magenta""m"[1 0 1]

A rectangle colored pure magenta

"yellow""y"[1 1 0]

A rectangle colored pure yellow

"black""k"[0 0 0]

A rectangle colored black

"white""w"[1 1 1]

A rectangle colored white

Here are the RGB triplets for the default colors that MATLAB uses in many types of plots.

RGB TripletAppearance
[0 0.4470 0.7410]

A rectangle colored medium blue

[0.8500 0.3250 0.0980]

A rectangle colored reddish-orange

[0.9290 0.6940 0.1250]

A rectangle colored dark yellow

[0.4940 0.1840 0.5560]

A rectangle colored dark purple

[0.4660 0.6740 0.1880]

A rectangle colored light green

[0.3010 0.7450 0.9330]

A rectangle colored light blue

[0.6350 0.0780 0.1840]

A rectangle colored dark red

Example: "StripeColor","r"

Example: "StripeColor","green"

Example: "StripeColor",[0 0.4470 0.7410]

Tag to associate with the ROI, specified as a character vector or string scalar. Use the tag value to find the ROI object in a hierarchy of objects using the findobj function.

Data to associate with the ROI, specified as any MATLAB data. For example, you can specify a scalar, vector, matrix, cell array, string, character array, table, or structure. The drawrectangle object does not use this data.

ROI visibility, specified as "on" or "off", or as a numeric or logical 1 (true) or 0 (false). 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.

ValueDescription
"on"Display the ROI.
"off"Hide the ROI without deleting it. You still can access the properties of an invisible ROI.

Output Arguments

collapse all

Rectangular ROI, returned as a Rectangle object.

Tips

  • The ROI supports the following interactivity, including keyboard shortcuts.

    BehaviorKeyboard shortcut
    Cancel drawing the ROI.Press Esc. The function returns a valid ROI object with an empty Position property.
    Resize (reshape) the ROI.

    Position pointer over a vertex and then click and drag. The rectangle has vertices at each corner and at the midpoint of each side. To preserve the aspect ratio while resizing, press the Shift key. To lock the aspect ratio, use the Fix Aspect Ratio in the right-click context menu.

    Move the ROI.Position the pointer over the ROI. The pointer changes to the fleur shape. Click and drag the ROI.
    Delete the ROI.Position the pointer anywhere on the ROI and right-click. Select Delete Rectangle from the context menu. You can also delete the ROI using the delete function.
  • The drawrectangle function creates a Rectangle object. After you create the object, you can modify the shape, position, appearance, and behavior of the ROI by using these object capabilities.

    CapabilitySupport
    Object properties

    ROI objects have properties that specify their shape, position, appearance, and behavior. After you create the ROI object, change properties using dot notation.

    For example, to change the color of the roi to yellow, set its Color property:

    roi.Color = 'yellow'

    Object functionsROI objects have object functions that operate on the ROIs. For example, if you want to pause the MATLAB command line after creating an ROI, use the wait function.
    Event notifications

    ROI objects can notify your code when certain events occur, such as when the ROI is clicked or when the ROI is being moved. To receive event notifications, set up listeners. When the ROI notifies your application through the listener, it returns data specific to the event. For example, with the ROIMoved event, the ROI object returns its previous position and its current position. You can specify a callback function that executes when an event occurs.

    For an example of using event listeners with the Rectangle object, see Set Up Listener for Rectangle ROI Events.

Version History

Introduced in R2018b

expand all