Crosshair region of interest
Crosshair object is a crosshair region-of-interest
To draw the ROI, position the cursor over the image and click the mouse to draw the ROI. For more information about using this ROI, including keyboard shortcuts, see Tips.
To create a
Crosshair object, use the
images.roi.Crosshair creation function. After creating the object, call
function to begin drawing the ROI. As a convenience, you can also use the
function to create a
Crosshair object. The
function creates the ROI object and automatically calls the
roi = images.roi.Crosshair
Crosshair object with default properties.
creates the ROI in the axes specified by
roi = images.roi.Crosshair(
sets properties using
one or more name-value pairs. For example,
roi = images.roi.Crosshair(___,
images.roi.Crosshair('Color','y') sets the color of the crosshair ROI
to yellow. Enclose each property name in quotes.
Color— ROI color
ROI color, specified as a MATLAB®
[0 1 0]
ContextMenu— Context menu
Context menu, specified as a
ContextMenu object. Use this property to
display a custom context menu when you right-click the ROI. You can create a custom
context menu by using the
uicontextmenu function and then
configuring context menu properties.
Deletable— ROI can be interactively deleted via context menu
ROI can be interactively deleted via a context menu, specified as
false. When the value is
true (default), you
can delete the ROI via the context menu. To disable this context menu item, set this
false. When the value is set to
you can still delete the ROI by calling the
specifying the handle to the ROI as the input.
DrawingArea— Area of axes in which you can interactively place ROI
Area of the axes in which you can interactively place the ROI, specified as one of the values in this table.
|The drawing area is the current axes limits (default).|
|The drawing area has no boundary and ROIs can be drawn or dragged to extend beyond the axes limits.|
|The drawing area is restricted to a rectangular region beginning at (x,y), and extending to width w and height h.|
HandleVisibility— Visibility of ROI handle in
Childrenproperty of parent
Visibility of the ROI handle in the
Children property of the parent,
specified as one of the values in this table.
|The object handle is always visible (default).|
|The object handle is hidden at all times.|
|The object handle is visible from within callbacks or functions invoked by callbacks, but not from within functions invoked from the command line.|
InteractionsAllowed— Interactivity of ROI
Interactivity of the ROI, specified as one of the values in this table.
|The ROI is fully interactable (default).|
|The ROI is not interactable, and no drag points are visible.|
Label— ROI label
''(default) | character vector | string
ROI label, specified as a character vector or string. By default, the ROI has no label
LabelVisible— Visibility of ROI label
Visibility of the ROI label, specified as one of these values:
|Label is visible when the ROI is visible and the |
|Label is visible only when the mouse hovers over the ROI.|
|Label is not visible.|
LineWidth— Width of ROI edge
Width of the ROI edge, specified as a positive numeric scalar in points. The default value is three times the number of points per screen pixel.
Parent— ROI parent
ROI parent, specified as an
UIAxes object. For
information about using an ROI in a UIAxes, including important limitations,
see Using ROIs in Apps Created with App Designer.
Position— Position of ROI
Position of the ROI, specified as a 1-by-2 numeric array of the form
y]. The values
y specify the
x- and y-coordinates of the location where the
horizontal line crosses the vertical line in the crosshair ROI. This value changes
automatically when you draw or move the ROI.
Selected— Selection state of ROI
Selection state of the ROI, specified as the logical value
false. You can also set this property interactively. For example,
clicking on the ROI selects the ROI and sets this property to
Similarly, pressing the Ctrl key and clicking the ROI deselects the ROI
and sets the value of this property to
SelectedColor— Color of ROI when selected
Color of the ROI when selected, specified as a MATLAB
ColorSpec (Color Specification). The
intensities must be in the range
'SelectedColor' is set to
'none', then the
Color defines the color of the ROI for all states,
selected or not.
[0 1 0]
StripeColor— Color of ROI stripe
Color of the ROI stripe, specified as a MATLAB
ColorSpec (Color Specification) value.
By default, the edge of an ROI is solid colored. If you specify
StripeColor, the ROI edge is striped. The striping consists of a
combination of the value specified by
'Color' and this
[0 1 0]
Tag— Tag to associate with the ROI
''(default) | character vector | string scalar
Tag to associate with the ROI, specified as a character vector or string. Use the tag value to
find the ROI object in a hierarchy of objects using the
UserData— Data to associate with ROI
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. MATLAB does not use this data.
Visible— ROI visibility
'off'| on/off logical value
ROI visibility, specified as
'off', or as
numeric or logical
1 (true) or
0 (false). A value
'on' is equivalent to true, and
equivalent to false. Thus, you can use the value of this property as a logical value.
The value is stored as an on/off logical value of type
|Display the ROI (default).|
|Hide the ROI without deleting it. You still can access the properties of an invisible ROI.|
Read an image into the workspace and display it.
I = imread('baby.jpg'); figure; imshow(I)
Place a crosshair ROI on the image programmatically. When you specify the position of the ROI, you must specify the axes.
h = images.roi.Crosshair(gca,'Position',[100,100]);
Read an image into the workspace.
img = imread('coins.png');
Display the image in a figure.
hAx = gca; imObj = imshow(img,'Parent',hAx); imObj.Parent.Visible = 'on';
Create a crosshair ROI on the image.
h = images.roi.Crosshair('Parent',hAx,'Position',[50 50],'LineWidth',1,'Color','y');
Set up a listener to get notification when the ROI moves over the image. Because the code displays the value of the pixel under the crosshair, you must pass the image as an argument to the listener.
displayInfo function called by the listener when a
'MovingROI' event occurs.
function displayInfo(~,data,hAx,img) pos = ceil(data.CurrentPosition); title(hAx,['Pixel Value: ',num2str(img(pos(2),pos(1)))]) end
Appearance of the image with title during interactive movement of the Crosshair ROI.
To receive notification from the ROI when certain events happen, set up listeners for these events. You can specify a callback function that executes when one of these events occurs. When the ROI notifies your application through the listener, it returns data specific to the event. Look at the event class for the specific event to see what is returned.
For an example of using event listeners, see Create Crosshair Tool to Check Pixel Values.
|Event Name||Trigger||Event Data||Event Attributes|
|ROI is about to be interactively deleted.|
|ROI is about to be interactively drawn.|
|ROI has been interactively drawn.|
|ROI shape or location is being interactively changed.|
|ROI shape or location has been interactively changed.|
|ROI has been clicked.|
This table describes how to perform common tasks with the
|Cancel drawing operation.||Press Esc. The function returns a valid ROI object with an
|Move the ROI.||Position the cursor over the center of the crosshair ROI (where the two lines cross) and click and drag the crosshair. Another way to move the crosshair ROI is to position the cursor anywhere on one of the two lines and click. The other line in the crosshair jumps to the new crosshair center position.|
|Delete the ROI.||Position the cursor over the ROI, right-click, and then choose
Delete Crosshair from the context menu. You can also
delete the ROI programmatically by using the |
For information about using an ROI in an app created with App Designer, see Using ROIs in Apps Created with App Designer.
UIContextMenuproperty is not recommended
Not recommended starting in R2020a
Starting in R2020a, using the
UIContextMenu property to assign a
context menu to a graphics object or UI component is not recommended. Use the
ContextMenu property instead. The property values are the
There are no plans to remove support for the
property at this time. However, the
UIContextMenu property no
longer appears in the list returned by calling the
get function on a
graphics object or UI component.