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++)
		overlay2.add((Roi)rois[i].clone());
	return overlay2;
}

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

Best regards,
Ruben

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.

hwada

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.drawLabels(overlay.getDrawLabels());
	overlay2.drawNames(overlay.getDrawNames());		
	overlay2.drawBackgrounds(overlay.getDrawBackgrounds());
	overlay2.setLabelColor(overlay.getLabelColor());
	overlay2.setLabelFont(overlay.getLabelFont(), overlay.scalableLabels());
	
	//theses properties are not set by the original duplicate method
	overlay2.setIsCalibrationBar(overlay.isCalibrationBar());
	overlay2.selectable(overlay.isSelectable());
	
	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.

Ruben

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

John

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

2 Likes

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

2 Likes

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!

hwada