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()
            rm.addRoi(roi)
        }

for (roi = 0; roi < rm.getCount(); roi++ )
    {
        rm.select(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?

John

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()
    rm.reset()

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("http://imagej.nih.gov/ij/images/mri-stack.zip"); 
imp.show()
#imp = IJ.getImage();

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

IJ.setTool("multipoint");
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)
	rm.addRoi(proi);

# 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)
    proi.setPosition(slice_number)
	rm.addRoi(proi);

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(); 
rm.add(points)

# 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)
	proi.setPosition(points.getPointPosition(idx))
	rm.addRoi(proi);
2 Likes

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