Merge ROIs into one big one/ bounding n polyline

Tags: #<Tag:0x00007fb87e1a05c0>



Is there a way to make merge multiple ROI into one big one ?
Basically I have the following
And I would like to have something that looks like this:
My aim being to erode the big ROI a certain thickness in order to have only the big fragment in the inside (shown by white arrow).

This is the original image:

Thank you very much,


Good day!

Perhaps you try the following ImageJ-macro;

setAutoThreshold("RenyiEntropy dark");
run("Analyze Particles...", "size=2500-3500 show=Nothing add");
roiManager("Select", 0);

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it after you’ve opened your sample image in ImageJ.




Hi Herbie,

Thank you very much for your reply

I actually omitted to say that the full stack looks like this

And using any of the thresholds I either select too much or not enough.

I had the best results with the Phansalkar Auto Local Threshold but I still get something like this:

This I why I thought that I I could have for example the bounding n polyline of a ROI like the following I could then erode it from the outside to the inside and only keep the features that I am interested in

Thank you for your help :slight_smile:


Sorry, but did you try the macro for your sample image?

Does it do what you want for the sample image, and if yes, could you please confirm?



I tried your macro and in does indeed select what I want but unfortunately it works only in that specific sample image. The reason being that the features in the inside have variable sizes and intensities.

This is why I thought that if I could erode from the outside to the inside I would then have only all these features that I am interested in regardless of their size of intensity.

Thank you very much for your help :wink:


What you are trying to achieve sounds a bit as if you expect a machine to know in what image details you are interested.

Besides, there is no erosion of a polyline selection. You can only shrink a selection or erode a mask. But how should such a process know where to erode more and where less and where to stop?

I don’t think that this is a successful strategy.




dear @LPUoO,
I think you are searching for something similar to the Convex Hull of your mask.
I have found this implementation in fiji/imagej
And it seems work quite good, but it seems not so user friendly to use and of course it really finds the convex hull so on the top part of your image it will do something not accurate and worse than your segmentation.
Here a results on one of your image:


It also find a circle, but you just need the convex hull.

It’s a big approximation and @herbie it could be right that it will not be a succesful strategy, but maybe it could be enough for your scopes.

Have a nice day,
Emanuele Martini


Hi Folks!
I don’t mean to butt in, but if all of your sample or images of interest are the same color you possibly might use the wand tool to isolate the object of interest.

Just a thought. Good Day


Hi @LPUoO,

There was a very similar question on StackOverflow recently, asking to get the convex hull of a discontinuous composite ROI. I posted a small Groovy script in the answer:

In case the link shouldn’t work, here’s the Groovy script again (you can run it from the script editor after choosing Language > Groovy):

#@ ImagePlus imp

import ij.gui.PolygonRoi
import ij.gui.Roi
import ij.plugin.filter.ThresholdToSelection
import ij.process.ImageProcessor

roi =
proi = new PolygonRoi(roi.getContainedFloatPoints(), Roi.POLYGON)
chRoi = new PolygonRoi(proi.getConvexHull(), Roi.POLYGON)

You would still somehow have to select the correct subregion from your image, but it should solve your issue of having discontinuous ROIs due to suboptimal thresholding.

Hope it helps. Let me know if you’re having any issues with it.


Thank you @imagejan,

This is exactly what I wanted. I will now combine this to a Semi-automatic segmentation by active contours ( in order to have a closer selection.

As I only know how to use IJ1Macros, Is it possible to run your groovy script from a IJ1Macro ?

Thank you :slight_smile:


I would have suggested a macro if I knew a way how to do this from the macro language. Unfortunately, the Edit > Selection > Convex Hull command (that you can call from a macro like run("Convex Hull"); ) supports only continuous polygon or freeline selections, so it’s not useful for the given task.

Maybe someone else can help you calling this directly from a macro [1], but I’d recommend to rather go the other direction and also do your other processing in a full-fledged scripting language. The macro recorder can record Javascript and Beanshell which can both be interpreted by the Groovy interpreter with almost no changes. You can also transform the Groovy script into a Javascript script by:

  • replacing import ij.<whatever> calls by importClass(Packages.ij.<whatever>);
  • adding semicolons at the end of every line.

[1]: Note you can always save your script in a subdirectory of (e.g. and then call it from macros via run("My Convex Hull"); after restarting ImageJ.