Creating Labeled Image from ROIs in the ROI Manager?

Hi,

Is there a built-in way to create a labeled mask from ROIs which are already in the ROI Manager. The idea is to get labels mask from manually drawn ROIs in the ROI Manager, similar to the one that is generated by Analyze Particles with the “count masks” option.

Thanks
Ofra Golani

Hi Ofra,

what you’re asking for is the general concept of a Labeling image.

As far as I know, there’s no built-in way to do exactly what you want (converting ROI Manager entries to a labeling image), but there are several tools available from update sites:

  • the LOCI update site provides a ROI Map command (probably the closest to what you want to do), that produces an 8-bit indexed color image for up to 255 ROIs, or a 16-bit image when the number of ROIs is higher,

  • the 3D ImageJ Suite update site (by @ThomasBoudier) provides Plugins > 3D > 3D Manager that allows to convert binary masks to labelled images,

  • the IJPB Plugins update site (by @iarganda and @dlegland) provides MorphoLibJ > Binary Images > Connected Components Labeling, converting binary masks to labelled images,

  • the BAR update site (by @tferr) provides a BAR > Annotation > ROI Color Coder that allows to label ROIs according to different parameters.


If you want to have more flexibility (or simply don’t want to depend on too many update sites), the task is also fairly easy to achieve with a small script. I’m pasting below a Groovy and a Python version of a script labeling every ROI with a value corresponding to its position in the ROI Manager list. They also show the unique syntax of those two languages to easily loop over lists with an index:

Groovy

#@ ImagePlus imp
#@OUTPUT ImagePlus result

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

result = IJ.createImage("Labeling", "16-bit black", imp.getWidth(), imp.getHeight(), 1)
ip = result.getProcessor()
rm = RoiManager.getInstance()

rm.getRoisAsArray().eachWithIndex { roi, index ->
	ip.setColor(index+1)
	ip.fill(roi)
}

ip.resetMinAndMax()
IJ.run(result, "glasbey", "")

Python

#@ ImagePlus imp
#@OUTPUT ImagePlus result

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

result = IJ.createImage("Labeling", "16-bit black", imp.getWidth(), imp.getHeight(), 1)
ip = result.getProcessor()
rm = RoiManager.getInstance()

for index, roi in enumerate(rm.getRoisAsArray()):
	ip.setColor(index+1)
	ip.fill(roi)

ip.resetMinAndMax()
IJ.run(result, "glasbey", "")
12 Likes

Thanks alot Jan,

LOCI=>ROI Map works perfectly for my task.

Just curious if there is a way to write the above code in with old-fashioned ImageJ macro language, so I can incorporate this in existing macros. Especially can I call the ip.setColor function from within the ImageJ macro language?

Thanks again
Ofra

1 Like

Sure, the same in IJ1 Macro code would look as follows:

newImage("Labeling", "16-bit black", getWidth(), getHeight(), 1);

for (index = 0; index < roiManager("count"); index++) {
	roiManager("select", index);
	setColor(index+1);
	fill();
}

resetMinAndMax();
run("glasbey");

Have a look at the list of macro functions for details.

3 Likes

Great, Thanks Jan
Ofra