Posted by
William O'Connell on
Aug 24, 2006; 8:54pm
URL: http://imagej.273.s1.nabble.com/interactive-plugin-tp3701761p3701763.html
Hi Tony,
I wrote such a plugin. The code is below with some usage snippets. Be sure to check that an ROI returned by "getCompletedRoi" is non null. It will be null if the wait for user action times out at 120 seconds.
Best, William O'Connell
<snip>
// usage examples
// get ED outline
Roi edRoi = getCompletedRoi(imp, "Please outline ED region of interest...");
.
.
// get background region
Roi bgRoi = getCompletedRoi(imp, "Please outline ventricular background");
.
.
<snap>
// Roi routines
Roi getCompletedRoi(ImagePlus imp, String message){
Roi roi = null;
boolean doRoi = true, firstTime = true;
String s = "Click OK to accept ROI\nCancel to redraw ROI";
while(doRoi) {
imp.killRoi(imp); // erase any existing ROI
if(firstTime) IJ.showMessage(message);
firstTime = false;
roi = waitForRoi(imp,120); // wait for new ROI
if(roi==null) IJ.log("timed out!");
else {
IJ.log("Type is " + roi.getTypeAsString() + " state is " + roi.getState());
doRoi = !IJ.showMessageWithCancel("ROI", s);
}
} // while
return roi;
}
// Waits for an area ROI to complete before returning it to user.
// If elapsed time exceeds user specified limit, waitForRoi returns a null ROI.
Roi waitForRoi(ImagePlus imp, int limit){
int COMPLETE = 3;
int elapsed = 0, interval = 500;
int timeOut = 1000*limit;
Roi roi = null;
while((roi==null) || (!roi.isArea()) || (roi.getState()!=COMPLETE))
{
IJ.wait(interval);
roi = imp.getRoi();
elapsed += interval;
if(elapsed>timeOut) return null;
}
return roi;
}
-------------- Original message ----------------------
From: Tony <
[hidden email]>
> Hi,
>
> I want to make an interactive plugin that asks the user to draw different
> ROIs during runtime (instead of using what ever ROI was drawn with ImageJ
> before the plugin was executed)
>
> Does anybody have a method for defining a polygon ROI interactively?
> It needs to listen to the mouse, populate a vector with co-ordinates where
> the mouse is clicked, and stop when the user double clicks (or similar).
>
> Thanks