def maximum_line_profile(imp, roi, pixel_width): """return a line profile taking the maximum value over n pixels perpendicular to roi line""" imp.setRoi(roi) IJ.run(imp, "Interpolate", "interval=1.0 smooth adjust") if pixel_width < 1: pixel_width = 1 pixel_width = int(2 * math.ceil(float(pixel_width) / 2)) ip = Straightener().straightenLine(imp, pixel_width) # debug #from ij import ImagePlus #from ij.gui import WaitForUserDialog #debug_imp = ImagePlus("debug", ip); #debug_imp.show(); #WaitForUserDialog("debug").show(); #debug_imp.close(); width = ip.getWidth() height = ip.getHeight() max_profile = [] poly = roi.getInterpolatedPolygon(1.0, True) for idx, (x, y) in enumerate(zip(poly.xpoints, poly.ypoints)): pix = ip.getLine(idx, 0, idx, height) max_profile.append(((x, y), max(pix))) return max_profile
def straighten_vessel(imp, smooth_centres, it=1, save_output=False): """use IJ straigtening tool to deal with convoluted vessels""" print("straighten vessel input image dims = " + str(imp.getWidth()) + "x" + str(imp.getHeight())) rot_imp = utils.rot3d(imp, axis='x') if it == 1: roi = PolygonRoi([x for x, y, z in smooth_centres], [z for x, y, z in smooth_centres], Roi.FREELINE) print("len interp polygon = " + str(roi.getInterpolatedPolygon().npoints)) elif it == 2: new_zs = [z for z in range(rot_imp.getWidth())] new_ys = lin_interp_1d([z for x, y, z in smooth_centres], [y for x, y, z in smooth_centres], new_zs) roi = PolygonRoi(new_zs, new_ys, Roi.FREELINE) split_ch = ChannelSplitter().split(rot_imp) mch_imp = split_ch[0] egfp_imp = split_ch[1] roi_imp = split_ch[2] roi_imp.setRoi(roi) for zidx in range(egfp_imp.getNSlices()): for chidx in range(3): split_ch[chidx].setZ(zidx + 1) split_ch[chidx].setRoi(roi) ip = Straightener().straightenLine(split_ch[chidx], 150) if chidx == 1: if zidx == 0: egfp_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()) egfp_straight_stack.addSlice(ip) elif chidx == 0: if zidx == 0: mch_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()) mch_straight_stack.addSlice(ip) else: if zidx == 0: roi_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()) roi_straight_stack.addSlice(ip) egfp_out_imp = ImagePlus("Straightened EGFP", egfp_straight_stack) mch_out_imp = ImagePlus("Straightened mCh", mch_straight_stack) roi_out_imp = ImagePlus("Straightened ROI", roi_straight_stack) if it == 2: egfp_out_imp = utils.rot3d(egfp_out_imp, axis='y') mch_out_imp = utils.rot3d(mch_out_imp, axis='y') roi_out_imp = utils.rot3d(roi_out_imp, axis='y') egfp_out_imp.show() mch_out_imp.show() roi_out_imp.show() IJ.run( "Merge Channels...", "c1=[" + mch_out_imp.getTitle() + "] c2=[" + egfp_out_imp.getTitle() + "] c7=[" + roi_out_imp.getTitle() + "] create keep") # WaitForUserDialog("pause").show(); # if it==1: egfp_out_imp.close() mch_out_imp.close() roi_out_imp.close() new_composite = IJ.getImage() if save_output: FileSaver(new_composite).saveAsTiffStack( os.path.join(output_path, "after rotation " + str(it) + ".tif")) return new_composite
def process_image(imps, rois, ais_chno, nucleus_chno, bg_roino=3, sample_width=3, method='mean', dilations=3, average=1, threshold=0.1): """Opens a file and applies a Gaussian filter.""" orig_title = imps[ais_chno-1].getTitle() print ",".join([i.getTitle() for i in imps]) print "Processing", orig_title options = IS.MEAN | IS.MEDIAN # many others nucleus_imp = imps[nucleus_chno-1].duplicate() IJ.run(nucleus_imp, "Median...", "radius=10") IJ.setAutoThreshold(nucleus_imp, "Default"); IJ.run(nucleus_imp, "Make Binary", "") IJ.run(nucleus_imp, "Invert", "") IJ.run(nucleus_imp, "Options...", "iterations=1 count=1 black do=Nothing"); IJ.run(nucleus_imp, "Watershed", ""); IJ.run(nucleus_imp, "Analyze Particles...", "size=20-Infinity clear add"); rm = RoiManager.getInstance2() nuclei = rm.getRoisAsArray() ais_imp = imps[ais_chno-1].duplicate() print ais_imp.getTitle() IJ.run(ais_imp, "8-bit","") IJ.run(ais_imp, "Median...", "radius=1") bg = 0 for i in range(bg_roino): bg_roi = rois[i] ais_imp.setRoi(bg_roi) stats = ais_imp.getStatistics(options) bg += stats.mean print "Bg Roi %s, %s: %s" % (bg_roi.getName(), bg_roi, stats) background = (int)(bg / bg_roino) results = [] for i in range(bg_roino, len(rois)): roiresult = {} print i, rois[i].getName() mimp = ais_imp.duplicate() mimp.setTitle("%s-%s-AIS-Skeleton" % (orig_title, rois[i].getName())) # IJ.run(mimp, "Median...", "radius=3") mimp.setRoi(rois[i]) # IJ.setAutoThreshold(mimp, "Huang dark") IJ.run(mimp, "Auto Local Threshold", "method=Phansalkar radius=15 parameter_1=0 parameter_2=0 white") IJ.setBackgroundColor(0,0,0) IJ.run(mimp, "Clear Outside", "") Prefs.blackBackground = True for j in range(dilations): IJ.run(mimp, "Dilate", "") IJ.run(mimp, "Skeletonize", "") #IJ.run(mimp, "Analyze Skeleton (2D/3D)", "prune=none prune_0 calculate show display"); ais_skeleton, ais_points, points, orthogonals, ais_roi, nucleus_roi = create_skeleton(mimp, '%s-%s-AIS' % (orig_title, rois[i].getName()), nuclei_rois=nuclei, sample_width=sample_width) if ais_skeleton is None: print "Skipping -- AIS skeleton segmentation failed for ROI", rois[i].getName() continue #images['ais-skeleton-' + rois[i].getName()] = ais_skeleton # subtract background print "Subtracting background: bg=%d" % background IJ.run(ais_imp, "Subtract...", "value=%d" % int(background)) ais_imp.setRoi(ais_roi) ip = Straightener().straightenLine(ais_imp, sample_width) straight_imp = ImagePlus('%s-%s-AIS-Straight' % (orig_title, rois[i].getName()), ip) straight_imp.setCalibration(imps[ais_chno-1].getCalibration().copy()) IJ.run(straight_imp, "Green Fire Blue", "") roiresult = { 'roi-name': rois[i].getName(), 'ais-image': straight_imp, 'ais-roi': ais_roi, 'nucleus-roi': nucleus_roi, } # plot if len(points) > 1: if method == 'sum': threshold *= sample_width plot, rt = create_plot(straight_imp, method, average, threshold=threshold) roiresult['plot'] = plot roiresult['table'] = rt results.append(roiresult) return results, background
#roi = zproj_imp.getRoi(); zproj_imp.changes = False; zproj_imp.close(); z_planes = egfp_imp.getNSlices(); #straight_stack = Straightener().straightenStack(egfp_imp, roi, 100); #out_imp = ImagePlus("Straightened", straight_stack); for zidx in range(z_planes): for chidx in range(3): #print("Working on " + str(["egfp", "mCh", "roi"][chidx])); split_ch[chidx].setZ(zidx+1); split_ch[chidx].setRoi(roi); ip = Straightener().straightenLine(split_ch[chidx], 150); if chidx==1: if zidx==0: egfp_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()); egfp_straight_stack.addSlice(ip); elif chidx==0: if zidx==0: mch_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()); mch_straight_stack.addSlice(ip); else: if zidx==0: roi_straight_stack = ImageStack(ip.getWidth(), ip.getHeight()); roi_straight_stack.addSlice(ip); egfp_out_imp = ImagePlus("Straightened EGFP", egfp_straight_stack); mch_out_imp = ImagePlus("Straightened mCh", mch_straight_stack);
shell_width = 10 imp = IJ.getImage() overlay = imp.getOverlay() raster_shells = [] if imp and overlay: for i in range(overlay.size()): roi = overlay.get(i) if roi.getName() and "Shell" in roi.getName(): imp.setRoi(roi) IJ.run(imp, "Area to Line", "") raster = ImagePlus( roi.getName(), Straightener().straighten(imp, roi, shell_width)) raster_shells.append(raster) if raster_shells: holding_imp = ImagesToStack.run(raster_shells) IJ.run(holding_imp, "Make Montage...", "columns=1 rows={} scale=1".format(holding_imp.getNSlices())) measurable_imp = IJ.getImage() measurable_imp.setTitle("Rasterized Shells") IJ.setAutoThreshold(measurable_imp, "Default dark") IJ.run(measurable_imp, "Analyze Particles...", " show=[Overlay Masks] display clear overlay") else: error() else: error()