Update the ROI with drawrectangle change

7 次查看(过去 30 天)
Hi,
I'm trying to draw a shape (rectangle, circle etc.) to determine region of interest (ROI) in an image. When I use the drawrectangle function, I can get the corners of the first instance that the rectangle is drawn, however when I edit the rectangle on the figure (change size/position), it does not update the roi variable. How can I update the roi variable when the rectangle is altered in a script?
Code I use is below and I use R20109a;
temp = snapshot(cam1);
imshow(temp);
rect = drawrectangle;
roi = rect.Position;
  4 个评论
Dheeraj Singh
Dheeraj Singh 2019-9-13
You can try running the code in the loop for the required functionality
while true
%when you receive an update
x=input()
rect = drawrectangle;
%update roi
roi = rect.Position;
end
Mert Karakaya
Mert Karakaya 2019-9-16
Thanks for your answer Mr. Singh,
Unfortunately, I couldn't get it to work. However, I have found a way that might be possible.
imshow(snapshot(camL)); %Show image
rect = drawrectangle; %Start draw rectangle
roi = rect.Position; %Get inital position
addlistener(rect,'ROIMoved',@roiChange); %Update if there is a change to the rectangle
function roi = roiChange(src,evt)
evname = evt.EventName;
if isequal(evname,'ROIMoved')
roi = evt.CurrentPosition; %Update roi
end
end
My only problem is that, I cannot get the roi output with the addlistener. I have searched the documentation for a few hours now, and I cannot figure out. How can I get the output with the addlistener?
Thanks.

请先登录,再进行评论。

采纳的回答

Mert Karakaya
Mert Karakaya 2019-9-16
For anyone who is wondering how to solve this issue; here is my solution that works;
This is the main code
imshow(snapshot(camL));
rect = drawrectangle;
roiL = rect.Position;
addlistener(rect,'ROIMoved',@(src, evt) roiChange(src,evt,'roiL'));
And the function
function roi = roiChange(src,evt,roi)
assignin('base',roi,evt.CurrentPosition);
end
Thanks for all the help.

更多回答(0 个)

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by