Selecting groups of selections within a defined area

I am using a threshold procedure to quantify labeled axons in some images I have taken. The threshold procedure results in a couple thousand selections being produced per image, and it is doing a nice job identifying and producing selections for axons. However, it is also picking up a lot of false positives in some sections (blood vesicles, background etc.). If I adjust the threshold so that these false positives are no longer identified, I lose the axons as well. As it stands, I need to comb through the images and delete a couple thousand small false positive selections one at a time. What I am looking for is some sort of magic wand type tool that allows me to select multiple selections within an area that I draw so that I can delete all of the selections within that area, but I have not been able to find anything like this.

Does a tool like this exist within ImageJ?

Hello Zak -

I’m not sure that I fully understand your use case, but let me
suggest how you might do what I think you want.

I do not believe that there is a built-in tool that will let you delete
a group of ROIs in a single step. But the ROI Manager supports
“set arithmetic” with ROIs that can be used to do what I think
you want.

I assume that you start with a bunch of ROIs (selections) in the
ROI Manager (from running, say, Analyze Particles...).
I also assume that you have the image you are working with open.

First select all of the ROIs in the ROI Manager. Then click
More >> on the right side of the ROI Manager and click
OR (Combine). This applies the “combined” ROI to your image.

Click Add [t] to add this new ROI to the ROI Manager. Delete
the individual ROIs by selecting them in the ROI Manager and
clicking Delete.

Now draw the ROI that contains the ROIs you wish to delete (for
example, using the “Polygon selections” tool on the Fiji toolbar).
Run Edit > Selection > Make Inverse. This makes the
outside of the ROI you drew into an ROI that contains the ROIs
you wish to keep (rather than delete). Add it to the ROI Manager.

Select the “combined” ROI and the “inverted-deletion” ROI in
the ROI Manager and run More >> AND. This applies a single
combined ROI to your image that is made up of the ROIs you
wish to keep. Add it to the ROI Manager (and delete the old
“combined” ROI and the “inverted-deletion” ROI).

Now select the new “combined” ROI and run More >> Split.
This loads the ROI Manager with the individual ROIs that you
wish to keep. (Delete the new “combined” ROI.)

The ROI Manager should now contain your original ROIs minus
those that you enclosed in your “deletion” ROI. (You can use
them for measurements, further processing, or apply them to
your original or other images.)

This process is illustrated with a complete, runnable jython
(python) script.

(To run the script, run File > New > Script... to open the
script editor, set the “Language” to “Python”, copy-paste the script
into the editor window, and click Run.)

Here is the script:

from ij import IJ
from ij.gui import Overlay
from ij.gui import Roi
from ij.plugin.frame import RoiManager

rm = RoiManager()

# make sample image for display purposes
imp = IJ.createImage ('roi_delete', '8-bit ramp', 512, 512, 1)
imp.getProcessor().multiply (0.5)
imp.show()

ovl_all = Overlay()
ovl_wand = Overlay()
ovl_deleted = Overlay()

# load ROI Manager with initial ROIs
for  i in range (8):
    for  j in range (8):
        x = i * 64 + 20
        y = j * 64 + 20
        r = Roi (x, y, 24, 24)
        ovl_all.add (r)
        rm.addRoi (r)

# first "Combine" ROIs into a single ROI
rm.runCommand ('Combine')
rm.reset()
rm.addRoi (imp.getRoi())

# make ROI containing ROIs to delete
r = Roi (128, 128, 256, 256)
ovl_wand.add (r)
imp.setRoi (r)

# "Inverse" the deletion ROI
IJ.run ('Make Inverse')
rm.addRoi (imp.getRoi())

# take intersection ("And") of combined ROI and inverted-deletion ROI
rm.runCommand ('And')
rm.reset()
rm.addRoi (imp.getRoi())

# now "Split" the partially-deleted combined ROI back into separate ROIs
rm.runCommand ('Split')

# delete the combined ROI
rm.select (0)
rm.runCommand ('Delete')

# display the individual ROIs
rm.runCommand (imp, 'Show All')

rois = rm.getRoisAsArray()
for  r in rois:
    ovl_deleted.add (r)

# save flattened overlay images illustrating process for upload
imp_all = imp.duplicate()
imp_all.setOverlay (ovl_all)
imp_all.flatten()
IJ.saveAs (imp_all, 'PNG', 'imp_all')
imp_wand = imp.duplicate()
imp_wand.setOverlay (ovl_wand)
imp_wand.flatten()
IJ.saveAs (imp_wand, 'PNG', 'imp_wand')
imp_deleted = imp.duplicate()
imp_deleted.setOverlay (ovl_deleted)
imp_deleted.flatten()
IJ.saveAs (imp_deleted, 'PNG', 'imp_deleted')

The images produced by the scripts are as follows:

The original ROIs:

The ROI that encloses the ROIs to delete:

And the ROIs after deletion:

Thanks, mm