def calculateThreshold(image, roi, method): if roi != None: bounds = roi.getBounds() stack = image.getStack() newstack = ImageStack(bounds.width, bounds.height) for i in xrange(1, stack.getSize() + 1): ip = stack.getProcessor(i).duplicate() ip.fillOutside(roi) ip.setRoi(roi) c = ip.crop() newstack.addSlice(str(i), c) imp = ImagePlus("ThresholdImage", newstack) else: imp = image thresholder = Auto_Threshold() result = thresholder.exec(imp, method, False, False, True, False, False, True) return result
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 test(): newImg = ImagePlus("GrayScaled", imp) newip = newImg.getProcessor() hist = newip.getHistogram() lowTH = Auto_Threshold.IsoData(hist) newip.setThreshold(lowTH, max(hist), ImageProcessor.BLACK_AND_WHITE_LUT) rt = ResultsTable() pa = ParticleAnalyzer(ParticleAnalyzer.SHOW_RESULTS | ParticleAnalyzer.SHOW_OVERLAY_OUTLINES, Measurements.AREA |Measurements.MEAN |\ Measurements.MEDIAN | Measurements.STD_DEV | Measurements.MIN_MAX | Measurements.RECT, rt,50, 200000, 0.5, 1 ) pa.setResultsTable(rt) pa.analyze(newImg) rt.show("Results")
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 Weka_Segm(dirs): """ Loads trained classifier and segments cells """ """ in aligned images according to training. """ # Define reference image for segmentation (default is timepoint000). w_train = os.path.join(dirs["Composites_Aligned"], "Timepoint000.tif") trainer = IJ.openImage(w_train) weka = WekaSegmentation() weka.setTrainingImage(trainer) # Select classifier model. weka.loadClassifier(str(classifier)) weka.applyClassifier(False) segmentation = weka.getClassifiedImage() segmentation.show() # Convert image to 8bit ImageConverter(segmentation).convertToRGB() ImageConverter(segmentation).convertToGray8() # Threshold segmentation to soma only. hist = segmentation.getProcessor().getHistogram() lowth = Auto_Threshold.IJDefault(hist) segmentation.getProcessor().threshold(lowth) segmentation.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE) segmentation.getProcessor().invert() segmentation.show() # Run Watershed Irregular Features plugin, with parameters. IJ.run(segmentation, "Watershed Irregular Features", "erosion=20 convexity_treshold=0 separator_size=0-Infinity") # Make selection and add to RoiManager. RoiManager() rm = RoiManager.getInstance() rm.runCommand("reset") roi = ThresholdToSelection.run(segmentation) segmentation.setRoi(roi) rm.addRoi(roi) rm.runCommand("Split")
def getThreshold(self, imp, method): thresholder = Auto_Threshold() duplicator = Duplicator() tmp = duplicator.run(imp) return thresholder.exec(tmp, method, False, False, True, False, False, True)
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])