.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_demos/viz_slicer.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_demos_viz_slicer.py: =============== 3D Image Slicer =============== This example demonstrates how to use the `slicer` actor to visualize 3D data in a 2D slice view. The `slicer` actor allows you to interactively slice through a 3D volume and visualize the resulting 2D slices. we will use the slicer actor to visualize a 3D volume where we generate a cube with a sphere inside it. The cube is filled with blue color, and the sphere is filled with white color. We will add event handlers to allow the user to interact with the slices Then, we will create a 3D volume slicer from `MNI template `_ present in `DIPY `_'s data. The user can pick a voxel in the slice to get its intensity value and use arrow keys to navigate through the slices. .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: Python import numpy as np from fury import actor, window from fury.actor import get_slices, show_slices from fury.lib import EventType from dipy.data import read_mni_template .. GENERATED FROM PYTHON SOURCE LINES 29-34 Create a 3D cube with a sphere inside it. This example demonstrates how to create a 3D cube with a sphere inside it and visualize it using the `slicer` actor. The cube is filled with blue color, and the sphere is filled with white color. The user can interactively slice through the cube and visualize the resulting 2D slices. .. GENERATED FROM PYTHON SOURCE LINES 34-50 .. code-block:: Python size = 100 cube = np.zeros((size, size, size, 3), dtype=np.float32) cube[..., 2] = 1 # Set blue color for the cube # Create coordinate grids x, y, z = np.indices((size, size, size)) center = (size - 1) / 2 # Calculate distances from center distances = np.sqrt((x - center) ** 2 + (y - center) ** 2 + (z - center) ** 2) # Create sphere (white) sphere_mask = distances <= size // 2 cube[sphere_mask] = [1, 1, 1] .. GENERATED FROM PYTHON SOURCE LINES 51-52 Create a slice actor for the cube and add it to the scene. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python slicer_actor = actor.data_slicer(cube) scene = window.Scene() scene.add(slicer_actor) .. GENERATED FROM PYTHON SOURCE LINES 57-63 Create a function to handle key events and navigate through the slices. The user can use the arrow keys to move up and down through the slices. The `get_slices` function retrieves the current slice positions, and the `show_slices` function updates the displayed slices based on the new positions. The `show_m.render()` function is called to update the scene after each key event. .. GENERATED FROM PYTHON SOURCE LINES 63-80 .. code-block:: Python def handle_key_event(event): position = get_slices(slicer_actor) if event.key == "ArrowUp": position += 1 elif event.key == "ArrowDown": position -= 1 bounds = slicer_actor.get_bounding_box() position = np.maximum(bounds[0], position) position = np.minimum(bounds[1], position) show_slices(slicer_actor, position) show_m.render() .. GENERATED FROM PYTHON SOURCE LINES 81-82 Add event handlers to the slice actor for key events. .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: Python show_m = window.ShowManager(scene=scene, title="FURY Cube Slicer Example") show_m.renderer.add_event_handler(handle_key_event, EventType.KEY_DOWN) .. GENERATED FROM PYTHON SOURCE LINES 88-89 Start the show manager to display the scene and allow interaction. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python show_m.start() .. GENERATED FROM PYTHON SOURCE LINES 92-94 Let's read the 3D data from dipy and check the shape of the data. The data also has an affine transformation matrix that maps the voxel coordinates to world coordinates. .. GENERATED FROM PYTHON SOURCE LINES 94-99 .. code-block:: Python nifti = read_mni_template() data = np.asarray(nifti.dataobj) affine = nifti.affine .. GENERATED FROM PYTHON SOURCE LINES 100-101 Let's check the shape of the data and the affine transformation matrix. .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python print(data.shape) print(affine) .. GENERATED FROM PYTHON SOURCE LINES 105-106 Create volume_slicer actor to visualize the 3D data as XY, YZ and XZ slices. .. GENERATED FROM PYTHON SOURCE LINES 106-109 .. code-block:: Python slicer_actor = actor.volume_slicer(data, affine=affine, depth_write=True) .. GENERATED FROM PYTHON SOURCE LINES 110-111 Create a scene and add the slicer actor to it. .. GENERATED FROM PYTHON SOURCE LINES 111-115 .. code-block:: Python scene = window.Scene() scene.add(slicer_actor) .. GENERATED FROM PYTHON SOURCE LINES 116-119 Create a function to handle the pick event and print the intensity of the voxel that was clicked. The event contains information about the picked voxel, including its index and intensity value. .. GENERATED FROM PYTHON SOURCE LINES 119-127 .. code-block:: Python def handle_pick_event(event): info = event.pick_info intensity = np.asarray(info["rgba"].rgb).mean() print(f"Voxel {info['index']}: {intensity:.2f}") .. GENERATED FROM PYTHON SOURCE LINES 128-129 Add event handlers to the slicer actor for picking and key events. .. GENERATED FROM PYTHON SOURCE LINES 129-136 .. code-block:: Python slicer_actor.add_event_handler(handle_pick_event, EventType.POINTER_DOWN) show_m = window.ShowManager(scene=scene, title="FURY MNI Template Slicer Example") show_m.renderer.add_event_handler(handle_key_event, EventType.KEY_DOWN) show_m.show_axes_gizmo() .. GENERATED FROM PYTHON SOURCE LINES 137-138 Start the show manager to display the scene and allow interaction. .. GENERATED FROM PYTHON SOURCE LINES 138-140 .. code-block:: Python show_m.start() .. _sphx_glr_download_auto_examples_04_demos_viz_slicer.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_slicer.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_slicer.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: viz_slicer.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_