def render_scene(scene, window_size, interactor, output, silent): """ Render a scene. If a output is supplied, a snapshot of the rendered scene is taken. """ if not silent: showm = window.ShowManager(scene, size=window_size, reset_camera=False, interactor_style=interactor) showm.initialize() showm.start() if output: snapshot(scene, output, size=window_size)
def render_scene(scene, window_size, interactor, output, silent, title='Viewer'): """ Render a scene. If a output is supplied, a snapshot of the rendered scene is taken. Parameters ---------- scene : window.Scene() 3D scene to render. window_size : tuple (width, height) The dimensions for the vtk window. interactor : str Specify interactor mode for vtk window. Choices are image or trackball. output : str Path to output file. silent : bool If True, disable interactive visualization. title : str, optional Title of the scene. Defaults to Viewer. """ if not silent: showm = window.ShowManager(scene, title=title, size=window_size, reset_camera=False, interactor_style=interactor) showm.initialize() showm.start() if output: snapshot(scene, output, size=window_size)
def display_slices(volume_actor, slices, output_filename, axis_name, view_position, focal_point, peaks_actor=None, streamlines_actor=None): # Setting for the slice of interest if axis_name == 'sagittal': volume_actor.display(slices[0], None, None) if peaks_actor: peaks_actor.display(slices[0], None, None) view_up_vector = (0, 0, 1) elif axis_name == 'coronal': volume_actor.display(None, slices[1], None) if peaks_actor: peaks_actor.display(None, slices[1], None) view_up_vector = (0, 0, 1) else: volume_actor.display(None, None, slices[2]) if peaks_actor: peaks_actor.display(None, None, slices[2]) view_up_vector = (0, 1, 0) # Generate the scene, set the camera and take the snapshot scene = window.Scene() scene.add(volume_actor) if streamlines_actor: scene.add(streamlines_actor) elif peaks_actor: scene.add(peaks_actor) scene.set_camera(position=view_position, view_up=view_up_vector, focal_point=focal_point) snapshot(scene, output_filename, size=(1920, 1080), offscreen=True)
def plot_proj_shell(ms, use_sym=True, use_sphere=True, same_color=False, rad=0.025, opacity=1.0, ofile=None, ores=(300, 300)): """ Plot each shell Parameters ---------- ms: list of numpy.ndarray bvecs for each bvalue use_sym: boolean Plot symmetrical vectors use_sphere: boolean rendering of the sphere same_color: boolean use same color for all shell rad: float radius of each point opacity: float opacity for the shells ofile: str output filename ores: tuple resolution of the output png Return ------ """ global vtkcolors if len(ms) > 10: vtkcolors = fury.colormap.distinguishable_colormap(nb_colors=len(ms)) scene = window.Scene() scene.SetBackground(1, 1, 1) if use_sphere: sphere = get_sphere('symmetric724') shape = (1, 1, 1, sphere.vertices.shape[0]) fid, fname = mkstemp(suffix='_odf_slicer.mmap') odfs = np.memmap(fname, dtype=np.float64, mode='w+', shape=shape) odfs[:] = 1 odfs[..., 0] = 1 affine = np.eye(4) sphere_actor = actor.odf_slicer(odfs, affine, sphere=sphere, colormap='winter', scale=1.0, opacity=opacity) scene.add(sphere_actor) for i, shell in enumerate(ms): if same_color: i = 0 pts_actor = actor.point(shell, vtkcolors[i], point_radius=rad) scene.add(pts_actor) if use_sym: pts_actor = actor.point(-shell, vtkcolors[i], point_radius=rad) scene.add(pts_actor) window.show(scene) if ofile: filename = ofile + '.png' snapshot(scene, filename, size=ores)
def main(): parser = _build_arg_parser() args = parser.parse_args() # The number of labels maps must be equal to the number of bundles tmp = args.in_bundles + args.in_labels args.in_labels = args.in_bundles[(len(tmp) // 2):] + args.in_labels args.in_bundles = args.in_bundles[0:len(tmp) // 2] assert_inputs_exist(parser, args.in_bundles + args.in_labels) assert_output_dirs_exist_and_empty(parser, args, [], optional=args.save_rendering) stats = {} num_digits_labels = 3 scene = window.Scene() scene.background(tuple(map(int, args.background))) for i, filename in enumerate(args.in_bundles): sft = load_tractogram_with_reference(parser, args, filename) sft.to_vox() sft.to_corner() img_labels = nib.load(args.in_labels[i]) # same subject: same header or coregistered subjects: same header if not is_header_compatible(sft, args.in_bundles[0]) \ or not is_header_compatible(img_labels, args.in_bundles[0]): parser.error('All headers must be identical.') data_labels = img_labels.get_fdata() bundle_name, _ = os.path.splitext(os.path.basename(filename)) unique_labels = np.unique(data_labels)[1:].astype(int) # Empty bundle should at least return a json if not len(sft): tmp_dict = {} for label in unique_labels: tmp_dict['{}'.format(label).zfill(num_digits_labels)] \ = {'mean': 0.0, 'std': 0.0} stats[bundle_name] = {'diameter': tmp_dict} continue counter = 0 labels_dict = {label: ([], []) for label in unique_labels} pts_labels = map_coordinates(data_labels, sft.streamlines._data.T - 0.5, order=0) # For each label, all positions and directions are needed to get # a tube estimation per label. for streamline in sft.streamlines: direction = np.gradient(streamline, axis=0).tolist() curr_labels = pts_labels[counter:counter + len(streamline)].tolist() for i, label in enumerate(curr_labels): if label > 0: labels_dict[label][0].append(streamline[i]) labels_dict[label][1].append(direction[i]) counter += len(streamline) centroid = np.zeros((len(unique_labels), 3)) radius = np.zeros((len(unique_labels), 1)) error = np.zeros((len(unique_labels), 1)) for key in unique_labels: key = int(key) c, d, e = fit_circle_in_space(labels_dict[key][0], labels_dict[key][1], args.fitting_func) centroid[key - 1], radius[key - 1], error[key - 1] = c, d, e # Spatial smoothing to avoid degenerate estimation centroid_smooth = gaussian_filter(centroid, sigma=[1, 0], mode='nearest') centroid_smooth[::len(centroid) - 1] = centroid[::len(centroid) - 1] radius = gaussian_filter(radius, sigma=1, mode='nearest') error = gaussian_filter(error, sigma=1, mode='nearest') tmp_dict = {} for label in unique_labels: tmp_dict['{}'.format(label).zfill(num_digits_labels)] \ = {'mean': float(radius[label-1])*2, 'std': float(error[label-1])} stats[bundle_name] = {'diameter': tmp_dict} if args.show_rendering or args.save_rendering: tube_actor = create_tube_with_radii( centroid_smooth, radius, error, wireframe=args.wireframe, error_coloring=args.error_coloring) scene.add(tube_actor) cmap = plt.get_cmap('jet') coloring = cmap(pts_labels / np.max(pts_labels))[:, 0:3] streamlines_actor = actor.streamtube(sft.streamlines, linewidth=args.width, opacity=args.opacity, colors=coloring) scene.add(streamlines_actor) slice_actor = actor.slicer(data_labels, np.eye(4)) slice_actor.opacity(0.0) scene.add(slice_actor) # If there's actually streamlines to display if args.show_rendering: showm = window.ShowManager(scene, reset_camera=True) showm.initialize() showm.start() elif args.save_rendering: scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'superior.png'), size=(1920, 1080), offscreen=True) scene.pitch(180) scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'inferior.png'), size=(1920, 1080), offscreen=True) scene.pitch(90) scene.set_camera(view_up=(0, 0, 1)) scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'posterior.png'), size=(1920, 1080), offscreen=True) scene.pitch(180) scene.set_camera(view_up=(0, 0, 1)) scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'anterior.png'), size=(1920, 1080), offscreen=True) scene.yaw(90) scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'right.png'), size=(1920, 1080), offscreen=True) scene.yaw(180) scene.reset_camera() snapshot(scene, os.path.join(args.save_rendering, 'left.png'), size=(1920, 1080), offscreen=True) print(json.dumps(stats, indent=args.indent, sort_keys=args.sort_keys))