Update the ROI with drawrectangle change

6 views (last 30 days)
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 Comments
Dheeraj Singh
Dheeraj Singh on 13 Sep 2019
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 on 16 Sep 2019
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.

Sign in to comment.

Accepted Answer

Mert Karakaya
Mert Karakaya on 16 Sep 2019
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.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!