Issue with reading slice coordinate of Rois in RoiManager when using Hyperstacks

Hi all,
Hi @Wayne

I’m experiencing issues when reading the slice positions when working with ROIs in the ROIManager. This is the code I’m executing:

// clean up first
close("*");
roiManager("reset");
print("\\Clear");

// laod data
run("Mitosis (5D stack)");  // it doesn't work with a 5D hyperstack
//run("T1 Head (16-bits)"); // it works with a normal 3D sack

// add point 1
Stack.setSlice(4);
makePoint(1, 2);
roiManager("add");

// add point 2
Stack.setSlice(7);
makePoint(3, 4);
roiManager("add");

// read out slice position of point 1
roiManager("select", 0);
Stack.getPosition(channel, slice, frame);
print(slice);

// read out slice position of point 2
roiManager("select", 1);
Stack.getPosition(channel, slice, frame);
print(slice);

And that’s the output it produces:
image

If one replaces the 5D-mitosis dataset with the 3D T1 Head, the output becomes:
image

I think the output should be the same in both cases (the second is right).

That’s the Fiji version I’m working with:

Might this be a bug?

Thanks for your support!

Best,
Robert

1 Like

Hi Robert,

An issue is that the Mitosis 5D stack only has 5 slices. So your second point cannot exist at slice 7 and Fiji places it at slice 5.

However, the macro gives “4 then 5” as results on my machine, so I don’t get how you obtain “5 and 5”.

Sincerely,

Matthieu

1 Like

Hey @MatthieuV ,

excellent point! When I adapt the slice positions as you suggested, the issue persists:

// clean up first
close("*");
roiManager("reset");
print("\\Clear");

// laod data
run("Mitosis (5D stack)");  // it doesn't work with a 5D hyperstack
//run("T1 Head (16-bits)"); // it works with a normal 3D sack

// add point 1
Stack.setSlice(2);
makePoint(1, 2);
roiManager("add");

// add point 2
Stack.setSlice(3);
makePoint(3, 4);
roiManager("add");

// read out slice position of point 1
roiManager("select", 0);
Stack.getPosition(channel, slice, frame);
print(slice);

// read out slice position of point 2
roiManager("select", 1);
Stack.getPosition(channel, slice, frame);
print(slice);

image

Very strange: I was getting 2 and 3. However, if I run the macro without a stack -and get an error message- then run with an image, I got 3 and 5…

I’m confused.

1 Like

It looks like the issue is with the ROI Manager since as soon as a point is included, its coordinates get mixed. The commands Roi.setPosition(channel, slice, frame) and Roi.setPosition(channel, slice, frame) don’t work anymore.

Try this on a multichannel stack:

roiManager("reset");
print("\\Clear");

// add point 1
//Stack.setSlice(2);
Title=getTitle();
Stack.setPosition(1, 2, 1);
makeRectangle(1, 2, 10, 10);
Roi.getPosition(channel, slice, frame);
print("RoiBeforeManager1", channel, slice, frame);
roiManager("add");
Roi.setPosition(1, 2, 1);
Stack.getPosition(channel, slice, frame);
print("Rectangle1", channel, slice, frame);

// read out slice position of point 1
roiManager("select", 0);
Roi.getPosition(channel, slice, frame);
print("RoiInmanager", channel, slice, frame);
selectWindow(Title);
Stack.getPosition(channel, slice, frame);
print("Roi1", channel, slice, frame);
2 Likes

Hi @haesleinhuepf,

Use Roi.setPosition(c,z,t) to set the position of an ROI before adding it to the ROI Manager. This works with ROIs on both stacks and hyperstacks. This is what is recorded when you manually add an ROI to a hyperstack. You also need to upgrade to the latest ImageJ daily build (1.53i23), which fixes a bug that caused roiManager(“select”,index) to sometimes restore ROIs at the wrong stack position.

Here is a version of your test macro that should work correctly:

// clean up first
close("*");
roiManager("reset");
print("\\Clear");

// load data
run("Mitosis (5D stack)");  // it doesn't work with a 5D hyperstack
//run("T1 Head (16-bits)"); // it works with a normal 3D sack

// add point 1
makePoint(1, 2);
Roi.setPosition(1, 3, 1);
roiManager("add");

// add point 2
makePoint(3, 4);
Roi.setPosition(1, 5, 1);
roiManager("add");

// read out slice position of point 1
roiManager("select", 0);
Stack.getPosition(channel, slice, frame);
print(slice);

// read out slice position of point 2
roiManager("select", 1);
Stack.getPosition(channel, slice, frame);
print(slice);
1 Like

Hi @MatthieuV,

Here is a version of your test macro that works as expected. It uses Roi.setPosition(c,z,t) to set the ROI position before the ROI is added to the ROI Manager. This is what is recorded when you manually add an ROI to the ROI Manager and the current image is a hyperstack.

roiManager("reset");
print("\\Clear");

// add rectangle
makeRectangle(1, 2, 10, 10);
Roi.setPosition(1, 2, 1);
Roi.getPosition(channel, slice, frame);
print("Roi Before Manager:", channel, slice, frame);
roiManager("add");

// read out slice position of rectangle
roiManager("select", 0);
Roi.getPosition(channel, slice, frame);
print("Roi In manager:", channel, slice, frame);
Stack.getPosition(channel, slice, frame);
print("Stack position:", channel, slice, frame);
1 Like

Thank you Wayne! It works like a charm :slight_smile: