Search code examples
javamacrosimagej

ImageJ, roiManager("add")


It's my first question in StackOverflow.

I have a doubt about roiManager("add") macro command. I'm trying to insert a macro content inside a plugin but im trying to understand what is adding to the roi manager. Here is the code:

run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Masks display clear record");

for (i=0; i<nResults; i++){
x = getResult('XStart', i);
    y = getResult('YStart', i);
    doWand(x,y);
    roiManager("add");
}

I dont sure if the roiManager("add") is inserting the "doWand" result or another thing.

If someone helps me I will be very grateful. Thanks.

Edit: Now I'm trying to develop the Macro with Java classes but I'm not sure how to add particles to the roi manager with the RoiManager class. I put the code here:

ij.plugin.frame.RoiManager roiManager = ij.plugin.frame.RoiManager.getInstance();
IJ.run("Convert to Mask");
IJ.run("Fill Holes");
IJ.run("Set Scale...", "distance=1 known="+pixelSize+" pixel=1 unit=um");
IJ.run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Masks display clear record");
// add the particles to the roiManager
ResultsTable rt = Analyzer.getResultsTable();
int nResults = rt.getCounter();
for (int i=0; i<nResults; i++) {
    int x = Integer.parseInt(rt.getStringValue("XStart", i));
    int y = Integer.parseInt(rt.getStringValue("YStart", i));
    int doWandResult = IJ.doWand(x,y);

    //roiManager.add(IJ.getImage(), Roi¿?, doWandResult); //¿?¿?¿?¿?¿
}

Solution

  • If you just want to add the results of Analyze Particles to the ROI Manager, use the option Add to Manager:

    run("Analyze Particles...", "add");
    

    Otherwise, you can add single ROIs as you propose, using:

    • the Macro language

    roiManager(add) adds the current selection to the ROI Manager, as if you used Edit > Selection > Add to Manager.

    In your macro, that means the selection created by doWand(x,y) is added to the ROI Manager.

    See also the macro language documentation.

    • a Java plugin:

    I recommend using the Recorder (Plugins > Macros > Record...) in Java mode to get the required code. In a plugin, you could use for example:

    IJ.run(imp, "Analyze Particles...", "add");
    

    or

    import ij.plugin.frame.RoiManager;
    
    ...
    
    RoiManager rm = RoiManager.getInstance();
    rm.addRoi(imp.getRoi());
    

    See also the RoiManager javadoc.