def selectionInterpolateAndFitSpline(roi, interval=1.0, smooth=True): """implement IJ.run(imp, "Interpolate", "interval=1.0 smooth adjust");IJ.run(imp, "Fit Spline", "");""" roi = PolygonRoi(roi.getInterpolatedPolygon(-1.0 * interval, smooth), Roi.FREELINE) if roi.subPixelResolution(): roi = selectionTrimFloatPolygon(roi, roi.getUncalibratedLength()) else: roi = selectionTrimPolygon(roi, roi.getUncalibratedLength()) roi.fitSpline() return roi
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