Main Content

Create Angle Measurement Tool Using ROI Objects

Note

The impoly function used in this example is not recommended. Use the new drawpolyline function and Polyline ROI object instead. See Use Polyline to Create Angle Measurement Tool.

This example shows how to create an angle measurement tool using interactive tools and ROI objects. The example displays an image in a figure window and overlays a simple angle measurement tool over the image. When you move the lines in the angle measurement tool, the function calculates the angle formed by the two lines and displays the angle in a title.

Create a function that accepts an image as an argument and displays an angle measurement tool over the image in a figure window. This code includes a second function used as a callback function that calculates the angle and displays the angle in the figure.

function my_angle_measurement_tool(im)
% Create figure, setting up properties
figure("Name","My Angle Measurement Tool",...
      "NumberTitle","off",...
      "IntegerHandle","off")

% Display image in the axes % Display image
imshow(im)

% Get size of image.
m = size(im,1);
n = size(im,2);

% Get center point of image for initial positioning.
midy = ceil(m/2);
midx = ceil(n/2);

% Position first point vertically above the middle.
firstx = midx;
firsty = midy - ceil(m/4);
lastx = midx + ceil(n/4);
lasty = midy;

% Create a two-segment right-angle polyline centered in the image.
h = impoly(gca,[firstx,firsty;midx,midy;lastx,lasty],"Closed",false);
api = iptgetapi(h);
initial_position = api.getPosition()

% Display initial position
updateAngle(initial_position)

% set up callback to update angle in title.
api.addNewPositionCallback(@updateAngle);
fcn = makeConstrainToRectFcn("impoly",get(gca,"XLim"),get(gca,"YLim"));
api.setPositionConstraintFcn(fcn);
%

% Callback function that calculates the angle and updates the title.
% Function receives an array containing the current x,y position of
% the three vertices.
function updateAngle(p)
% Create two vectors from the vertices.
% v1 = [x1 - x2, y1 - y2]
% v2 = [x3 - x2, Y3 - y2]
v1 = [p(1,1)-p(2,1), p(1,2)-p(2,2)];
v2 = [p(3,1)-p(2,1), p(3,2)-p(2,2)];
% Find the angle.
theta = acos(dot(v1,v2)/(norm(v1)*norm(v2)));
% Convert it to degrees.
angle_degrees = (theta * (180/pi));
% Display the angle in the title of the figure.
title(sprintf("(%1.0f) degrees",angle_degrees))

Read image into the workspace.

I = imread("gantrycrane.png");

Open the angle measurement tool, specifying the image as an argument. The tool opens a figure window, displaying the image with the angle measure tool centered over the image in a right angle. Move the pointer over any of the vertices of the tool to measure any angle in the image. In the following figure, the tool is measuring an angle in the image. Note the size of the angle displayed in the title of the figure.

my_angle_measurement_tool(I);

Angle measurement tool appears as two joined line segments over an image. The title of the figure displays the current angle in degrees between the line segments.

Related Topics