ThresholdAdjuster Jython macro problem



I am developing Fiji macro written in Jython and I have a big problem with ThresholdAdjuster class.
In my macro I analyse series of images, and I would like to use ThresholdAdjuster on the first one and apply the chosen value for the rest of the images. That is I would like to choose it manually only on the first image. However I have three problems:

  1. I have no idea how to read the threshold value from ThresholdAdjuster
  2. How can I close the theshold window once Apply button is clicked?
  3. If I change the default threshold (that is if I move the bar) even when i close the threshold window my macro stops working. Is there any command to make in continue working?

This part of code looks like this:

                            <----- I would like to read threshold value here, e.g: thresh_val = ThresholdAdjuster.GetValue()
imp.setRoi(roi2) <-----This does not execute once threshold is changed!
ip = imp.getProcessor()

I appreciate any help because I really need to fix this asap.



Dear @MaciejG,
I think that you need to pass by ImageProcessor and WaitForUserDialog API to achieve your task.

This is a simple Jython code that does that:

  1. open an Image
  2. ask the user to set the initial thresholds (to do that I usually use“Threshold…”))
  3. get those values via imageprocessor.getMinThreshold/getMaxThreshold
  4. log them out
  5. open a new image and set the same threshold values

This is the code:

from ij import IJ,
from ij.gui import WaitForUserDialog

imp = IJ.openImage()"Threshold...")
# this is the method to wait the user set up the threshold
WaitForUserDialog("Threshold","set a threshold").show()             

#to get the threshold you could use imageProcessor API 
thres_min = imp.getProcessor().getMinThreshold()
thres_max = imp.getProcessor().getMaxThreshold()
IJ.log("min threshold : "+str(thres_min))
IJ.log("max threshold : "+str(thres_max))

#now you can set to another image using IJ.setThreshold(imp, thres_min, thres_max);  
imp_new = IJ.openImage()
IJ.setThreshold(imp_new, thres_min, thres_max);  

does this help you?

Have a nice day,
Emanuele Martini


Dear emartini,

Thank you very much for help and your time.
However something strange is going on here. I have copied your macro and it shows 255 for both thres_min and thres_max. Honestly I have no idea why is that.
One more question - do you know how to automatically close threshold window once “Apply” is clicked?

kind regards,

EDIT: OK I found the answer for the first question. Once “apply” is clicked it shows 255 for both values. So “OK” in dialog window should be clicked first. Unfortunately that functionality is not the best, but still better than nothing:)

EDIT2: I got it working! It does all I need now. Thank you very much for your help, you’re the best:)


I am happy that it helped you :wink:

I struggled with those things some time ago and now I am glad to avoid the struggling to other people.

Have nice day,