Scripting the ROI manager

Hi all, the script below creates an ROI and adds it to the ROI manager with the appropriate name. This approach feels rather cumbersome, is there a more succinct method? I am considering defining a custom wrapper class instead, is that the approach people typically take?
Cheers

File:manager.py

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

imp = IJ.getImage()
rm = RoiManager()

roi = OvalRoi(75, 75, 50, 50); # define and add ROI
imp.setRoi(roi) # make active on image
rm.addRoi(roi) # add
rm.select(0) # select the zeroth ROI and rename it
rm.runCommand(“Rename”, “roi”);

Unfortunately, I think that’s how you have to do it. If you look at the RoiManager source code, the rename method (called internally by runCommand) is package private, so you cannot call it directly. And the name is not tied to the Roi object itself, but in fact stored in the RoiManager instance’s DefaultListModel. I don’t see a nicer way to do it. Just write a utility method that you can call to encapsulate these operations.

You can set the name of the ROI before adding it to the ROI Manager, as in this example:

imp = IJ.getImage()
rm = RoiManager()
roi = new OvalRoi(75, 75, 50, 50);
roi.setName(“roi”);
rm.addRoi(roi)

3 Likes