Main Content

drawrectangle

创建可自定义的矩形ROI

Description

drawrectanglefunction creates aRectangle指定矩形区域(ROI)的形状和位置的对象。您可以通过使用鼠标在图像上绘制ROI或使用名称值参数来交互创建ROI。您还可以指定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, seeTips

例子

roi= drawrectanglecreates aRectangle对象并启用当前轴上ROI的交互式图。

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, seeTips

roi= drawrectangle(斧头creates the ROI in the axes specified by斧头

roi= drawrectangle(___,,,,名称,价值modifies the appearance and behavior of the ROI using one or more name-value pairs. Unspecified name-value pairs are set to the default value.

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('标签',,,,'OuterRectangle',,,,'Color',[1 0 0]);

绘制另一个矩形ROI,将绘图区域限制在第一个矩形内部的区域。

r2 = drawRectangle('标签',,,,“ innerrectangle',,,,'DrawingArea',,,,r1.Position);

Read image into the workspace and display it.

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

图包含一个轴对象。轴对象包含类型图像的对象。

使用命名参数在图像上绘制矩形ROI,以指定矩形的位置和大小。该示例还规定了矩形的边缘是条纹线。

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

图包含一个轴对象。这斧头es 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,'MoveRoi',@Pallvents);addlistener(roi,'ROIMoved',@Pallvents);

缓解callback function displays the previous position and the current position of the ROI.

function缓解((src,evt) evname = evt.EventName;switch((evname)case{'MoveRoi'} disp(['ROI moving previous position: 'mat2str(evt.PreviousPosition)]); disp([“ ROI移动当前位置”:'mat2str(evt.currentposition)]);case{'ROIMoved'} disp(['ROI moved previous position: 'mat2str(evt.PreviousPosition)]); disp(['ROI moved current position: 'mat2str(evt.currentposition)]);结尾结尾

输入参数

collapse all

Parent of ROI, specified as anAxes目的or aUIAxes目的。For information about using an ROI in a UIAxes, including important limitations, seeUsing ROIs in Apps Created with App Designer

名称值参数

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN,,,,wHereName是参数名称和Value相应的价值。名称-值参数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 encloseNamein quotes.

Example:'Color','y'creates a yellow coloredRectangle目的

矩形的纵横比,指定为非负数。纵横比定义为高度宽度

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

您可以使用RGB三胞胎指定任何颜色。RGB三重态是一个3元素行矢量,其元素指定颜色的红色,绿色和蓝色组件的强度。强度必须在[0,1]范围内。

您可以按名称指定一些常见的颜色为字符串标量或字符向量。该表列出了命名的颜色选项和等效的RGB三重态。

颜色Name Short Name RGB三胞胎 Appearance
'红色的' 'r' [1 0 0]

A rectangle colored pure red

'green' 'g' [0 1 0]

A rectangle colored pure green

'blue' 'b' [0 0 1]

矩形彩色纯蓝色

'cyan' 'c' [0 1 1]

矩形颜色的纯青色

'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

'白色的' 'W' [1 1 1]

A rectangle colored white

这是MATLAB的默认颜色的RGB三胞胎®uses in many types of plots.

RGB三胞胎 Appearance
[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]

矩形颜色的深红色

Example:“颜色”,'r'

Example:'Color','green'

Example:'Color',[0 0.4470 0.7410]

Context menu that displays when you right-click the ROI, specified as aContextMenu目的。You can create a custom context menu by using theuicontextmenufunction and then configuring context menu properties.

Context menu provides an option to delete the ROI, specified asa numeric or logical1((true)or0((错误的。当值为true,,,,you can delete the ROI interactively using the context menu. When the value is错误的,,,,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 thedelete功能。

区域的轴interactively place the ROI, specified as one of the values in this table.

Value Description
'汽车' 这drawing area is the current axes limits (default).
'unlimited' 这drawing area has no boundary and ROIs can be drawn or dragged to extend beyond the axes limits.
[x,y,w,h] 这drawing area is restricted to a rectangular region beginning at (X,,,,y),延伸到宽度wand heightH

ROI面的透明度,指定为[0,1]范围内的数字。当值为1,ROI的脸完全不透明。当值为0,,,,the ROI face is completely transparent.

ROI face can capture clicks, specified asa numeric or logical1((true)or0((错误的。什么时候true,,,,the ROI face captures mouse clicks. When错误的,ROI脸不会捕获鼠标点击。

纵横比在交互期间保持恒定,指定为a numeric or logical0((错误的)or1((true。当值为true,,,,the aspect ratio remains constant when you draw or resize the ROI. When the value is错误的,您可以在绘制或调整ROI大小时更改纵横比。

ROI手柄的可见性孩子们父的属性,指定为该表中的值之一。

Value Description
'上' 这目的Handle is always visible (default).
'off' 这目的Handle is hidden at all times.
'打回来' 这目的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.

Value Description
'全部' 完全interactable ROI。
'没有任何' ROI不可交互,也看不到阻力点。
'翻译' 这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 to1,文字背景完全不透明。设置为0,,,,the text background is completely transparent.

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

您可以使用RGB三胞胎指定任何颜色。RGB三重态是一个3元素行矢量,其元素指定颜色的红色,绿色和蓝色组件的强度。强度必须在[0,1]范围内。

您可以按名称指定一些常见的颜色为字符串标量或字符向量。该表列出了命名的颜色选项和等效的RGB三重态。

颜色Name Short Name RGB三胞胎 Appearance
'红色的' 'r' [1 0 0]

A rectangle colored pure red

'green' 'g' [0 1 0]

A rectangle colored pure green

'blue' 'b' [0 0 1]

矩形彩色纯蓝色

'cyan' 'c' [0 1 1]

矩形颜色的纯青色

'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

'白色的' 'W' [1 1 1]

A rectangle colored white

这是MATLAB的默认颜色的RGB三胞胎uses in many types of plots.

RGB三胞胎 Appearance
[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]

矩形颜色的深红色

Example:“ LabelTextColor','r'

Example:“ LabelTextColor”,“绿色”

Example:“ LabelTextColor”,[0 0.4470 0.7410]

ROI标签的可见性,指定为这些值之一。

Value Description
'上' Label is visible when the ROI is visible, and theLabel属性是非空的(默认)。
'hover'

仅当小鼠徘徊在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父母,,,,specified as anAxesorUIAxes目的。For information about using an ROI in a UIAxes, including important limitations, seeUsing ROIs in Apps Created with App Designer

ROI的位置,指定为表格的1 x-4数字向量[Xmin,,,,ymin,,,,width,,,,Height].Xminandyminspecify the coordinates of the upper left corner of the rectangle.widthandHeightspecify the width and height of the rectangle and must be nonnegative.

Ability of the ROI to be rotated, specified asa numeric or logical0((错误的)or1((true。当值为true,,,,you can rotate the rectangle by clicking near the markers at the corners. When the value is错误的,您不能旋转矩形。

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 asa numeric or logical0((错误的)or1((true

颜色of the ROI when selected, specified as an RGB triplet, a color name, a short color name, or'没有任何'。If you specify'没有任何',然后的价值颜色定义所有状态的ROI的颜色,无论是否选择。

您可以使用RGB三胞胎指定任何颜色。RGB三重态是一个3元素行矢量,其元素指定颜色的红色,绿色和蓝色组件的强度。强度必须在[0,1]范围内。

您可以按名称指定一些常见的颜色为字符串标量或字符向量。该表列出了命名的颜色选项和等效的RGB三重态。

颜色Name Short Name RGB三胞胎 Appearance
'红色的' 'r' [1 0 0]

A rectangle colored pure red

'green' 'g' [0 1 0]

A rectangle colored pure green

'blue' 'b' [0 0 1]

矩形彩色纯蓝色

'cyan' 'c' [0 1 1]

矩形颜色的纯青色

'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

'白色的' 'W' [1 1 1]

A rectangle colored white

这是MATLAB的默认颜色的RGB三胞胎uses in many types of plots.

RGB三胞胎 Appearance
[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]

矩形颜色的深红色

Example:'SelectedColor','r'

Example:'SelectedColor','green'

Example:“ Selected Color”,[0 0.4470 0.7410]

颜色of the ROI stripe, specified as an RGB triplet, a color name, a short color name, or'没有任何'。If you specify'没有任何',然后ROI边缘是由固体颜色指定的颜色。Otherwise, the edge of the ROI is striped, with colors alternating between the colors specified by颜色and脱衣舞

您可以使用RGB三胞胎指定任何颜色。RGB三重态是一个3元素行矢量,其元素指定颜色的红色,绿色和蓝色组件的强度。强度必须在[0,1]范围内。

您可以按名称指定一些常见的颜色为字符串标量或字符向量。该表列出了命名的颜色选项和等效的RGB三重态。

颜色Name Short Name RGB三胞胎 Appearance
'红色的' 'r' [1 0 0]

A rectangle colored pure red

'green' 'g' [0 1 0]

A rectangle colored pure green

'blue' 'b' [0 0 1]

矩形彩色纯蓝色

'cyan' 'c' [0 1 1]

矩形颜色的纯青色

'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

'白色的' 'W' [1 1 1]

A rectangle colored white

这是MATLAB的默认颜色的RGB三胞胎uses in many types of plots.

RGB三胞胎 Appearance
[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]

矩形颜色的深红色

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 thefindobj功能。

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. Thedrawrectangle目的does not use this data.

ROI可见性,指定为'上'or'off',或作为数字或逻辑1((true)or0((错误的)。A value of'上'is equivalent to true, and'off'is equivalent to false. The value is stored as an on/off logical value of typeOnOffSwitchState

Value Description
'上' 显示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 aRectangle目的。

Tips

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

    Behavior Keyboard shortcut
    Cancel drawing the ROI. Esc。这function returns a valid ROI object with an empty位置财产。
    Resize (reshape) the ROI.

    将指针定位在顶点上,然后单击并拖动。矩形在每个角和每一侧的中点都有顶点。为了在调整大小时保持纵横比,请按转移key. To lock the aspect ratio, use the修复长宽比in the right-click context menu.

    Move the ROI. 位置the pointer over the ROI. The pointer changes to the fleur shape. Click and drag the ROI.
    Delete the ROI. 位置the pointer anywhere on the ROI and right-click. SelectDelete Rectangle从上下文菜单。您也可以使用delete功能。
  • drawrectanglefunction creates aRectangle目的。After you create the object, you can modify the shape, position, appearance, and behavior of the ROI by using these object capabilities.

    能力 万博1manbetx
    Object properties

    ROI对象具有指定其形状,位置,外观和行为的属性。创建ROI对象后,使用点表示法更改属性。

    For example, to change the color of theroito yellow, set its颜色property:

    ROI.Color ='yellow'

    Object functions ROI 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 thewait功能。
    事件通知

    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 uplisteners。什么时候the ROI notifies your application through the listener, it returns data specific to the event. For example, with theROIMovedevent, 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 theRectangle目的,,,,seeSet Up Listener for Rectangle ROI Events

版本历史记录

在R2018B中引入

expand all

Not recommended starting in R2020a