How to extract value of a column in Results table using Python

I have the following code:


from ij.plugin.frame import RoiManager
from ij.measure import ResultsTable
from ij import IJ

roi_file = "RoiSet_test.zip"
image_path = "./"
rm = RoiManager()
rm.runCommand("Open", roi_file)
n = rm.getCount() 

i = -1
for roi in rm:
    i = i + 1
    roi_name = roi.getName()
    image_file = image_path +   roi_name + ".png"
    imp = IJ.openImage(image_file)
    imp.show()
    # Select area based on ROI
    rm.select(i)
    # Measure it
    IJ.run(imp, "Measure", "");

    # It breaks here, where I tried to get the value of Mean column
    bg_intensity = ResultsTable.getValue("Mean",i )

    # we want to process 1st ROI only as example hence break here.
    break

What I tried to do is to loop over the ROI list.
Measure the area based on the position defined in the ROI coordinate.
Store the Mean value as bg_intensity variable, this is done by getting the value from the Result table.

As noted above the code failed on this line
bg_intensity =ResultsTable.getValue("Mean",i, i )

What’s the right way to do it?

I get the following error:

TypeError: getValue(): self arg can't be coerced to 
ij.measure.ResultsTable

You need to get the current instance of the ResultTable, while with

bg_intensity = ResultsTable.getValue("Mean", i )

you are calling the function getValue with the general class, without adresing a specific table
so first

table = ResultsTable.getResultsTable() # the table currently open
bg_intensity = table.getValue("Mean", i)

In the API the static methods are the ones you can call directly with the class, while with the instance method you first need an object of this class to call the function.

1 Like