def apply_autothreshold(hist, method='Otsu'): if method == 'Otsu': lowthresh = Auto_Threshold.Otsu(hist) if method == 'Triangle': lowthresh = Auto_Threshold.Triangle(hist) if method == 'IJDefault': lowthresh = Auto_Threshold.IJDefault(hist) if method == 'Huang': lowthresh = Auto_Threshold.Huang(hist) if method == 'MaxEntropy': lowthresh = Auto_Threshold.MaxEntropy(hist) if method == 'Mean': lowthresh = Auto_Threshold.Mean(hist) if method == 'Shanbhag': lowthresh = Auto_Threshold.Shanbhag(hist) if method == 'Yen': lowthresh = Auto_Threshold.Yen(hist) if method == 'Li': lowthresh = Auto_Threshold.Li(hist) return lowthresh
def auto_threshold(imp_in, str_thresh, bScale=False): """ auto_threshold_otsu(imp_in, str_thresh="Otsu", bScale=True) Compute an autothreshold for an image. Adapted from http://wiki.cmci.info/documents/120206pyip_cooking/python_imagej_cookbook Parameters ---------- imp_in ImagePlus The image to threshold str_thresh String (Default: Default) The threshold: Otsu, Huang, IsoData, Intermodes, Li, MaxEntropy, Mean, MinError, Minimum, Moments, Percentile, RenyiEntropy, Shanbhag, Triangle, Yen, or Default bScale Boolean (Default: False) Return ------ imp_out ImagePlus The binary image thr_val integer The threshold value """ ti = imp_in.getShortTitle() imp = imp_in.duplicate() hist = imp.getProcessor().getHistogram() if (str_thresh == "Otsu"): lowTH = Auto_Threshold.Otsu(hist) elif (str_thresh == "Huang"): lowTH = Auto_Threshold.Huang(hist) elif (str_thresh == "Intermodes"): lowTH = Auto_Threshold.Intermodes(hist) elif (str_thresh == "IsoData"): lowTH = Auto_Threshold.IsoData(hist) elif (str_thresh == "Li"): lowTH = Auto_Threshold.Li(hist) elif (str_thresh == "MaxEntropy"): lowTH = Auto_Threshold.MaxEntropy(hist) elif (str_thresh == "Mean"): lowTH = Auto_Threshold.Mean(hist) elif (str_thresh == "MinError"): lowTH = Auto_Threshold.MinError(hist) elif (str_thresh == "Minimum"): lowTH = Auto_Threshold.Minimum(hist) elif (str_thresh == "Moments"): lowTH = Auto_Threshold.Moments(hist) elif (str_thresh == "Percentile"): lowTH = Auto_Threshold.Percentile(hist) elif (str_thresh == "RenyiEntropy"): lowTH = Auto_Threshold.RenyiEntropy(hist) elif (str_thresh == "Shanbhag"): lowTH = Auto_Threshold.Shanbhag(hist) elif (str_thresh == "Triangle"): lowTH = Auto_Threshold.Triangle(hist) elif (str_thresh == "Yen"): lowTH = Auto_Threshold.Yen(hist) else: lowTH = Auto_Threshold.Default(hist) imp.getProcessor().threshold(lowTH) imp.setDisplayRange(0, lowTH + 1) ImageConverter.setDoScaling(bScale) IJ.run(imp, "8-bit", "") imp.setDisplayRange(0, 255) imp.setTitle(ti + "-bin-" + str_thresh) return ([imp, lowTH])