Clear Filters
Clear Filters

Question about how to use drawline to record line segment lengths in real time

3 views (last 30 days)
When drawing line on an image, I may need to adjust multiple times to get the optimal position. The drawline example code provided by Matlab can record the line segment length in real time, but I am unable to output and save this real-time length as a variable. I am looking for a way that can tell me the real-time line segment length in the command window as I adjust the line segment, and after I confirm the final position, exit drawline, the function will output the final length value and save it as a variable in the workspace.
Thanks for the help! This is part of my code
img = imread('3.png');
imshow(img);
roi = drawline('Color','r');
addlistener(roi,'MovingROI',@allevents);
addlistener(roi,'ROIMoved',@allevents);
function allevents(src,evt)
evname = evt.EventName;
switch(evname)
case {'MovingROI', 'ROIMoved'}
linePosition = src.Position;
x1 = linePosition(1, 1);
y1 = linePosition(1, 2);
x2 = linePosition(2, 1);
y2 = linePosition(2, 2);
lineLength = sqrt((x2 - x1)^2 + (y2 - y1)^2);
disp(['length of the line: ', num2str(lineLength)]);
end
end

Answers (1)

Walter Roberson
Walter Roberson on 3 Nov 2023
Use wait to wait until the user finishes with the ROI. After that you can examine roi.Position and save that to an appropriate variable or return it from the function.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!