Update the ROI with drawrectangle change

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 comentarios

xi
xi el 6 de Sept. de 2019
After you change the size/position , run 'roi=rect.Position' again. Should see the difference, at least on my computer
Mert Karakaya
Mert Karakaya el 9 de Sept. de 2019
Dear Xi,
I understand your solution, and I know it works, however I am looking for a solution in a script. I don't know how to implement a refresh on that line, when the rectangle is changed. I should have clarified that.
Thanks.
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 el 16 de Sept. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Mert Karakaya
Mert Karakaya el 16 de Sept. de 2019

2 votos

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.

Más respuestas (0)

Productos

Versión

R2019a

Preguntada:

el 6 de Sept. de 2019

Respondida:

el 16 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by