def threshold_stack(imp, thvalue): # get the stacks stack, nslices = ImageTools.getImageStack(imp) for index in range(1, nslices + 1): ip = stack.getProcessor(index) # get the histogramm hist = ip.getHistogram() lowth = Auto_Threshold.Otsu(hist) ip.threshold(thvalue) return imp
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
filename = od.getFileName() directory = od.getDirectory() path = od.getPath() print filename print directory print path imp = IJ.openImage(path) imp.show() IJ.run(imp, "8-bit", "") imp = IJ.getImage() hist = imp.getProcessor().getHistogram() lowTH = Auto_Threshold.Otsu(hist) print lowTH imp.getProcessor().threshold(lowTH) #pulled from http://wiki.cmci.info/documents/120206pyip_cooking/python_imagej_cookbook#pluginauto_threshold #imp2 = IJ.getImage() imp.show() IJ.run("Watershed") IJ.run("Set Measurements...", "area min redirect=None decimal=3") IJ.run("Analyze Particles...", "display") IJ.run( imp, "Properties...", "channels=1 slices=1 frames=1 unit=inch pixel_width=1 pixel_height=1 voxel_depth=1.0000000 global" )
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])