Create Mask from roi manager selection in a Stack

Hi to all,
I am trying to write a function in jython (but ok, the language it is not a problem) that starting from a stack with roi assigned to slices (by setting the roi position) creates a stack where each slice is the resulting mask of the rois present at that specific slice.

Essentially, I would like to use the command this commands for each slice:

rm.runCommand(imp,"Combine");
IJ.run("Create Mask", "");

So my questions are:

  1. is there a function/command/plugin that already does that?
  2. if not, how can I check smartly which are the rois present in specific slice?

by now I’ve only found this:

rm.runCommand(imp,"List");

that gives me a list where the rois positions are listed and I could use it… but I think that there is a more elegant way to do that.

Thank you very much,
Emanuele Martini

You can achieve this by

  1. creating a new empty (i.e. black) 8-bit stack with the same dimensions as your original image (File > New > Hyperstack),
  2. setting the foreground color to white,
  3. deselecting all ROIs in the ROI manager, and
  4. running More > Fill in the ROI manager to fill each ROI (in its respective z position) in the new stack.

Here’s a python script that does this (using the SciJava parameter annotation to get the current image):

# @ImagePlus imp

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

def run():
    rm = RoiManager.getInstance()
    if not rm:
        print "Please first add some ROIs to the ROI Manager"
        return
    impMask = IJ.createImage("Mask", "8-bit grayscale-mode", imp.getWidth(), imp.getHeight(), imp.getNChannels(), imp.getNSlices(), imp.getNFrames())
    IJ.setForegroundColor(255, 255, 255)
    rm.runCommand(impMask,"Deselect")
    rm.runCommand(impMask,"Fill")
    impMask.show()

run()
3 Likes

thank you @imagejan, the command was “Fill”.
I implented it using
rm.runCommand(“Set Fill Color”, “white”);
since that way I set the fill color only to a specific roimanager.

Thank you,
have a nice day
Emanuele

2 Likes