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()
imp.show()
for i in range(0,rm.getCount()):
    rm.select(i)
    IJ.run(imp, "Clear", "slice");        
    rm.runCommand(imp,"Deselect");

It of course works but I cannot use it without imp.show() 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:
https://imagej.net/ij/developer/api/ij/plugin/filter/Filler.html#clear-ij.process.ImageProcessor-

I implemented this code:

FIL = Filler();
for roi in rm.getRoisAsArray():
    imp.setZ(roi.getZPosition())
    imp.setC(roi.getCPosition())
    imp.setT(roi.getTPosition())
    imp.setRoi(roi)
    ip = imp.getProcessor();
    FIL.clear(ip) 

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
  ip.setRoi(rm.getRoi(i))
  ip.fill(ip.getMask())

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

There are other variations, see https://javadoc.imagej.net/ImageJ1/ij/process/ImageProcessor.html

3 Likes

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

As for the IJ.run() 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)
	IJ.run(imp, "Clear", "slice")

IJ.run(imp, "Select None", "")
2 Likes

Indeed, I forgot that IJ.run 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()

rm.setSelectedIndexes(range(0,rm.getCount()))
#clear inside the sphere
for roi in rm.getSelectedRoisAsArray():
    imp.setZ(roi.getZPosition())
    imp.setC(roi.getCPosition())
    imp.setT(roi.getTPosition())
    imp.setRoi(roi)        
    IJ.run(imp, "Clear", "slice")

IJ.run(imp, "Select None", "")

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

Have a nice day,
Emanuele

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:

rm.deselect()

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.

2 Likes