Overlay duplication doesn't keep all the settings

Hi there,

We are currently working on a Java plugin for physicians to compute certain parameters in medical images. For this application, we rely heavily on the ImagePlus overlay to display different informations to the user easily.

I need to get a copy of the ImagePlus overlay with all the settings (selectable, drawNames, drawLabels, etc.) unchanged. I tried to use the overlay.duplicate() method but it only keeps the Rois in the cloned overlay, all the settings are set to default.

Here’s the source for the duplicate method:

/** Returns a clone of this Overlay. */
public Overlay duplicate() {
	Roi[] rois = toArray();
	Overlay overlay2 = create();
	for (int i=0; i<rois.length; i++)
	return overlay2;

Would it be possible to change the duplicate method in order to keep the settings unchanged?

Best regards,

1 Like

Hi RubenGres,

In order to solve this problem, Java can make the extended class.
I think that it is only adding the item that sets the original setting to overlay2.

And, I came up with several ways, but I refrain from concrete examples as your purpose is not fully understood.


Hi @hwada,

First of all, thank you for your reply.

I dug a bit deeper into this issue and I think I understood where my problem came from :
The overlay.duplicate method relies on overlay.create method to keep the overlay properties unchanged. It seems that overlay.create() method doesn’t keep the properties calibration bar and selectability.

I’ve written my own duplicate method to go around this issue, it goes like this:

public static Overlay duplicateOverlay(Overlay overlay) {
	Overlay overlay2 = overlay.duplicate();

	overlay2.setLabelFont(overlay.getLabelFont(), overlay.scalableLabels());
	//theses properties are not set by the original duplicate method
	return overlay2;

I ran into this issue trying to use the duplicate method but the same problem should come up with the crop method too since it’s also using overlay.create()

I wasn’t really sure where my issue came from in my first message, I hope I made myself clearer this time around.


1 Like

Hi @RubenGres,

Since it sounds like you figured it out, perhaps you can instead modify that
create method and file a pull request here


This bug is fixed in the latest ImageJ daily build (1.52d6).


For those interested in the details, here’s the relevant git commit:


Thank you for Wayne’s help!
I have the possibility to create plugin using this like duplication.
So this fix is ​​also happy for me!