.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_demos/viz_contour.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_contour.py: =================== 3D Contour Surfaces =================== This example demonstrates how to use contour actors to visualize 3D volume data as surface meshes. FURY provides two main functions for creating contours: 1. ``contour_from_volume``: Creates contours directly from 3D volume data 2. ``contour_from_roi``: Creates contours with affine transformation support We will start by creating simple geometric shapes and visualizing their contours. Then, we'll work with real medical imaging data from the `MNI template `_ to demonstrate contours from regions of interest (ROI). .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: Python import numpy as np from fury import actor, window from dipy.data import read_mni_template .. GENERATED FROM PYTHON SOURCE LINES 25-30 Example 1: Simple Contour from Volume ====================================== Let's create a 3D volume with a simple geometric shape - a sphere inside a cube. We'll use ``contour_from_volume`` to extract and visualize the surface of the sphere. .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python size = 100 volume = np.zeros((size, size, size), dtype=np.uint8) # 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 a sphere by setting voxels within radius to 1 sphere_radius = size // 3 volume[distances <= sphere_radius] = 1 .. GENERATED FROM PYTHON SOURCE LINES 46-49 Now we'll create a contour actor from this volume. The contour will extract the surface where the volume transitions from 0 to 1, creating a mesh representation of the sphere. .. GENERATED FROM PYTHON SOURCE LINES 49-57 .. code-block:: Python contour_actor = actor.contour_from_volume( volume, color=(1, 0, 0), opacity=0.8, material="phong" ) scene = window.Scene() scene.add(contour_actor) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Create the ShowManager that we'll reuse throughout the tutorial .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python show_m = window.ShowManager(scene=scene, title="FURY Contour Examples") .. GENERATED FROM PYTHON SOURCE LINES 63-64 Display the first example .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python show_m.start() .. GENERATED FROM PYTHON SOURCE LINES 68-73 Example 2: Multiple Contours with Different Objects ==================================================== Let's create a more complex volume with multiple objects and visualize them with different colors. We'll create a cube with two spheres of different sizes. .. GENERATED FROM PYTHON SOURCE LINES 73-96 .. code-block:: Python volume_multi = np.zeros((size, size, size), dtype=np.uint8) # First sphere (larger) sphere1_radius = size // 3 sphere1_center = (size // 3, size // 2, size // 2) distances1 = np.sqrt( (x - sphere1_center[0]) ** 2 + (y - sphere1_center[1]) ** 2 + (z - sphere1_center[2]) ** 2 ) volume_multi[distances1 <= sphere1_radius] = 1 # Second sphere (smaller) sphere2_radius = size // 4 sphere2_center = (2 * size // 3, size // 2, size // 2) distances2 = np.sqrt( (x - sphere2_center[0]) ** 2 + (y - sphere2_center[1]) ** 2 + (z - sphere2_center[2]) ** 2 ) volume_multi[distances2 <= sphere2_radius] = 2 .. GENERATED FROM PYTHON SOURCE LINES 97-99 Create one actor for all contours to identify they are coming from same volume. .. GENERATED FROM PYTHON SOURCE LINES 99-109 .. code-block:: Python scene.clear() contour_actors = actor.contour_from_volume( volume_multi, color=(0, 1, 0), opacity=0.5, material="phong" ) scene.add(contour_actors) show_m = window.ShowManager(scene=scene, title="FURY Multi Contour Examples") show_m.start() .. GENERATED FROM PYTHON SOURCE LINES 110-112 Create contours for each label directly from the labeled volume using ``contour_from_label``. .. GENERATED FROM PYTHON SOURCE LINES 112-119 .. code-block:: Python scene.clear() label_colors = np.array([[1, 0, 0, 0.7], [0, 0, 1, 0.7]]) multi_contour_actor = actor.contour_from_label(volume_multi, colors=label_colors) scene.add(multi_contour_actor) .. GENERATED FROM PYTHON SOURCE LINES 120-121 Display the scene with multiple contours .. GENERATED FROM PYTHON SOURCE LINES 121-124 .. code-block:: Python show_m = window.ShowManager(scene=scene, title="FURY Multi Contour Examples") show_m.start() .. GENERATED FROM PYTHON SOURCE LINES 125-132 Example 3: Contour from ROI with Affine Transformation ======================================================= Now let's work with real medical imaging data. We'll read the MNI template and create an ellipsoid ROI with the same dimensions. The ``contour_from_roi`` function allows us to apply affine transformations to properly position the contours in world coordinates. We'll display the contour alongside the actual volume slices. .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: Python nifti = read_mni_template() data = np.asarray(nifti.dataobj) affine = nifti.affine .. GENERATED FROM PYTHON SOURCE LINES 138-139 Let's examine the data shape and affine matrix .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: Python print(f"Data shape: {data.shape}") print(f"Affine transformation matrix:\n{affine}") .. GENERATED FROM PYTHON SOURCE LINES 144-146 Create an ellipsoid ROI with the same dimensions as the MNI template. The ellipsoid will be centered in the volume. .. GENERATED FROM PYTHON SOURCE LINES 146-169 .. code-block:: Python roi_shape = data.shape roi_ellipsoid = np.zeros(roi_shape, dtype=np.uint8) # Calculate the center and radii for the ellipsoid center_x, center_y, center_z = roi_shape[0] // 2, roi_shape[1] // 2, roi_shape[2] // 2 radius_x, radius_y, radius_z = roi_shape[0] // 5, roi_shape[1] // 5, roi_shape[2] // 5 # Create coordinate grids x_roi, y_roi, z_roi = np.indices(roi_shape) # Create ellipsoid using the equation: (x/a)^2 + (y/b)^2 + (z/c)^2 <= 1 ellipsoid_eq = ( ((x_roi - center_x) / radius_x) ** 2 + ((y_roi - center_y) / radius_y) ** 2 + ((z_roi - center_z) / radius_z) ** 2 ) roi_ellipsoid[ellipsoid_eq <= 1] = 1 print(f"Created ellipsoid ROI with shape: {roi_ellipsoid.shape}") print(f"Number of voxels in ROI: {np.sum(roi_ellipsoid)}") .. GENERATED FROM PYTHON SOURCE LINES 170-173 Now create a contour from the ellipsoid ROI with affine transformation. The affine matrix ensures the contour is positioned correctly in anatomical space alongside the volume slicer. .. GENERATED FROM PYTHON SOURCE LINES 173-178 .. code-block:: Python contour_ellipsoid = actor.contour_from_roi( roi_ellipsoid, affine=affine, color=(1, 0.5, 0), opacity=0.6, material="phong" ) .. GENERATED FROM PYTHON SOURCE LINES 179-180 Create a volume slicer for the MNI template to display alongside the contour .. GENERATED FROM PYTHON SOURCE LINES 180-183 .. code-block:: Python slicer_actor = actor.volume_slicer(data, affine=affine, interpolation="linear") .. GENERATED FROM PYTHON SOURCE LINES 184-185 Clear the scene and add both the contour and the slicer .. GENERATED FROM PYTHON SOURCE LINES 185-190 .. code-block:: Python scene.clear() scene.add(contour_ellipsoid) scene.add(slicer_actor) .. GENERATED FROM PYTHON SOURCE LINES 191-192 Display the scene with the ellipsoid contour and volume slices .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: Python show_m = window.ShowManager(scene=scene, title="FURY Contour from ROI Examples") show_m.start() .. _sphx_glr_download_auto_examples_04_demos_viz_contour.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_contour.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_contour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: viz_contour.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_