Image J Jython Question

Hi everyone,

I am working on script in Fiji but I have run into an issue.

Any idea what the Jython function to call ‘Set Measurements’ for Feret’s diameter? I have scoured all the documentation online and have been unable to find what I am looking for. Please let me know if you have any ideas.


Try using the Plugins > Macro > Recorder to find command syntax.

In your Jython script it might look like this:

from ij import IJ“Set Measurements…”, “feret’s redirect=None decimal=3”)

Thanks for the quick response! It works perfectly.

This is the order of operations I complete it my script.

  1. Load the image
  2. Set it to 8 bit grayscale
  3. Do an automatic threshold
  4. Measure the Ferets Diameter

Next I need to use the ‘analyze particles’ macro. I recorded the macro and it works however it returns me with the message “A thresholded image or 8-bit binary image is required. Threshold levels can be set using the Image->Adjust->Threshold tool.”

I am assuming that this is because it does not convert my image to 8 bit/ threshold it before it runs that macro.

Any ideas on how to get around this?

Appreciate the help



when scripting with jython you’re going to get to know the java API quite well. You can use some_string) but eventually you need to call the functions directly.

Here’s a quick script that does what you outlined (I’ve left out the load part and just grabbed the active image)



def main():
	# grab the active image
	from ij import IJ
	imp = IJ.getImage()

	# make a copy of the image as we are going to threshold it
	ip = imp.getProcessor().duplicate()

	# threshold the image
	import ij.process.AutoThresholder.Method as ATM
	ip.setAutoThreshold(ATM.Otsu, False)  # Change OTSU to whatever you'd like
	bp = ip.createMask()                  # create a mask from the image

	# put this mask into an imageplus (needed to the particle analyzer)
	from ij import ImagePlus
	imp_mask = ImagePlus("mask",bp)  # uncomment this line to see the mask

	# set up the particle analuser
	from ij.plugin.filter import ParticleAnalyzer
	from ij.measure import Measurements, ResultsTable
	from java.lang import Double
	rt = ResultsTable() # empty results table

	# theses are the measurements you request 
	imeas = sum( [getattr(Measurements,meas) for meas in "CIRCULARITY AREA FERET  CENTROID ".split()])

	# Run the particle analyzer                                      min size, max size,    min circ, max circ
	pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, imeas , rt, 0, Double.POSITIVE_INFINITY, 0.0, 1.0) 

if( __name__ == '__builtin__'):

Hi Chris,

Wow, great answer, thank you for taking the time, I learned some neat stuff. That api you refer to would be more useful if it had code examples and some documentation as to how it was intended to be used. It’s interesting you included ‘circularity’ as a measurement because I could not include that one by macro recording. For the uninitiated I would recommend macro recording as the best first place to find some usable code. Here’s the same thing (except circularity!) with fewer function imports. -John

from ij import IJ

# mac or linux path
exampleImage = "/Users/Desktop/example_dapi.tif"

# grab the active image
imp = IJ.getImage()"Duplicate...", "duplicate")
imp2 = IJ.getImage()

# threshold the image, "Auto Threshold", "method=Otsu")

# create a mask from the image
mask = imp2.createThresholdMask()

# put this mask into an imageplus (needed to the particle analyzer)
from ij import ImagePlus
maskImp = ImagePlus("Mask", mask) # uncomment this line to see the mask

# these are the measurements you request"Set Measurements...", "area centroid feret's redirect=None decimal=3")

# run particle analyzer, "Analyze Particles...", "size=0-Infinity circularity=0.0-1.00 show=Nothing display clear add")
# get the measurements, "Measure", "")

# not necessary to replicate other script gui output, but it is for further programming with resulting values
from ij.measure import ResultsTable
rt = ResultsTable.getResultsTable()"Results")

Here are some helpful docs: