I have a 16-bit z-stack from which I would like return the value of the lower threshold as calculated by an AutoThreshold method (e.g. Otsu) in a python script.
in an IJ-macro, I could do:
run("T1 Head (2.4M, 16-bits)"); setAutoThreshold("Otsu dark stack"); getThreshold(lower, upper); resetThreshold(); print(lower);
which returns 146 for the lower threshold using the Otsu method.
But I have not managed to reproduce this script in python. There seems to be no “IJ.getThreshold”, and as I did not find any alternative, I only managed to get as far as this:
from ij import IJ imp = IJ.openImage("http://imagej.nih.gov/ij/images/t1-head.zip") IJ.setAutoThreshold(imp, "Otsu dark stack") imp.show()
This works so far, but I am missing the step to retrieve the actual threshold value.
I also tried to use AutoThresholder() class, but the value it returns does not match the above:
from ij import IJ from ij import ImagePlus from ij.process import StackStatistics from ij.process import AutoThresholder imp = IJ.openImage("http://imagej.nih.gov/ij/images/t1-head.zip"); stackstats = StackStatistics(imp) stack_histogram = stackstats.histogram autothresh = AutoThresholder() threshold = autothresh.getThreshold("Otsu", stack_histogram) print threshold
which returns 41.
Does somebody know what I am missing? Thanks for your help!