Error in loading ROI

Hi all,

I am having some troubles with ROI.
So basically I open an image and then select some ROI using single point selection. Then I save the list of ROI in Zip format.

The image I am using has the following info:

Title: Beads_01.tif
Width: 520 pixels
Height: 520 pixels
Depth: 30 pixels
Size: 15MB
Voxel size: 1x1x1 pixel^3
ID: -4
Bits per pixel: 16 (unsigned)
Display range: 0 - 61
Image: 1/30
No threshold
Uncalibrated
Path: filepath
Screen location: 402,23 (1920x1080)
SetMenuBarCount: 13 (244ms)
Coordinate origin: 0,0,0
No overlay
No selection

To open and read the ROI I use the following script:


rm = RoiManager.getRoiManager();
rm = RoiManager.getInstance();
rm.reset();
rm.runCommand("Open", "file"); 

roi = rm.getRoi(2)
print roi
print roi.getXBase(),roi.getYBase(),roi.getZPosition(),roi.getTPosition(),roi.getCPosition()

In the ROI Manager, I can see that the 3rd ROI is the following:
0018-0199-0391

However, the print of the above script gives me:
Roi[Point, x=391, y=199]
391.0 199.0 0 0 0

Do you know why the ZPosition is wrong?

Thanks a lot for your help
Zhan

1 Like

I consider this a bug in ImageJ1: apparently, roi.getZPosition() only works when an active selection from a hyperstack was added to the ROI manager, but not if the image was a normal stack.

The following Groovy script illustrates this:

iimport ij.IJ
import ij.gui.PointRoi
import ij.plugin.frame.RoiManager

rm = RoiManager.getInstance()

imp = IJ.openImage("http://imagej.net/images/bat-cochlea-volume.zip")
roi = new PointRoi(60,60)
imp.setSlice(14)
imp.setRoi(roi)
rm.addRoi(imp.getRoi())
imp.close()

imp2 = IJ.openImage("http://imagej.net/images/Spindly-GFP.zip")
imp2.setSlice(5)
imp2.setRoi(roi)
rm.addRoi(imp2.getRoi())
imp2.close()

for (r in rm.getRoisAsArray()) {
	println r.getZPosition()
}

This will print:

0
3

@Wayne is this behavior intended?


@yinxiu as a workaround, you can get the z position by parsing the default name of the ROI (using roi.getName()) as discussed here:

@yinxiu, you can get the stack position using roi.getPosition(). The roi.getZPosition() method is normally only used with hyperstacks.

@imagejan, I do not see a way to get roi.getZPosition() to work with selections added to a normal stack without the risk of breaking existing code.

@imagejan @Wayne Thanks a lot for your replies!

roi.getPosition() works on images that are not hyperstacks.

Just on more (maybe naive) question: is it normal that, even if I load the image with Bio-Formats Importer, and select specifically hyperstacks mode, it still does not work with roi.getZPosition()?

Thanks
Zhan

Upgrade to the latest ImageJ daily build (1.51m2) and roi.getZPosition() should work as expected.

Hello @Wayne and @imagejan,

I’m reviving this old thread, but with an up-to-date Fiji (9th August 2020, Fiji 2.1.0; Java 1.8.0_172; ImageJ 1.53c), the script from Jan fails : I get 0 and 0 as outputs.

I noticed this because a few scripts I wrote that used roi.getZPosition() (or roi.getPosition()) are all failing right now : z position is always zero.

Do you have the same output as me @imagejan with your script ?

Nico

1 Like

This behaviour changed somewhere between pom scijava 27.0.1 and 29.2.0.

I continue to investigate…

I’m trying to find a way to specifiy the version of IJ1 explicitely (ok, this can be done with <imagej1.version>1.52u</imagej1.version>in the pom file)

EDIT : the behaviour changed between ij1 v1.52u (OK) and v1.52v (not OK)

EDIT2 : this commit:

Changed the behaviour

1 Like

@Wayne shouldn’t it be (NOT ignorePosition) in this condition:

The following JavaScript example works as expected. It outputs:

bat-cochlea-volume
  Position: 14
  ZPosition: 14
Spindly-GFP
  Position: 5
  ZPosition: 5
ROI Manager
  Position: 14
  ZPosition: 14
  Position: 5
  ZPosition: 5
rm = RoiManager.getInstance()
if (rm==null)
   rm = new RoiManager();
rm.reset();

imp = IJ.openImage("http://imagej.net/images/bat-cochlea-volume.zip")
roi = new PointRoi(60,60)
roi.setPosition(14)
print("bat-cochlea-volume");
print("  Position: "+roi.getPosition())
print("  ZPosition: "+roi.getZPosition())
imp.setRoi(roi)
rm.addRoi(imp.getRoi())
imp.close()

imp2 = IJ.openImage("http://imagej.net/images/Spindly-GFP.zip")
roi.setPosition(5)
print("Spindly-GFP");
print("  Position: "+roi.getPosition())
print("  ZPosition: "+roi.getZPosition())
imp2.setRoi(roi)
rm.addRoi(imp2.getRoi())
imp2.close()

print("ROI Manager");
for each (r in rm.getRoisAsArray()) {
   print("  Position: "+r.getPosition())
   print("  ZPosition: "+r.getZPosition())
}
1 Like

Ok, so now we need to call explicitely roi.setPosition ? This was not present in the script of Jan.

Before this change, the Z position of the ROI could be set by calling imp.setSlice() before adding it to the roiManager. If that’s not possible anymore, I’ll change my macros and will put
Roi.setPosition(channel, slice, frame); when needed.

The best way to set the position of an ROI is to call Roi.setPosition(slice) or Roi.setPosition(channel,slice,frame).

2 Likes

Yes, and that is also what the recorder records (e.g. in Beanshell or Javascript mode) when you call Image › Stacks › Set Slice… and then create an ROI.

That’s definitely a change that breaks backwards compatibility.

@Wayne if this change is going to persist, I suggest changing the recorder behavior accordingly.

Please provide a small script that that shows there is a change. The output of this JavaScript (“position=0”) is the same on ImageJ 1.51w, 1.52p and 1.53c:

rm = RoiManager.getRoiManager()
rm.reset();
imp = IJ.createImage("Untitled", "8-bit ramp", 200, 200, 50);
roi = new PointRoi(60,60)
imp.setSlice(14)
imp.setRoi(roi)
rm.addRoi(imp.getRoi())
roi = rm.getRoi(0)
print("position="+roi.getPosition())