ROIs added to ROI Manager by macro do not maintain all properties

Hello everyone,

I’m trying to write a macro that adds ROIs to the ROI Manager with various properties, especially a slice position. However in 1.53a, ROIs added to the ROI manager via an ij macro are losing their slice position information (as well as other properties like stroke color, stroke width, and fill color - depending on the order in the macro). I’ve written a small .ijm macro to investigate this issue:

requires("1.53a");

// For each scenario please take a moment to select ROI and look at its slice association, and properties
// ROI Manager > Properties...
// Also note discrepancies other properties such as fill and stroke colors

newImage("Test_Image", "8-bit black", 300, 300, 5);
roiManager("Associate", "true");

function drawRandomTriangle()
{
	// Random Triangles
	x = newArray(getWidth * random, getWidth * random, getWidth * random);
	y = newArray(getHeight * random, getHeight * random, getHeight * random);

	// Draw Triangle Selection
	makeSelection("Polygon", x, y);
	Roi.setStrokeColor("yellow");
	Roi.setStrokeWidth(2);
	Roi.setFillColor("red");
}

// Test Parameters
Dialog.create("ROI Manager Test Parameters");
	Dialog.addCheckbox("setSlice()", true);
	Dialog.addCheckbox("Roi.setPosition", true);
	Dialog.addCheckbox("Roi.setName()", true);
Dialog.show()
paramA = Dialog.getCheckbox();
paramB = Dialog.getCheckbox();
paramC = Dialog.getCheckbox();

// Generate and Apply Parameters
roiManager("Reset");
for (i = 1; i <= nSlices; i++)
{
	drawRandomTriangle();

	if (paramA) setSlice(i);
	if (paramB) Roi.setPosition(0, i, 0);
	if (paramC) Roi.setName("someName_" + i);
	
	roiManager("Add");
}

It appears that slice association is only maintained when I use the following parameters:

  • setSlice(i) = true (Setting the correct slice before adding the ROI)
  • Roi.setName(…) = false (Using the default ROI Manager naming scheme)

Roi.setPosition(0, i, 0) seems to only alter the ROI Manager > Properties… > Position (c, s, t) information but does not associate the ROI to a slice (when selecting that ROI from the ROI manager). Slice association is lost if I alter the ROI name with Roi.setName.

Shouldn’t any combination of parameters in this test macro - with either assigning a slice via setSlice() or Roi.setPosition() - yield correct slice association and slice property information? Am I misunderstanding how slice information is stored in the ROI Manager for each ROI? Any guidance would be appreciated.

Hello Erod -

This is reminiscent of (but not the same as) the issue discussed in
the following thread:

It may be useful to ask @Wayne to comment.

I can partially reproduce your issue on 1.52t. On that version, only
the Roi.setPosition() value is not remembered (or maybe the
call fails), but the stroke color, stroke width, and fill color all do seem
to be preserved.

You say “However in 1.53a.” Did this used to work for you in
previous versions? If so, which ones? Could you try downgrading
to something like 1.52r (or maybe 1.52t) and see if you see the
same or a similar issue?

Thanks, mm

1 Like

Hi @erodb,
Upgrade to the latest ImageJ daily build (1.53b5), change Roi.setPosition(0,i,0) in your test macro to Roi.setPosition(i), and it should work as expected.

1 Like

Hello Wayne (and Erod) -

Thanks to Wayne, I can confirm that changing Erod’s macro to use
Roi.setPosition(i) works for me on the new 1.53b5.

Thanks, mm

1 Like

Downgrading to anything pre-1.52r resolved the issue for me resolved the issue for me

This has resolved the issue as well in both my test macro, as well as other macros I’ve been trying to develop with a similar issue

Thank you @Wayne and @mountain_man for your help!