See examples in the help for imdistline
help imdistline
IMDISTLINE Draggable Distance tool.
H = IMDISTLINE creates a draggable distance tool on the current
axes. The function returns H, a handle to an imdistline object.
H = IMDISTLINE(HPARENT) creates a draggable distance tool on the object
specified by HPARENT. HPARENT specifies the HG parent of the imdistline
graphics, which is typically an axes but can also be any other object
that can be the parent of an hggroup.
H = IMDISTLINE(...,X,Y) creates a draggable distance tool with endpoints
located at the locations specified by the vectors X and Y. X and Y specify
the initial endpoint positions of the draggable distance tool in the form
X = [X1 X2], Y =[Y1 Y2].
The draggable distance tool has a context menu associated with it that
allows you to:
Export endpoint and distance data to the workspace
Toggle the distance label on/off
Set the line color
Specify horizontal and vertical drag constraints
Delete the distance tool object
Remarks
-------
If you use IMDISTLINE with an axis 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
axis created by the PLOT function, the axis limits automatically expand to
accommodate the movement of the line.
To understand how IMDISTLINE calculates the angle returned by
getAngleToHorizontal, draw an imaginary horizontal vector from the bottom
endpoint of the distance line, extending to the right. The value returned
by getAngleToHorizontal is the angle from this horizontal vector to the
distance line, which can range from 0 to 180 degrees.
Example 1
---------
% Insert a distance tool into an image. Use makeConstrainToRectFcn to specify
% a position constraint function that prevents distance tool from being dragged
% outside the extent of the image. Explore the context menu options of the
% distance tool by right clicking on the distance tool.
figure, imshow('pout.tif');
h = imdistline(gca);
fcn = makeConstrainToRectFcn('imline',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(h,fcn);
Example 2
---------
% Use distance tool with XData and YData of associated image in non-pixel
% units.
start_row = 1;
end_row = 1350;
meters_per_pixel = 1;
start_col = 731;
end_col = 2955;
I = imread('concordorthophoto.png');
I1 = I(start_row:meters_per_pixel:end_row, start_col:meters_per_pixel:end_col);
figure;
imshow(I1);
title('1 meter per pixel');
% Specify initial position of distance tool on image.
h1 = imdistline(gca,[4 2218],[40 781]);
setLabelTextFormatter(h1,'%02.0f meters');
% Repeat process but work with a 2 meter per pixel sampled image. Verify
% that the same distance is obtained.
meters_per_pixel = 2;
I = imread('concordorthophoto.png');
I1 = I(start_row:meters_per_pixel:end_row, start_col:meters_per_pixel:end_col);
figure;
hImg = imshow(I1);
title('2 meters per pixel');
% Convert XData and YData to meters using conversion factor.
XDataInMeters = get(hImg,'XData')*meters_per_pixel;
YDataInMeters = get(hImg,'YData')*meters_per_pixel;
% Set XData and YData of image to reflect desired units.
set(hImg,'XData',XDataInMeters,'YData',YDataInMeters);
set(gca,'XLim',XDataInMeters,'YLim',YDataInMeters);
% Specify initial position of distance tool on image.
h2 = imdistline(gca,[4 2218],[40 781]);
setLabelTextFormatter(h2,'%02.0f meters');
See also makeConstrainToRectFcn.
Documentation for imdistline
doc imdistline