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")