Clear Selection implementation in Jython

Dear Forum,
how do you implement in Jython the “Selection->Clear…” command?

I have a long list of ROIs in a quite big multiple z multiple times acquisition I want to clear inside each of the ROIs without showing the image in order to be faster.

By now I have implemented a simple:

rm = RoiManager.getInstance()
for i in range(0,rm.getCount()):, "Clear", "slice");        

It of course works but I cannot use it without and that it’s really time consuming.

For this reason, I searched for the API of the Clear Function and I think that it is this one:

I implemented this code:

FIL = Filler();
for roi in rm.getRoisAsArray():
    ip = imp.getProcessor();

but that way it clears inside the bounding box of the roi and not inside the boundaries of the roi.

What is the correct implementation of Clear command in jython?

Thank you
Emanuele Martini

The “Clear” command fills the ROI with the background color. So just do that:

rm = RoiManager.getInstance()
value = 0 # black

for i in xrange(0, rm.getCount()):
  ip = imp.getProcessor()
  ip.setValue(value)  # for filling

Note that you can skip the “getMask()” if your ROI is rectangular.

There are other variations, see


Yes, the low level way would be as proposed by @albertcardona.

As for the way:

This works if you use imp.setRoi(roi) for every selected ROI:

# @ImagePlus imp

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

rm = RoiManager.getInstance()

for roi in rm.getSelectedRoisAsArray():
	imp.setRoi(roi), "Clear", "slice"), "Select None", "")

Indeed, I forgot that can take an imp directly, even though I wrote it!

1 Like

That works very well,
I had tried a similar apporach but without selecting the rois (I thouht that that wasn’t necessary) and using rm.getRoisAsArray()
but that didn’t work.

I have just added some lines to place the roi to be cleared in the correct c,z,t slice and the selection of all the rois just above the code and it works perfectly.

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

rm = RoiManager.getInstance()

#clear inside the sphere
for roi in rm.getSelectedRoisAsArray():
    imp.setRoi(roi)   , "Clear", "slice"), "Select None", "")

Thank you and thanks also to @albertcardona for his low level implementation.

Have a nice day,

If you want to process all ROIs, you can simply use:

for roi in rm.getRoisAsArray():
    # etc...

Also, selecting all ROIs in the ROI Manager is equivalent to _de_selecting all: in both cases, all ROIs will be processed.

So if you want to stick with rm.getSelectedRoisAsArray(), a simple:


would suffice.

See also in the user guide:

For most ROI Manager↑ operations Deselect works as a Select All button, e.g., to measure all ROIs in manager one would press Deselect then Measure.