Scripting ImageStatistics

Hi all, I wish to use the ImageStatistics class to get the X and Y coordinated of the center of mass of my image but the script below delivers an error with my options below. An example on the web used the option IS.MEAN to access the ‘mean’ field, is capitalisation important here, and if yes, what is the correct form for xCenterOfMass?


from ij import IJ
from ij.process import ImageStatistics as IS
from ij.process import ImageProcessor

imp = IJ.openImage(“C:\Users\RCole02\Desktop\images\wrong.JPG”)

ip = imp.getProcessor()

options = IS.xCenterOfMass
stats = IS.getStatistics(ip, options, imp.getCalibration())
print "xCenterOfMass: " + str(stats.xcenterofmass)

Capitalization is always important in Java. You may find the naming convention docs useful.

This line is a problem because you imported the ImageStatistics class but do not have an ImageStatistics instance. The class members docs may help with these concepts.

As written, instead of accessing the value of the instance field, you get a reference to the xCenterOfMass field declaration. This is not useful to ImageStatistics.getStatistics method, which is just expecting an integer.

So we have to ask “What integer is expected here?” Well, the Measurements class has integer constants for the various measurements… so this is what we want to use.

A python script illustrating this:

from ij import IJ
from ij.process import ImageStatistics as IS
from ij.measure import Measurements as Measure"Blobs (25K)");

imp = IJ.getImage()

ip = imp.getProcessor()

# Reference the center of mass constant
stats = IS.getStatistics(ip, Measure.CENTER_OF_MASS, imp.getCalibration())
print "xCenterOfMass: " + str(stats.xCenterOfMass)
1 Like

Hi @hinerm thank you for you post, there are some very significant concepts here of which I was unaware.