imfreehand

Create draggable freehand region

Syntax

h = imfreehand
h = imfreehand(hparent)
h = imfreehand(...,param1, val1,...)

Description

h = imfreehand begins interactive placement of a freehand region of interest on the current axes. The function returns h, a handle to an imfreehand object. A freehand region of interest can be dragged interactively using the mouse and supports a context menu that controls aspects of its appearance and behavior. See Interactive Behavior.

h = imfreehand(hparent) begins interactive placement of a freehand region of interest on the object specified by hparent. hparent specifies the HG parent of the freehand region graphics, which is typically an axes, but can also be any other object that can be the parent of an hggroup.

h = imfreehand(...,param1, val1,...) creates a freehand ROI, specifying parameters and corresponding values that control the behavior of the tool. The following table lists the parameters available. Parameter names can be abbreviated, and case does not matter.

ParameterDescription
'Closed'Scalar logical that controls whether the freehand region is closed. 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 to false, imfreehand leaves the region open.
'PositionConstraintFcn'

Function handle specifying the function that is called whenever the freehand region is dragged using the mouse. Use this parameter to control where the freehand region can be dragged. See the help for the setPositionConstraintFcn method for information about valid function handles.

Interactive Behavior

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. 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 'Closed' parameter. The following figure illustrates a freehand region with its context menu.

The following table lists the interactive features supported by imfreehand.

Interactive BehaviorDescription
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. imfreehand copies an n-by-2 array of coordinates on the boundary of the ROI to the clipboard..
Deleting the regionMove the pointer inside the region. Right-click and select Delete from the context menu. To remove this option from the context menu, set the Deletable property to false: h = imfreehand(); h.Deletable = false;

Methods

The imfreehand object supports the following methods. Type methods imfreehand to see a complete list of all methods.

addNewPositionCallback — Add new-position callback to ROI object

See imroi for information.

createMask — Create mask within image

See imroi for information.

delete — Delete ROI object

See imroi for information.

getColor — Get color used to draw ROI object

See imroi for information.

getPosition — Return current position of freehand region

pos = getPosition(h) returns the current position of the freehand region h. The returned position, pos, is an N-by-2 array [X1 Y1;...;XN YN].

getPositionConstraintFcn — Return function handle to current position constraint function

See imroi for information.

removeNewPositionCallback — Remove new-position callback from ROI object.

See imroi for information.

resume — Resume execution of MATLAB command line

See imroi for information.

setClosed — Set geometry of freehand region

setClosed(h,TF) sets the geometry of the freehand region h. TF is a logical scalar. True means that the freehand region is closed. False means that the freehand region is open.

setColor — Set color used to draw ROI object

See imroi for information.

setConstrainedPosition — Set ROI object to new position

See imroi for information.

setPositionConstraintFcn — Set position constraint function of ROI object.

See imroi for information.

wait — Block MATLAB command line until ROI creation is finished

See imroi for information.

Examples

Interactively place a closed freehand region of interest by clicking and dragging over an image.

figure, imshow('pout.tif');
h = imfreehand(gca);    

Interactively place a freehand region by clicking and dragging. Use the wait method to block the MATLAB command line. Double-click on the freehand region to resume execution of the MATLAB command line.

figure, imshow('pout.tif');
h = imfreehand;
position = wait(h); 

More About

expand all

Tips

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 region.

Was this topic helpful?