roi.getStatistics() returning NaN

ROI.getStatistics() is returning NaN values?

I used StarDist to create a label image with a number of coloured cells on a black background. Each cell is associated with a corresponding roi which is stored in the Roi Manager. However when I try to get statistics for these ROIs in a JavaScript macro, everything except area returns NaN.

var rm = RoiManager.getRoiManager();
var rois = rm.getRoisAsArray();
for(var i=0; i<rois.length; i++) {
  print(rois[i].getStatistics()); //  stats[count=86, mean=NaN, min=NaN, max=NaN]
}
print(rois[0].area) // 86
print(rois[0].mean // NaN
print(rois[0].areaFraction) // NaN - should be 95.

Using the GUI “Measure” function in the Roi Manager lists the values correctly. Rewriting in .ijm format also worked as expected and returned numerical values:

for(i=0; i<roiManager("count"); i++) {
  roiManager("select", i);
  someValue = getValue("%Area"); // someValue = 95
}

Managed to find a work around, but is this a bug in the JavaScript API, or am I incorrectly implementing the code?
Thanks

Hi,

the ROI doesn’t know from which image to take the intensity values from.
To verify this: print(rois[0].getImage()).

If you associate an image with the ROI, it should work:
rois[i].setImage(imp);
To be added inside the loop, before the getStatistics line.

3 Likes

Hi Noreen,

This was indeed the problem and adding rois[i].setImage(imp) within the loop solved my issue.
Thanks for your help! :slight_smile:

Best,
Peter