def save_qc_image(imp, rois, output_path): """save rois overlaid on imp to output_path""" imp.killRoi() roim = RoiManager(False) for roi in rois: roim.addRoi(roi) roim.runCommand("Show All with labels") RGBStackConverter.convertToRGB(imp) roim.moveRoisToOverlay(imp) FileSaver(imp).saveAsTiff(output_path) roim.runCommand("Show None") roim.close()
def segmentation(imp, spot_data, channel, diameter_init, ES_tolerance, ES_area_max, ES_ctrl_pts, ES_iteration, repeat_max): # Open files cal = imp.getCalibration() manager = RoiManager.getInstance() if manager is None: manager = RoiManager() # Prepare log files for output options = IS.MEDIAN | IS.AREA | IS.MIN_MAX | IS.CENTROID | IS.PERIMETER | IS.ELLIPSE | IS.SKEWNESS convergence = [] Sintensity = [] for spot in spot_data: repeat = 0 flag = False spotID = int(spot[0]) Xcenter = (float(spot[1]) / cal.pixelWidth) Ycenter = (float(spot[2]) / cal.pixelHeight) Quality = float(spot[3]) diameter_init = float(spot[4] / cal.pixelWidth) * 2.0 while True: manager = RoiManager.getInstance() if manager is None: manager = RoiManager() Xcurrent = int(Xcenter - diameter_init / 2.0) Ycurrent = int(Ycenter - diameter_init / 2.0) Dcurrent1 = int(diameter_init * (1.2 - repeat / 10.0)) Dcurrent2 = int(diameter_init * (0.8 + repeat / 10.0)) roi = OvalRoi(Xcurrent, Ycurrent, Dcurrent1, Dcurrent2) imp.setPosition(channel) imp.setRoi(roi) Esnake_options1 = "target_brightness=Bright control_points=" + \ str(ES_ctrl_pts) + " gaussian_blur=0 " Esnake_options2 = "energy_type=Contour alpha=2.0E-5 max_iterations=" + \ str(ES_iteration) + " immortal=false" IJ.run(imp, "E-Snake", Esnake_options1 + Esnake_options2) roi_snake = manager.getRoisAsArray() roi_ind = len(roi_snake) - 1 stats = IS.getStatistics( imp.getProcessor(), options, imp.getCalibration()) perimeter = roi_snake[roi_ind].getLength() * cal.pixelWidth circularity = 4.0 * 3.1417 * (stats.area / (perimeter * perimeter)) if stats.area > 17.0 and stats.area < ES_area_max and stats.skewness < -0.01 and circularity > 0.01 and stats.minor > 2.0 and boundaries(Xcenter, Ycenter, stats.xCentroid / cal.pixelWidth, stats.yCentroid / cal.pixelHeight, ES_tolerance): Sintensity = stats.median convergence.append(True) break if stats.median > 6000 and stats.area > 17.0 and stats.area < ES_area_max: Sintensity = stats.median convergence.append(True) break elif repeat > repeat_max: manager.select(imp, roi_ind) manager.runCommand(imp, 'Delete') roi = OvalRoi(Xcenter + 1.0 - diameter_init / 2.0, Ycenter + 1.0 - diameter_init / 2.0, diameter_init, diameter_init) imp.setRoi(roi) manager.add(imp, roi, spotID) roi_snake.append(roi) stats = IS.getStatistics( imp.getProcessor(), options, imp.getCalibration()) Sintensity = stats.median convergence.append(False) break else: IJ.log('Area=' + str(stats.area) + ' Skewness=' + str(stats.skewness) + ' circularity=' + str(circularity) + ' Minor=' + str(stats.minor)) manager.select(imp, roi_ind) manager.runCommand(imp, 'Delete') repeat += 1 # End Spot-segmentation # End all Spots-segmentation manager.runCommand(imp, 'Show All') imp.setPosition(channel) color = imp.createImagePlus() ip = imp.getProcessor().duplicate() color.setProcessor("segmentation" + str(channel), ip) color.show() IJ.selectWindow("segmentation" + str(channel)) manager.moveRoisToOverlay(color) spot_optimal = manager.getRoisAsArray() manager.reset() for i in xrange(0, len(spot_optimal)): spot = spot_optimal[i] spot.setStrokeWidth(2) if convergence[i]: spot.setStrokeColor(Color.GREEN) else: spot.setStrokeColor(Color.MAGENTA) imp.setRoi(spot) manager.add(imp, spot, i) manager.runCommand(imp, 'Show All') imp.setPosition(channel)
def Overlayer(org_size, dirs): """ Overlays ROIs with appropriate color, saves to .tif and animates aligned images to .gif """ # Get colors. Colors, Colors_old = colorlist() # Get ROImanager. rm = RoiManager().getInstance() rois = rm.getCount() # Overlays ROI on aligned images, converts to 8-bit (for gif). for root, directories, filenames in os.walk(dirs["Composites_Aligned"]): for filename in filenames: imp = IJ.openImage(os.path.join(root, filename)) converter = ImageConverter(imp) converter.setDoScaling(True) converter.convertToGray8() # Lookup table and local contrast enhancement for vizualisation. IJ.run(imp, "Rainbow RGB", "") IJ.run(imp, "Enhance Local Contrast (CLAHE)", "blocksize=127 histogram=256 maximum=3 mask=*None*") for roi in range(rois): roi_obj = rm.getRoi(roi) roi_obj.setStrokeWidth(2) if roi < 19: roi_obj.setStrokeColor(Color(*Colors[roi][0:3])) else: roi_obj.setStrokeColor(eval(Colors_old[roi])) rm.moveRoisToOverlay(imp) IJ.saveAs(imp, "Tiff", os.path.join(dirs["Overlays"], filename)) # Opens overlaid images, saves as tiff stack. overlay_stack = IJ.run("Image Sequence...", "open="+dirs["Overlays"]+ " number=3040 starting=0 increment=1 scale=300 file=.tif sort") # Takes care of spaces in titles. tiftitle = Title.replace(" ", "_") tiftitle = tiftitle.replace(".", "_") # Gets dimensions for scalebar. imp = WindowManager.getImage("Overlays") dimensions = imp.getDimensions() size = dimensions[0] + dimensions[1] microns = org_size / size # Sets scale and writes scale-bar, flattens overlays. IJ.run(imp, "Set Scale...", "distance=1 known=" +str(microns)+" pixel=1 unit=micron") IJ.run(imp, "Scale Bar...", "width=10 height=4 font=14 color=Yellow background=None location=[Lower Right] bold overlay") IJ.run(imp, "Flatten", "stack") IJ.saveAs(imp, "Tiff", os.path.join(dirs["Gifs"], tiftitle)) # Animates tiff stack from directoy. for root, directories, filenames in os.walk(dirs["Gifs"]): for filename in filenames: if tiftitle in filename and filename.endswith(".tif"): # set=xx parameter controls gif speed. # for additional parameters run with macro recorder. try: print "Animating gif..." imp = WindowManager.getImage(tiftitle + ".tif") gif = IJ.run("Animated Gif ... ", "set=200 number=0 filename=" + os.path.join(dirs["Gifs"], tiftitle + ".gif")) except Exception, e: print str(e) print "gif animated."