def process(Destination_Directory, Current_Directory, filename, parameters): """ Rolling ball method. """ print "Processing:" # Opening the image print "Open image file", filename imp = IJ.openImage(os.path.join(Current_Directory, filename)) ip = imp.getProcessor() # Parameters: Image processor, Rolling Ball Radius, Create background, # light background, use parabaloid, do pre smoothing (3x3), # correct corners b = BackgroundSubtracter() b.rollingBallBackground(ip, float(parameters["ballsize"]), ast.literal_eval(parameters["create_b"]), ast.literal_eval(parameters["light_b"]), ast.literal_eval(parameters["parab"]), ast.literal_eval(parameters["smooth"]), ast.literal_eval(parameters["corners"]) ) print "Saving to", Destination_Directory IJ.saveAs(imp, "Tiff", os.path.join(Destination_Directory, filename)) imp.close()
def binarize(f): Prefs.blackBackground = True imp = IJ.openImage(f) if imp: binimp = imp.duplicate() bs = BackgroundSubtracter() ip = binimp.getProcessor() bs.rollingBallBackground(ip, 15, False, False, False, True, True) IJ.run(binimp, "Unsharp Mask...", "radius=2 mask=0.9") def smooth(n=1): for i in range(n): IJ.run(binimp, "Smooth", "") smooth(10) IJ.setAutoThreshold(binimp, "Moments dark") IJ.run(binimp, "Make Binary", "thresholded remaining") smooth(12) IJ.setAutoThreshold(binimp, "Moments white") IJ.run(binimp, "Make Binary", "thresholded remaining") IJ.run(binimp, "Erode", "") IJ.run(binimp, "Dilate", "") IJ.run(binimp, "Analyze Particles...", "size=250-Infinity circularity=0.2-1.00 exclude clear add") binimp.close() else: pass
def apply_rollingball(imp, radius=30, createBackground=False, lightBackground=False, useParaboloid=False, doPresmooth=True, correctCorners=False): # Create BackgroundSubtracter instance bs = BackgroundSubtracter() stack = imp.getStack() # get the stack within the ImagePlus nslices = stack.getSize() # get the number of slices for index in range(1, nslices + 1): ip = stack.getProcessor(index) # Run public method rollingBallBackground bs.rollingBallBackground(ip, radius, createBackground, lightBackground, useParaboloid, doPresmooth, correctCorners) return imp
return SpotDetectionLog(imgBgs32, data, ops, thresholdmethod, dimensions2D, factory) def SpotDetection2(imp): imp=Duplicator().run(imp) # subtract background bgs=BackgroundSubtracter()
def back_substraction(ip1, ip2, radius_background): bgs=BackgroundSubtracter() bgs.rollingBallBackground(ip1, radius_background, False, False, True, True, True) bgs.rollingBallBackground(ip2, radius_background, False, False, True, True, True) imp1 = ImagePlus("ch1 back sub", ip1) imp2 = ImagePlus("ch2 back sub", ip2) return imp1, imp2
return SpotDetectionLog(imgBgs32, data, ops, thresholdmethod, dimensions2D, factory) def SpotDetection2(imp): imp=Duplicator().run(imp) # subtract background
def back_substraction(ip1, ip2, radius_background): bgs=BackgroundSubtracter() bgs.rollingBallBackground(ip1, radius_background, False, False, True, True, True) bgs.rollingBallBackground(ip2, radius_background, False, False, True, True, True) imp1 = ImagePlus("ch1 back sub", ip1) imp2 = ImagePlus("ch2 back sub", ip2) IJ.run(imp1, "Enhance Contrast", "saturated=0.35") IJ.run(imp2, "Enhance Contrast", "saturated=0.35") return imp1, imp2
IJ.run(imp, "Auto Threshold", "method=Triangle white"); return imp def SpotDetection3(imp, invert=False): # operate on a duplicate as not to change the original imp=Duplicator().run(imp) if (invert): IJ.run(imp, "Invert", ""); # subtract background bgs=BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True)
bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) IJ.run(imp, "Auto Threshold", "method=Triangle white"); return imp def SpotDetection3(imp, invert=False): # operate on a duplicate as not to change the original imp=Duplicator().run(imp) if (invert): IJ.run(imp, "Invert", ""); # subtract background bgs=BackgroundSubtracter()
def __localwand(self, x, y, ip, seuil, method, light): self.__image.killRoi() ip.snapshot() if method == "mean" : peak=ip.getPixel(x,y) tol = (peak - self.getMean())*seuil w = Wand(ip) w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED) #print "method=", method, tol, peak elif method == "background" : radius = self.getMinF()/4 bs = BackgroundSubtracter() #rollingBallBackground(ImageProcessor ip, double radius, boolean createBackground, boolean lightBackground, boolean useParaboloid, boolean doPresmooth, boolean correctCorners) bs.rollingBallBackground(ip, radius, False, light, False, True, False) peak=ip.getPixel(x,y) tol = peak*seuil w = Wand(ip) w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED) ip.reset() #print "method=", method, tol, radius, peak else : peak=ip.getPixel(x,y) tol = peak*seuil w = Wand(ip) w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED) #print "method=", method, tol peak=ip.getPixel(x,y) temproi=PolygonRoi(w.xpoints, w.ypoints, w.npoints, PolygonRoi.POLYGON) self.__image.setRoi(temproi) #self.__image.show() #time.sleep(1) #peakip=self.__image.getProcessor() #stats=peakip.getStatistics() temprt = ResultsTable() analyser = Analyzer(self.__image, Analyzer.AREA+Analyzer.INTEGRATED_DENSITY+Analyzer.FERET, temprt) analyser.measure() #temprt.show("temprt") rtValues=temprt.getRowAsString(0).split("\t") area=float(rtValues[1]) intDen=float(rtValues[4]) feret=float(rtValues[2]) mean=intDen/area #time.sleep(2) temprt.reset() self.__image.killRoi() return [peak, area, mean, intDen, feret]
def apply_rollingball(imp, rolling_ball, createBackground, lightBackground, useParaboloid, doPresmooth, correctCorners): ## taken from https://forum.image.sc/t/run-fiji-python-script-with-parameters-on-windows/6162/9 print 'Substracting Background - Radius:', rolling_ball # Create BackgroundSubtracter instance bs = BackgroundSubtracter() # get the stacks stack, nslices = getImageStack(imp) for index in range(1, nslices + 1): ip = stack.getProcessor(index) # Run public method rollingBallBackground bs.rollingBallBackground(ip, rolling_ball, createBackground, lightBackground, useParaboloid, doPresmooth, correctCorners) return imp
def SpotDetectionGray(gray, data, display, ops, invert): # get the dimensions dimensions2D = array([gray.dimension(0), gray.dimension(1)], 'l') factory = gray.getImg().factory() # wrap as ImagePlus imp = ImageJFunctions.wrap(gray, "wrapped") # create and call background subtractor bgs = BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap the result of background subtraction as Img and display it iplus = ImagePlus("bgs", imp.getProcessor()) # if (invert==True): # iplus.getProcessor().invert() imgBgs = ImageJFunctions.wrapByte(iplus) display.createDisplay("back_sub", data.create(ImgPlus(imgBgs))) # convert the background subtracted image to 32 bit temp = ops.run("createimg", factory, FloatType(), dimensions2D) imgBgs32 = ImgPlus(temp) ops.convert(imgBgs32, imgBgs, ConvertPixCopy()) #display.createDisplay("back_sub 32", data.create(ImgPlus(imgBgs32))) # create the Laplacian of Gaussian filter kernel = DetectionUtils.createLoGKernel(3.0, 2, array([1.0, 1.0], 'd')) # apply the log filter and display the result log = ImgPlus(ops.run("createimg", factory, FloatType(), dimensions2D)) ops.convolve(log, imgBgs32, kernel) #display.createDisplay("log", data.create(ImgPlus(log))) # apply the threshold operation #thresholded=ops.run("threshold", ops.create( dimensions2D, BitType()), log, Triangle()) thresholded = ops.run("triangle", log) return ImgPlus(thresholded)
def SpotDetectionGray(gray, data, display, ops, invert): # get the dimensions dimensions2D=array( [gray.dimension(0), gray.dimension(1)], 'l') factory=gray.getImg().factory() # wrap as ImagePlus imp=ImageJFunctions.wrap(gray, "wrapped") # create and call background subtractor bgs=BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap the result of background subtraction as Img and display it iplus=ImagePlus("bgs", imp.getProcessor()) # if (invert==True): # iplus.getProcessor().invert() imgBgs=ImageJFunctions.wrapByte(iplus) display.createDisplay("back_sub", data.create(ImgPlus(imgBgs))) # convert the background subtracted image to 32 bit temp=ops.run( "createimg", factory, FloatType(), dimensions2D ) imgBgs32=ImgPlus( temp ) ops.convert(imgBgs32, imgBgs, ConvertPixCopy() ) #display.createDisplay("back_sub 32", data.create(ImgPlus(imgBgs32))) # create the Laplacian of Gaussian filter kernel = DetectionUtils.createLoGKernel( 3.0, 2, array([1.0, 1.0], 'd' ) ) # apply the log filter and display the result log=ImgPlus( ops.run("createimg", factory, FloatType(), dimensions2D) ) ops.convolve(log, imgBgs32, kernel) #display.createDisplay("log", data.create(ImgPlus(log))) # apply the threshold operation #thresholded=ops.run("threshold", ops.create( dimensions2D, BitType()), log, Triangle()) thresholded = ops.run("triangle", log) return ImgPlus(thresholded)
filters = RankFilters() filters.setup("median", frame) filters.rank(processor, filter_window, filters.MEDIAN) # Perform gamma correction processor.gamma(gamma) frame = ImagePlus("Frame " + str(frame_i), processor) # Rolling ball background subtraction processor = frame.getProcessor() bg_subtractor = BackgroundSubtracter() bg_subtractor.setup("", frame) bg_subtractor.rollingBallBackground(processor, rolling_ball_size/pixel_size, False, False, False, False, True) frame = ImagePlus("Frame " + str(frame_i), processor) # Calibrate pixels calibration = Calibration() calibration.setUnit("pixel") calibration.pixelWidth = pixel_size calibration.pixelHeight = pixel_size calibration.pixelDepth = 1.0 frame.setCalibration(calibration) # Save to output dir file_name = output_dir + "/" + str(frame_i).zfill(4) + ".tif" FileSaver(frame).saveAsTiff(file_name)
ops.convert(red32, red, ConvertPixCopy() ) green32=ImgPlus( ops.create( dimensions2D, FloatType()) ) ops.convert(green32, green, ConvertPixCopy() ) redgreen= ops.add(red32,green32) display.createDisplay("redgreen", data.create(redgreen)) # make a copy of the red + green image copy=redgreen.copy() # wrap as ImagePlus imp=ImageJFunctions.wrap(copy, "wrapped") # create and call background subtractor bgs=BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap as Img and display iplus=ImagePlus("bgs", imp.getProcessor()) print type(imp) imgBgs=ImageJFunctions.wrapFloat(iplus) display.createDisplay("back_sub", data.create(ImgPlus(imgBgs))) kernel = DetectionUtils.createLoGKernel( 3.0, 2, array([1.0, 1.0], 'd' ) ) print type(kernel) print type(imgBgs) print type(red32.getImg()) log = ops.convolve(ops.create( dimensions2D, FloatType()), imgBgs, kernel) display.createDisplay("log", data.create(ImgPlus(log)))
def __calRois(self, imp, indice) : """ Returns the ROIs of a slice given (identified with its n°) in a stack """ ##imp=self.__dictImages[nameimages] # IL FAUT RÉCUPÉRER L'IMAGE DU STACK !!!!! #if self.__batch : imp.hide() #else : imp.show() #imp.hide() imp.show() if self.__batch : imp.hide() imp.setSlice(indice) imp.killRoi() ip = imp.getProcessor() bs=BackgroundSubtracter() #if str(self.__subback) == "0" or str(self.__subback) == "1" : self.__subback = bool(int(self.__subback)) #if self.__subback == True : IJ.run(imp, "Subtract Background...", "rolling="+str(self.__radius)+" light") if self.__subback == True : bs.rollingBallBackground(ip, self.__radius, False, True, False, True, False) if self.__runmacro : imp.show() imp.setSlice(indice) imp.updateAndDraw() IJ.runMacroFile(self.__macropath, imp.getTitle()) imp.updateAndDraw() #if str(self.__manthresh) == "0" or str(self.__manthresh) == "1" : self.__manthresh = bool(int(self.__manthresh)) #if self.__manthresh : IJ.setThreshold(imp, self.__minthr, self.__maxthr) if self.__manthresh : ip.setThreshold(self.__minthr, self.__maxthr, ImageProcessor.RED_LUT) else : self.__setThreshold(imp, indice) rt=ResultsTable() pa1=ParticleAnalyzer(ParticleAnalyzer.SHOW_MASKS+ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES , Measurements.AREA, rt, self.__minArea, self.__maxArea, self.__minCirc, self.__maxCirc) pa1.setHideOutputImage(True) pa1.analyze(imp) masks=pa1.getOutputImage() masks.getProcessor().erode() masks.getProcessor().dilate() masks.getProcessor().invertLut() masks.getProcessor().threshold(1) rm = RoiManager.getInstance() if (rm==None): rm = RoiManager() rm.runCommand("reset") #rm.hide() pa2=ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.CLEAR_WORKSHEET+ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES , Measurements.AREA, rt, self.__minArea, self.__maxArea, self.__minCirc, self.__maxCirc) pa2.analyze(masks) masks.close() temparray=rm.getRoisAsArray() for r in temparray : tempnameroi=r.getName() r.setPosition(indice) r.setName(str(indice)+"-"+tempnameroi) r.setStrokeWidth(1) if len(self.__params) > 0 : for k in self.__params: #if k[0]=="Area": self.__minArea, self.__maxArea = str(k[1]), str(k[2]) if k[0]=="Area": self.__minArea, self.__maxArea = k[1], k[2] for k in self.__params: #if k[0]=="Circ": self.__minCirc, self.__maxCirc = str(k[1]), str(k[2]) if (k[0]=="Circ") and k[3] : self.__minCirc, self.__maxCirc = k[1], k[2] else : self.__minCirc, self.__maxCirc = 0, 1 self.__rr.setRoisarray(temparray, imp) self.__rr.setRange(indice, self.__params) return self.__rr.includeRois else : return temparray
from jarray import array from fiji.plugin.trackmate.detection import DetectionUtils from net.imagej.ops.convert import ConvertPixCopy ############################################################### # Step 1: Rolling ball background subtraction (still uses IJ1) ############################################################### # wrap as ImagePlus imp = ImageJFunctions.wrap(inputData, "wrapped") # create and call background subtractor bgs = BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap the result of background subtraction as Img iplus = ImagePlus("bgs", imp.getProcessor()) imgBgs = ImageJFunctions.wrapShort(iplus) ############################################################### # Step 2: Laplacian of Gaussian Filtering ############################################################### # convert to 32 bit imgBgs32 = ops.run("createimg", imgBgs, FloatType()) ops.convert(imgBgs32, imgBgs, ConvertPixCopy()) # create the Laplacian of Gaussian filter kernel = DetectionUtils.createLoGKernel(3.0, 2, array([1.0, 1.0], 'd'))
composite_img = IJ.openImage(stack) # Get the first DAPI slice dapi = composite_img.createImagePlus() composite_img.close() # happens to get the first slice dapi.setProcessor("nuclei", composite_img.getProcessor().duplicate()) dapi_ip = dapi.getProcessor() if SHOW_IMAGES: dapi.show() # IJ.run("Enhance Local Contrast (CLAHE)", "blocksize=127 histogram=256 maximum=3 mask=*None* fast_(less_accurate)"); FastFlat.getInstance().run(dapi, *[a[1] for a in CLAHE]) # IJ.run("Subtract Background...", "rolling=40"); BS = BackgroundSubtracter() BS.rollingBallBackground(dapi_ip, *[a[1] for a in bkgd_sub]) if SHOW_IMAGES: dapi.updateAndDraw() # IJ.setAutoThreshold("Otsu"); # IJ.run("Convert to Mask"); dapi_ip.autoThreshold() dapi.setProcessor(dapi_ip.convertToByteProcessor(False)) dapi_ip = dapi.getProcessor() # IJ.run("Watershed"); EDM().toWatershed(dapi_ip) dapi_ip.invert() # IJ.run("Analyze Particles...", "size=30-400 show=Masks"); pa_options = particle_analyzer(dapi.getCalibration().pixelWidth) PA = ParticleAnalyzer(*[a[1] for a in pa_options])
from ij import IJ, Prefs from ij.plugin.filter import BackgroundSubtracter Prefs.blackBackground = True imp = IJ.getImage() binimp = imp.duplicate() bs = BackgroundSubtracter() ip = binimp.getProcessor() bs.rollingBallBackground(ip, 15, False, False, False, True, True) IJ.run(binimp, "Unsharp Mask...", "radius=2 mask=0.9") def smooth(n=1): for i in range(n): IJ.run(binimp, "Smooth", "") smooth(10) Prefs.blackBackground = True IJ.setAutoThreshold(binimp, "Moments dark") IJ.run(binimp, "Make Binary", "thresholded remaining") smooth(12) IJ.run(binimp, "Unsharp Mask...", "radius=2 mask=0.9") IJ.setAutoThreshold(binimp, "Moments white") IJ.run(binimp, "Make Binary", "thresholded remaining")