Gilles Carpentier Research Web Site: Computer Image Analysis
Image analysis tools in biology and biochemistry using ImageJ

Home > Sort Selections in Overlay - Application

Sort Selections in Overlay - Application

Thursday 22 March 2018, by Gilles Carpentier

  Table of contents  

The « Sort Selections in Overlays » macro is an example of handling vectorial objects - named overlays in ImageJ - by using single or composite selection of different nature. This brief page gives examples of use, and an application in cellular neurology.


Get overlays elements from any kind of selection (line, freehand etc...) with the options of inclusion, exclusion and intersection (using exclusion removes selected overlays). It allows sorting of the selected overlays elements from their colors. Compatible with composite images.


From the code page here select all and copy the text. Then open a new macro window on ImageJ (Plugins -> New -> Macro menu), paste the text code and install it ("Macro -> Install Macros" ImageJ text editor menu).

 Demo Image:

A demo image of neuron cells culture is downloadable from the macro menu. Once the code installed as explained above, open the demo image by the "Plugins -> Macros -> Open Sample Image" menu. A two channel composite image is then opened, containing detected objects as overlays from different colors and two user selections.

Left: composite image of a portion of a neuronal dendrite. Labeling: psd-95 post-synaptic
elements (red), V-Glut (green) pre-synaptic elements. Image courtesy of Pr.O. Stettler.
Experiment author: Asma Soltani. Image acquisition and processing: Gilles Carpentier,
laser confocal microscope Olympus IX81 FV 1000.
Right, the same with detected objets (overlays) and user selections (yellow).
Yellow selections can be removed by clicking in the image and then restored by « Edit ->
Selection -> Restore Selection » ImageJ’s menu. Overlays can be hidden/showed
by « Image -> Overlay -> Hide/Show » ImageJ’s menus.

 Example of Sorting:

After opening the demo image, select « Sort Selection In Overlays » function in the menu accessible through "Plugins -> Macros". Run the default settings to get the following result:

Left, default settings of the « Sort Selection In Overlays » function. Right, result of the function;
vectorial objects retained are those included and at the intersection of the limits of the polygonal
and free hand selections.


In the demo image, the selections are made by the user. The object sorting can also work automatically by using selections made from activation of overlays by user menu actions or macro code. This can be done through the ROI Manager interface, by the "Combine" option. The next pictures detail how to proceed.

- First, from the demo image the sorting of green objects in and at the intersection with user selections was performed. Resulting overlays were converted into a composite selection by using the ROI Manager:

Above: after sorting green objects in user selections, green overlays were transferred to the
ROI Manager (ImageJ menu : Image-> Overlay -> To Roi Manger).

Above: Green objects were all selected and the "more >> OR (Combine)" operator applied.

- Then sorting of red objects in intersection with previously sorted green objects was performed after restoring the composite selection obtained from the ROI Manager (ImageJ menu : Edit-> Selection -> Restore Selection) in the initial demo image and then "Plugin -> Macros -> Sort Selections in Overlays" menu.

Above: right Image exhibits the composite selection after combining operation in the ROI Manager,
left image shows the result of the sorting using the same composite selection restored in the
initial demo image.


This kind of operation implemented in an original program allowed to put in evidence synaptic alterations in mouse neurone primary culture:

  • Below: example of object sorting used in the paper : The slide show exhibits a confocal image analysis of a neuron synaptic area from a mouse hyppocampal neuron culture in conditioned medium. The first one shows the user selection in the initial image. The second one shows in yellow the intersection between red and green labelling after detection (not shown). It was performed according the same principle than described above.
    Neuron synaptic area detection - user selection Neuron synaptic area detection
    | prev | next |

Soltani A, Lebrun S, Carpentier G, Zunino G, Chantepie S, Maïza A, Bozzi Y, Desnos C, Darchen F, Stettler O. Increased signaling by the autism-related Engrailed-2 protein enhances dendritic branching and spine density, alters synaptic structural matching, and exaggerates protein synthesis. PLoS One. 2017 Aug 15;12(8):e0181350. doi: 10.1371/journal.pone.0181350. Abstact, Paper.

logoij ImageJ ( is a public domain Java image processing program inspired by NIH Image for the Macintosh. It runs, either as an online applet or as a downloadable application, on any computer with a Java 1.1 or later virtual machine. Downloadable distributions are available for Windows, Mac OS, Mac OS X and Linux. The author, Wayne Rasband (, is at the Research Services Branch, National Institute of Mental Health, Bethesda, Maryland, USA.
Gilles Carpentier, Faculte des Sciences et Technologie,
Universite Paris Est Creteil Val-de-Marne, France.

Special thanks to Alessandra Albano for the English correction of these sites.
Computer Data Acquisition
for Biochemistry Practice Works
Image analysis tools in biology
and biochemistry using ImageJ