ROI tool with multiple objects (imrect, imellipse and impoly): obtaining position data for each object

3 views (last 30 days)
Hello,
I'm trying to write a small masking tool using Matlab's ROI tool. I would like to save multiple objects to crop different regions of data. I'm struggling, however, to save the position data of multiple and different objects.
For example:
fighandle =figure();
axshandle = axes('Parent',fighandle);
imshow('cameraman.tif','Parent',axshandle)
roi(1).handle = imrect(axshandle);
roi(2).handle = imellipse(axshandle);
roi(3).handle = impoly(axshandle);
for i =1:3
addNewPositionCallback(roi(i).handle,@(pos) getpos(pos,roi(i).handle,i));
end
Function to call back position of objects.
function getpos(pos, handle, i)
getPosition(handle)
end
The script works in that it calls back the position data of the three objects, even if they are moved. However, how do I save this position data to a variable (even if the objects are moved after placing them)?
Any help would be much appreciated.

Accepted Answer

Adam
Adam on 23 Sep 2016
Edited: Adam on 23 Sep 2016
Use a function instead of a script, have a variable in your main function representing the position and update this in your callback, ensuring that your callback is a nested function so that it has access to the variables in the main function.
There are other ways to do it, personally I use classes, but for an example this would be the simplest.
e.g
function roiTool( )
fighandle =figure();
axshandle = axes('Parent',fighandle);
imshow('cameraman.tif','Parent',axshandle)
roi(1).handle = imrect(axshandle);
roi(2).handle = imellipse(axshandle);
roi(3).handle = impoly(axshandle);
positions = cell( 1, 3 );
for i =1:3
addNewPositionCallback(roi(i).handle,@(pos) getpos(pos,roi(i).handle,i));
end
function getpos(pos, handle, i)
positions{i} = getPosition(handle);
end
end
I haven't tested that code so can't guarantee it works, but it gives the general idea.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!