ROI Manager in Jython: saving selected ROIs

I’d like to save a subset of the ROIs – specified as an array – in the ROI Manager.

I’m trying to use Jython for this, because for the larger project, I also need to generate sets of unique random numbers, which seems to be cumbersome in the macro language.

(Background: I want to load a slide scanner image into the VSI Reader, use the Grid ROI function to generate a set of ROIs covering the tissue on the slide, and then randomly select fields among these to extract and analyze.)

My problem is that when I select a subset of ROIs using rm.setSelectedIndexes() then run “Save”, it ends up saving all the ROIs instead of the subset. But the same idea works fine in the GUI or in a macro.

An attempt at a minimal example script is below. The idea is to create 3 ROIs, then select the 1st and 3rd and save only those in an RoiSet zip file. Then I want to re-open that set of 2 ROIs. But when I run it I get all 3 ROIs back.

Thanks in advance for any suggestions!

# roiMgr_.py
# goal: save a set of selected ROIs

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import Roi

# open blobs and show it

imp = IJ.openImage("http://imagej.nih.gov/ij/images/blobs.gif");
imp.show()

# make 3 ROIs and add to manager

rm = RoiManager.getInstance()
if not rm:
  rm = RoiManager()
rm.reset()

roi1 = Roi(10, 70, 20, 15)
rm.addRoi(roi1)
roi2 = Roi(50, 70, 20, 15)
rm.addRoi(roi2)
roi3 = Roi(90, 70, 20, 15)
rm.addRoi(roi3)

imp.show()
rm.runCommand(imp, "Show All")

# define a set of ROIs as an array

from array import array
aROIs = array('i', [0,2])
# print aROIs

# select the ROIs listed in the array, and save them

rm.setSelectedIndexes(aROIs)
selRois = rm.getSelectedIndexes()
# print selRois, " are selected"

# get path for temp file

import os
from ij.io import DirectoryChooser

dc = DirectoryChooser("Pick folder for saving ROI set")
folder = dc.getDirectory()
rm.runCommand("Save", os.path.join(folder, "temp.zip"))

# reset ROI mgr and open the saved set

rm.reset()
rm.runCommand("Open", os.path.join(folder, "temp.zip"))
rm.runCommand(imp, "Show All")
3 Likes

@tswayne I get the same result with a IJ macro, too.

However, if I save it with the GUI of the ROI Manager everything works fine.

If I record the GUI save action with the Recorder and execute the command all ROI’s are saved as described by @tswayne.

@Wayne is this macro behaviour intended or a bug?

1 Like

Thanks, @Bio7!

The IJ1 macro below works as desired. So for me the issue is only with jython. But as I mentioned I hope to use jython because of the ease of generating unique random numbers. I’m open to other ideas though!

// roiMgr_.ijm
// goal: save a subset of ROIs provided as an array

// open blobs

run("Blobs (25K)");

// make some ROIs

makeRectangle(10, 70, 20, 15);
roiManager("Add");
makeRectangle(50, 70, 20, 15);
roiManager("Add");
makeRectangle(90, 70, 20, 15);
roiManager("Add");

// select the desired fields and save in a placeholder file

path = getDirectory("Select directory to save ROI temp file");
fields = newArray(0, 2);

roiManager("select", fields);
roiManager("save selected", path+"/temp.zip");

// clear out the old ROIs

roiManager("reset");

// Re-load the saved set which should include the first and third ROIs

roiManager("open", path+"/temp.zip");
roiManager("show all");
2 Likes

What happens if you replace this in your above Python script by the following?

rm.runCommand("save selected", os.path.join(folder, "temp.zip"))

See here in the source code:

5 Likes

That was it, @imagejan – your code works! Thank you so much!

1 Like