def blur_and_noise(imp, blur, noise): IJ.run(imp, "Select None", ""); # Blur IJ.run(imp, "Gaussian Blur...", "sigma="+str(blur)); # Noise IJ.run(imp, "RandomJ Poisson", "mean="+str(noise)+" insertion=Modulatory"); imp.close() imp = IJ.getImage() IJ.run(imp, "16-bit", ""); IJ.resetMinAndMax(imp); return(imp)
def blur_and_noise(imp, blur, noise): IJ.run(imp, "Select None", "") # Blur IJ.run(imp, "Gaussian Blur...", "sigma=" + str(blur)) # Noise IJ.run(imp, "RandomJ Poisson", "mean=" + str(noise) + " insertion=Modulatory") imp.close() imp = IJ.getImage() IJ.run(imp, "16-bit", "") IJ.resetMinAndMax(imp) return (imp)
def displayStack(self): IJ.log("Creating virtual stacks from files") FluoStack = None for (DAPIFile, fluoFiles) in self.imageFiles: for fluoFile in fluoFiles: fluoFile = self.outputFluoFolder + os.path.basename(fluoFile) FluoImp = IJ.openImage(fluoFile) if not FluoStack: FluoStack = ImageStack(FluoImp.width, FluoImp.height) (minimum, maximum) = alignImages.getMinMax(FluoImp) FluoImp.setDisplayRange(minimum, maximum) IJ.run(FluoImp, "8-bit", "") FluoStack.addSlice(FluoImp.getProcessor()) FluoStackImp = ImagePlus("Fluo stack", FluoStack) FluoStackImp.setTitle("Fluorescence alignment (8 bit)") FluoStackImp.show() IJ.run(FluoStackImp, "Make Composite", "display=Composite") IJ.resetMinAndMax(FluoStackImp)
size = imp.getNFrames() else: size = imp.getNSlices() for i in xrange(1, size+1): #get imageprocessor for slice ip = stack.getProcessor(i) #show progress! IJ.showProgress(i, size+1) #find the mean using the getMean function, then append it to the list mean = getMean(ip, imp) means.append(mean) IJ.showProgress(1) IJ.resetMinAndMax() #set up the variables for plotting and then plot! x = xrange(1, size + 1) y = means plot = Plot("Illumination intensity stability (" + path.basename(stackpath) + ")", "Frame", "Mean frame intensity", [], []) plot.setLineWidth(1) #plot.setColor(Color.BLACK) plot.addPoints(x, y, Plot.LINE) plot_window = plot.show() def stdev(s): avg = sum(s)*1.0/len(s) variance = map(lambda x: (x-avg)**2, s)
height = ip.getHeight() cal = ip.getCalibration() nip = NewImage.createShortImage(title+"-mask", width, height, ip.getNSlices(), NewImage.FILL_BLACK) index = 1 for c in LimeSeg.allCells: roisX = {} roisY = {} LimeSeg.currentCell=c ct = c.getCellTAt(1) for dn in ct.dots: fac = cal.pixelDepth / cal.pixelWidth z = int(round(dn.pos.z/fac)) print(z) if not z in roisX: roisX[z] = [] if not z in roisY: roisY[z] = [] roisX[z].append(dn.pos.x) roisY[z].append(dn.pos.y) for key in roisX.keys(): roi = PolygonRoi(roisX[key], roisY[key], Roi.POLYGON) roi = PolygonRoi(roi.getConvexHull(), Roi.POLYGON) roi.setPosition(key) nip.setSlice(key) nip.getProcessor().setValue(index) nip.getProcessor().fill(roi) index = index + 1 nip.show() IJ.resetMinAndMax(nip); IJ.run(nip, "3-3-2 RGB", "");
def processImage(filename): # many zeiss images have a pyramid format. In order to speed up the evaluation, we chose only the second highest resolution (=series_2). # depending on your computer/workstation this parameter can be optimized (i.e. chose series_1 if you have a fast computer or series_3 for slow ones) IJ.run( "Bio-Formats", "open=" + filename + " color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT series_2" ) imp = IJ.getImage() imp.show() if (not entertain): imp.show() # preparing memory for saving the results original1 = NewImage.createFloatImage("Original 1", imp.getWidth() / tileSize + 1, imp.getHeight() / tileSize + 1, 1, NewImage.FILL_BLACK) original1Map = original1.getProcessor() original2 = NewImage.createFloatImage("Original 2", imp.getWidth() / tileSize + 1, imp.getHeight() / tileSize + 1, 1, NewImage.FILL_BLACK) original2Map = original2.getProcessor() spotCount1 = NewImage.createFloatImage("Spot count 1", imp.getWidth() / tileSize + 1, imp.getHeight() / tileSize + 1, 1, NewImage.FILL_BLACK) spotCount1Map = spotCount1.getProcessor() spotCount2 = NewImage.createFloatImage("Spot count 2", imp.getWidth() / tileSize + 1, imp.getHeight() / tileSize + 1, 1, NewImage.FILL_BLACK) spotCount2Map = spotCount2.getProcessor() ratio = NewImage.createFloatImage("Ratio", imp.getWidth() / tileSize + 1, imp.getHeight() / tileSize + 1, 1, NewImage.FILL_BLACK) ratioMap = ratio.getProcessor() if (entertain): ratio.show() # go through all tiles for x in range(0, ratioMap.getWidth() - 1): for y in range(0, ratioMap.getHeight() - 1): # crop out the tile from the original images imp.setRoi(Roi(x * tileSize, y * tileSize, tileSize, tileSize)) channel1 = Duplicator().run(imp, 1, 1, 1, 1, 1, 1) channel2 = Duplicator().run(imp, 2, 2, 1, 1, 1, 1) # spot detection spots1 = detectSpots(channel1, spotDetection_GaussianBlur_sigma_GFP, spotDetection_findMaxima_noise_GFP) spots2 = detectSpots(channel2, spotDetection_GaussianBlur_sigma_DAPI, spotDetection_findMaxima_noise_DAPI) # pixel statistics statistics1 = channel1.getStatistics() statistics2 = channel2.getStatistics() # calculate ratio if spots were found s1 = 0 s2 = 0 r = 0 if (spots1 is not None and spots2 is not None): fp1 = spots1.getFloatPolygon() fp2 = spots2.getFloatPolygon() s1 = fp1.npoints s2 = fp2.npoints if (s2 > cutOff_DAPI and s1 > cutOff_GFP): r = 1.0 * s1 / s2 # fill result memory original1Map.setf(x, y, statistics1.mean) original2Map.setf(x, y, statistics2.mean) spotCount1Map.setf(x, y, s1) spotCount2Map.setf(x, y, s2) ratioMap.setf(x, y, r) if (entertain): # show current result image ratio.updateAndDraw() IJ.run(ratio, "Enhance Contrast", "saturated=0.35") # put all results image channels together to one image images = [] images.append(original1) images.append(original2) images.append(spotCount1) images.append(spotCount2) images.append(ratio) resultMap = RGBStackMerge.mergeChannels(images, False) # fix pixel size # factor is multiplied by 2 because ImageJ seems to have a problem when using .czi file series of lower resolution (i.e. series_2); please check for individual cases! factor = (imp.getWidth() / resultMap.getWidth()) * 2 IJ.run( resultMap, "Properties...", "channels=5 slices=1 frames=1 unit=" + imp.getCalibration().getUnit() + " pixel_width=" + str(imp.getCalibration().pixelWidth * factor) + " pixel_height=" + str(imp.getCalibration().pixelHeight * factor) + " voxel_depth=1.0000") IJ.run( ratio, "Properties...", "channels=1 slices=1 frames=1 unit=" + imp.getCalibration().getUnit() + " pixel_width=" + str(imp.getCalibration().pixelWidth * factor) + " pixel_height=" + str(imp.getCalibration().pixelHeight * factor) + " voxel_depth=1.0000") # visualisation resultMap.setC(1) IJ.run(resultMap, "Green", "") IJ.run(resultMap, "Enhance Contrast", "saturated=0.35") resultMap.setC(2) IJ.run(resultMap, "Blue", "") IJ.run(resultMap, "Enhance Contrast", "saturated=0.35") resultMap.setC(3) IJ.run(resultMap, "mpl-inferno", "") IJ.run(resultMap, "Enhance Contrast", "saturated=0.35") resultMap.setC(4) IJ.run(resultMap, "mpl-inferno", "") IJ.run(resultMap, "Enhance Contrast", "saturated=0.35") resultMap.setC(5) IJ.run(resultMap, "mpl-inferno", "") resultMap.show() IJ.resetMinAndMax(resultMap) resultMap.setDisplayMode(IJ.COLOR) IJ.run(ratio, "mpl-inferno", "") IJ.setMinAndMax(ratio, 0, 1) # save result IJ.saveAsTiff(resultMap, filename + "_suitable-name_map.tif") IJ.saveAsTiff(ratio, filename + "_suitable-name_ratio.tif") IJ.run("Close All", "")
frame_unit = 's' else: try: z_interval = ome_meta.getPixelsPhysicalSizeZ(0).value() z_unit = ome_meta.getPixelsPhysicalSizeZ(0).unit().getSymbol() except: z_interval = 1.0 z_unit = 'um' try: str(z_unit) except: z_unit = "um" IJ.run(imp, "8-bit", "") imp.show() IJ.resetMinAndMax() IJ.run("Enhance Contrast", "saturated=0.35") if imp.getNFrames() > imp.getNSlices(): IJ.run( imp, "Label...", "format=0 starting=0 interval=" + str(frame_interval) + " x=5 y=20 font=18 text=[ " + str(frame_unit) + "] range=1-" + str(imp.getNFrames()) + " use_text") else: IJ.run( imp, "Label...", "format=0 starting=0 interval=" + str(z_interval) + " x=5 y=20 font=18 text=[ " + str(z_unit) + "] range=1-" + str(imp.getNSlices()) + " use_text") output_path = os.path.join( output_folder, (os.path.splitext(tiff_file)[0] + " autocontrast avi.avi"))
def measureWidth(numberOfWidthMeasurements): impIn = WindowManager.getCurrentImage() title = impIn.getTitle() IJ.run("Duplicate...", " ") impWork = WindowManager.getCurrentImage() workTitle = impWork.getTitle() IJ.run("Clear Results") IJ.run("Measure") rt = ResultsTable.getResultsTable() min = rt.getValue("Min", 0) IJ.run("Subtract...", "value=" + str(min)) IJ.resetMinAndMax() imp = WindowManager.getCurrentImage() width = imp.getWidth() height = imp.getHeight() IJ.setAutoThreshold(imp, "Mean dark") roiManager = RoiManager.getRoiManager() roiManager.reset() IJ.run("Analyze Particles...", "size=1000-Infinity add") IJ.run("Clear Results") roiManager.select(0) roiManager.runCommand("Measure") angle = rt.getValue("Angle", 0) IJ.run("Select None") roiManager.runCommand("Delete") IJ.run("Rotate... ", "angle="+str(angle)+" grid=1 interpolation=Bilinear enlarge") IJ.setAutoThreshold(imp, "Li dark") IJ.run("Convert to Mask") IJ.run("Fill Holes") roiManager.reset() IJ.run("Analyze Particles...", "size=1000-Infinity add") IJ.run("Clear Results") roiManager.select(0) IJ.run("Clear Outside") IJ.selectWindow(title) IJ.run("Rotate... ", "angle="+str(angle)+" grid=1 interpolation=Bilinear enlarge") roiManager.select(0) roiManager.runCommand("Measure") IJ.run("Select None") IJ.run("Rotate... ", "angle="+str(-angle)+" grid=1 interpolation=Bilinear enlarge") IJ.run("Canvas Size...", "width="+str(width)+" height="+str(height)+" position=Center zero") IJ.selectWindow(workTitle) xBox = rt.getValue("BX", 0) lengthBox = rt.getValue("Width", 0) heightBox = rt.getValue("Height", 0) lengthEllipse = rt.getValue("Major", 0) heightEllipse = rt.getValue("Minor", 0) calibration = imp.getCalibration() xMiddle = int(round(((lengthBox - xBox) / 2.0) + xBox, 0)) xOuter = xBox + lengthBox xInner = xBox widths = zeros('f', numberOfWidthMeasurements) delta = (xOuter - xInner) / (numberOfWidthMeasurements + 1.0) for i in range(1,numberOfWidthMeasurements+1): deltaLen = delta * i; xM = xInner + deltaLen widths[i-1] = getWidthAt(xM, imp) IJ.run("Flatten"); IJ.run("Rotate... ", "angle="+str(-angle)+" grid=1 interpolation=Bilinear enlarge") IJ.run("Canvas Size...", "width="+str(width)+" height="+str(height)+" position=Center zero") impFlat = WindowManager.getCurrentImage() imp.changes = False; imp.close() titleIn = impIn.getTitle() titleFlat = impFlat.getTitle() IJ.selectWindow(titleIn) IJ.run("Add Image...", "image=" + titleFlat + " x=0 y=0 opacity=50 zero") impFlat.changes = False impFlat.close() return widths