Main Content

imline

(Not recommended) Create draggable, resizable line

imline is not recommended. Use the new Line ROI object instead. You can also use the ROI creation convenience function drawline. For more information, see Version History.

Description

An imline object encapsulates an interactive line over an image.

You can adjust the size and position of the line by using the mouse. The line also has a context menu that controls aspects of its appearance and behavior. For more information, see Usage.

Creation

Description

h = imline begins interactive placement of a line on the current axes, and returns an imline object.

example

h = imline(hparent) begins interactive placement of a line on the object specified by hparent.

h = imline(hparent,position) creates a draggable, resizeable line, with coordinates defined by position.

example

h = imline(hparent,x,y) creates a draggable, resizeable line, with x- and y-coordinates of the endpoints defined by x and y.

h = imline(___,"PositionConstraintFcn",fcn) also specifies where the line can be dragged using a position constraint function, fcn.

Input Arguments

expand all

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 hggroup object.

Position of line endpoints, specified as a 2-by-2 array of the form [x1 y1; x2 y2].

x-coordinates of line endpoints, specified as a 2-element vector of the form x = [x1 x2].

y-coordinates of line endpoints, specified as a 2-element vector of the form y = [y1 y2].

Position constraint function, specified as a function handle. fcn is called whenever the mouse is dragged. You can use this function to control where the ellipse can be dragged. See the help for the setPositionConstraintFcn function for information about valid function handles.

Properties

expand all

ROI can be deleted, specified as true or false.

Data Types: logical

Usage

When you call imline with an interactive syntax, the pointer changes to a cross hairs when over the image. Click and drag the mouse to specify the position and length of the line. The line supports a context menu that you can use to control aspects of its appearance and behavior.

Blue line displayed over an image, with a context menu that gives options to copy the position, set the color, or delete the line.

The table describes the interactive behavior supported by imline.

Interactive BehaviorDescription
Moving the line.Move the pointer over the line. The pointer changes to a fleur shape . Click and drag the mouse to move the line.
Moving the endpoints of the line.Move the pointer over either end of the line. The pointer changes to the pointing finger, . Click and drag the mouse to resize the line.
Changing the color used to display the line. Move the pointer over the line. Right-click and select Set Color from the context menu.
Retrieving the coordinates of the endpoints of the line.Move the pointer over the line. Right-click and select Copy Position from the context menu. imline copies a 2-by-2 array to the clipboard specifying the coordinates of the endpoints of the line in the form [X1 Y1; X2 Y2].
Deleting the lineMove the pointer on top of the line. Right-click and select Delete from the context menu. To remove this option from the context menu, set the Deletable property to false: h = imline(); h.Deletable = false;

Object Functions

Each imline object supports a number of functions. Type methods imline to see a complete list.

addNewPositionCallbackAdd new-position callback to ROI object
createMask(Not recommended) Create mask within image
deleteDelete handle object
getColorGet color used to draw ROI object
getPositionReturn current position of ROI object
getPositionConstraintFcnReturn function handle to current position constraint function
removeNewPositionCallbackRemove new-position callback from ROI object
resume(Not recommended) Resume execution of MATLAB command line
setColor(Not recommended) Set color used to draw ROI object
setConstrainedPositionSet ROI object to new position
setPosition(Not recommended) Move ROI object to new position
setPositionConstraintFcnSet position constraint function of ROI object
wait(Not recommended) Block MATLAB command line until ROI creation is finished

Examples

collapse all

Display a line in a custom color.

imshow("pout.tif")
h = imline(gca,[10 100],[100 100]);
setColor(h,[0 1 0]);

Use the addNewPositionCallback function. Move the line, note that the 2-by-2 position vector of the line is displayed in the title above the image. Explore the context menu of the line by right clicking on the line.

id = addNewPositionCallback(h,@(pos) title(mat2str(pos,3)));

After observing the callback behavior, remove the callback using the removeNewPositionCallback function.

removeNewPositionCallback(h,id);

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

imshow("pout.tif")
h = imline;
position = wait(h);

Tips

  • If you use imline with an axes that contains an image object, and do not specify a position constraint function, users can drag the line outside the extent of the image and lose the line. When used with an axes created by the plot function, the axis limits automatically expand to accommodate the movement of the line.

  • Use imdistline to create an interactive line with a text box that displays the distance between line endpoints.

Version History

Introduced before R2006a

collapse all

R2018b: imline is not recommended

Starting in R2018b, a new set of ROI objects replaces the existing set of ROI objects. The new objects provide more functional capabilities, such as face color transparency. The new classes also support events that you can use to respond to changes in your ROI such as moving or being clicked. Although there are no plans to remove the old ROI objects at this time, switch to the new ROIs to take advantage of the additional capabilities and flexibility. For more information on creating ROIs using the new ROI functions, see Create ROI Shapes.

Replace use of the imline ROI object with the new Line ROI object. You can also use the ROI creation convenience function drawline.

Update ROI Creation Code

Update all instances of imline.

Discouraged UsageRecommended Replacement

This example creates a line ROI.

imshow("cameraman.tif");
h = imline(gca,[10 10; 100 150]);

Here is equivalent code, replacing the old ROI with the equivalent new ROI object. This example uses the ROI creation convenience function. Note that you must specify the size and position information as name-value arguments.

imshow("cameraman.tif");
h = drawline(gca,"Position",[10 10; 100 150]);
Other ROI Code Updates

Update instances where your code uses one of the object functions of imline. In many cases, you replace the call to an object function by simply accessing or setting the value of a Line ROI object property. For example, replace calls to getColor or setColor with use of the Color property. In some cases, you must replace the imline object function with an object function of the new Line ROI. Each of the individual imline ROI object functions include information about migrating to the new Line ROI object. For a migration overview, see ROI Migration.