Question about how to use drawline to record line segment lengths in real time
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!