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
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); roi_out_imp = ImagePlus("Straightened ROI", roi_straight_stack); egfp_out_imp.show(); mch_out_imp.show();