Create draggable freehand region
imfreehand object encapsulates an interactive freehand
region over an image.
You can add vertices and adjust the size and position of the polygon by using the mouse. The polygon also has a context menu that controls aspects of its appearance and behavior. For more information, see Usage.
h = imfreehand
h = imfreehand(hparent)
h = imfreehand(___,Name,Value)
h = imfreehand begins interactive placement of a freehand
region on the current axes, and returns an
h = imfreehand( begins
interactive placement of a freehand region on the object specified by
h = imfreehand(___,
specifies name-value pairs that control the behavior of the freehand
hparent— Handle to parent object
Handle to parent object, specified as a handle. The parent is
typically an axes object, but can also be any other object that can be
the parent of an
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
'PositionConstraintFcn'— Position constraint function
Position constraint function, specified as the comma-separated
pair consisting of
'PositionConstraintFcn' and a
fcn is called whenever the mouse
is dragged. You can use this function to control where the freehand
region can be dragged. See the help for the
setPositionConstraintFcn function for information
about valid function handles.
'Closed'— Freehand region is closed
Freehand region is closed, specified as the comma-separated pair
false. When set to
true (the default),
imfreehand draws a straight line to connect
the endpoints of the freehand line to create a closed region. If set
the region open.
Deletable— ROI can be deleted
ROI can be deleted, specified as
When you call
imfreehand with an interactive syntax, the pointer
changes to a cross hairs when positioned over an image. Click and drag the
mouse to draw the freehand region and adjust the position of the region. By default,
imfreehand draws a straight line connecting the last point you
drew with the first point, but you can control this behavior using the
The freehand region also supports a context menu that you can use to control aspects of its appearance and behavior.
The table lists the interactive features supported by
|Moving the region.||Move the pointer inside the freehand region. The pointer changes to a fleur shape . Click and hold the left mouse button to move the region.|
|Changing the color used to draw the region.||Move the pointer inside the freehand region. Right-click and select Set Color from the context menu.|
|Retrieving the current position of the freehand region.||Move the pointer inside the freehand region. Right-click and select
Copy Position from the context menu.
|Deleting the region||Move the pointer inside the region. Right-click and select
Delete from the context menu. To remove
this option from the context menu, set the |
imfreehand object supports a number of methods. Type
methods imfreehand to see a complete list.
|Add new-position callback to ROI object|
|Create mask within image|
|Delete handle object|
|Get color used to draw ROI object|
|Return current position of ROI object|
|Return function handle to current position constraint function|
|Remove new-position callback from ROI object|
|Resume execution of MATLAB command line|
|Set closure behavior of ROI object|
|Set color used to draw ROI object|
|Set ROI object to new position|
|Set position constraint function of ROI object|
|Block MATLAB command line until ROI creation is finished|
Interactively place a closed freehand region of interest by clicking and dragging over an image.
imshow('pout.tif') h = imfreehand;
Interactively move the freehand region by clicking and dragging. Use the
wait function to
block the MATLAB® command line. Double-click on the freehand region to resume execution
of the MATLAB command line.
position = wait(h);
If you use
imfreehand with an axes that contains an image
object, and do not specify a position constraint function, users can drag the
freehand region outside the extent of the image and lose the freehand region.
When used with an axes created by the
plot function, the axes
limits automatically expand to accommodate the movement of the freehand
To cancel the interactive placement, press the Esc key.
imfreehand returns an empty object.