Error with Z position of ROI

Hi all,

I’m getting some ROIs using TrackMate and then want to use it in the ROI Manager to do 3D Watershed at a later step. However, somehow, when I’m adding the ROIs to the manager, something weird happens to the Z.

for (peak in peaks)
            // Add the current peak to the Roi Manager
            def roi = new PointRoi(peak.getDoublePosition(0) / cal.pixelWidth,
                    peak.getDoublePosition(1) / cal.pixelHeight)

            println "Z"
            def z_position = ((int) (peak.getDoublePosition(2) / cal.pixelDepth).round() + 1 )
            // Prints the good Z position
            println z_position
            roi.setPosition(channel_of_interest, z_position, 1)
            // Works as well
            println roi.getZPosition()

for (roi = 0; roi < rm.getCount(); roi++ )
        def test = rm.getRoi(roi)
        println "Test"
        // Prints 1 everytime
        println test.getZPosition()

Any ideas why ? Thanks.

Hi @lguerard,

what’s rm in that code? RoiManager I guess?


Also, try changing this:

def test = rm.getRoi(roi)

to this:

test = rm.getRoi(roi)

Edit: I take it back, this probably wont help

Yes sorry. It was defined like that

    // Add points to ROI Manager
    RoiManager rm = RoiManager.getRoiManager()
    rm.runCommand("Associate", "true");
    if (!rm)
        rm = new RoiManager()

Tested that as well but didn’t work either :confused:

Hey @Iguerard!

Did you manage to find a proper solution to get the Z- position?

I am currently struggling with a similar issue. I ask the user to mark some points in a z-stack using the WaitForUserDialog and add them to the ROI-manager. I use the multi-point tool for this. When multiple points have been added, the z-position is always the last slice where the point was placed when I run rm.runCommand(imp, "measure") and always 0 if I run roi.getZPosition(). However, if I manually mark one point on a specific slice, add to the ROI manager click on “Measure”, I get the slice number correctly.

Hey @gayaJ,

I actually asked a second time on the forum about this issue and it got fixed in recent version of ImageJ as you can see from this forum thread : Issue with ROI Manager?

Which version of Fiji are you using ?

I have the version: 1.53c. I already saw the post you have tagged and it didn’t help me. I am not sure whether this is because I specifically create PointRoi from the x, y points.

A minimal example to reproduce my issue (in jython):

from ij import IJ, ImagePlus, ImageStack
from ij.gui import Roi, PointRoi 
from ij.measure import ResultsTable 
from ij.gui import WaitForUserDialog
from ij.plugin.frame import RoiManager

imp = IJ.openImage("");
#imp = IJ.getImage();

rm = RoiManager.getInstance()
if not rm:
	rm = RoiManager();

WaitForUserDialog("Action required", "Mark some points and click ok").show() 

# get points and add them to roi manager
points = imp.getRoi(); 
for p in points:
	proi = PointRoi(p.x, p.y)

# measure --> slice number is always the last slice marked
# but if only one point was marked, slice number is correct
rm.runCommand(imp, "measure");

# here z is always 0 irrespective of number of points marked
roi_array = rm.getRoisAsArray();
for roi in roi_array:
	print "Roi Z position =", roi.getZPosition()

I guess the error you’re getting here is that you’re not setting the Z position of your ROI when adding it to the ROI Manager.

You should have something like this :

points = imp.getRoi(); 
for p in points:
	proi = PointRoi(p.x, p.y)

I’m not that familiar with the multi-point tool but I guess there should be a way to get the Z slice from it ?

That works well if I set the slice myself. But since I get the points from “user”, I cannot hard code the slice numbers. The output I get from getRoi() is just a list of points (java.awt.Point) and this gives me just the x, y coordinates :confused: Even p.getLocation() gives me just x, y.

Guess it’s suitable for 2D only. But then it’s strange that for a single point, I can still get the slice number right. I’ll check more on the multi-point tool.

I was kinda right on the explicit PointRoi creation. What kinda worked for me just now is by directly adding the points to the ROI manager and then running measure.

# get points and add them to roi manager
points = imp.getRoi(); 

# now I get all the slice numbers from the results table
rm.runCommand(imp, "measure");

Now, I will have to grab hold of the slice numbers from the results table. Might not be so clean, but at least something works :wink:

I was also testing and this seems to work :slight_smile:

points = imp.getRoi(); 
print points.getPointPosition(ROI_number)

Oooh! This looks much much neater. How did you get the roi_number though? I tried points.nPoints, gives me attribute error :confused:

This seems to be working with your script and with limited change :wink: :

for idx, p in enumerate(points.iterator()):
	proi = PointRoi(p.x, p.y)
	# print points.getPointPosition(idx)

Thats wonderful! It works beautifully! Thanks a lot for the efforts :smiley: it was like a treasure hunt, although very rewarding!

1 Like

Glad that it’s working, it was fun discovering the multi point tool as well :wink:

1 Like