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

3 ビュー (過去 30 日間)
luo jingcheng
luo jingcheng 2023 年 11 月 3 日
回答済み: Walter Roberson 2023 年 11 月 3 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 11 月 3 日
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.

カテゴリ

Help Center および File ExchangeImage Segmentation and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by