distMin = dist return centPart, (x, y) def isSpecialParticle(image, roim, iParticle, boundariesNuc, boundariesMito, pixelSize, z): try: image.setSlice(int(round(z))) image2 = ImagePlus("slice",image.getProcessor()) roim.select(image2, iParticle) image3 = Duplicator().run(image2); image3.show() sp = segment_Particles() image3 = sp.preprocessImage(image3, "MAX", "C1", 0, 0) image3 = sp.segmentParticles(image3, "Triangle", boundariesNuc[0], boundariesNuc[1], 0, 0) if roim.getCount() > 0: roim.select(image3, 0) stats = image3.getStatistics(Measurements.AREA + Measurements.SHAPE_DESCRIPTORS) IJ.log("Area (um2) " + str(stats.area*pixelSize*pixelSize) + "; Aspect Ratio " + str(stats.major/stats.minor)) if stats.area > boundariesMito[0] and stats.area < boundariesMito[1] and stats.major/stats.minor > boundariesMito[2]: return 1 else: return 0 except BaseException, err:
# analyse objects and put results in table IJ.run("Set Measurements...", "area display redirect=None decimal=5") IJ.run(imp, "Analyze Particles...", "exclude add") manager = RoiManager.getRoiManager() manager.runCommand("Save", file.toString() + ".RoiSet.zip") for roi in manager.getRoisAsArray(): copy.setRoi(roi) copy.show() IJ.run(copy, "Enlarge...", "enlarge=-2") # analyse the image statistics = copy.getStatistics() print("mean: " + str(statistics.mean)) print("area:" + str(statistics.area)) # write the analysed valurs in a table table = ResultsTable.getResultsTable() table.incrementCounter() table.addValue("filename", file.toString()) table.addValue("mean_robert", statistics.mean) table.addValue("area", statistics.area) table.addValue("total instensity", statistics.mean * statistics.area) table.show("Results") # dirty hack to make the program stop here: # x = 1 / 0
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", "")