Exemple #1
0
def load_labelled_volume(data, vmin=0, alpha=1, **kwargs):
    """
        Load volume image from .nrrd file. 
        It assume that voxels with value = 0 are empty while voxels with values > 0
        are labelles (e.g. to indicate the location of a brain region in a reference atlas)

        :param data: str, path to file with volume data or 3d numpy array
        :param vmin: float, values below this numner will be assigned an alpha=0 and not be visualized
        :param **kwargs: kwargs to pass to the Volume class from vtkplotter
        :param alpha: float in range [0, 1], transparency [for the part of volume with value > vmin]
    """
    # Load/check volumetric data
    if isinstance(data, str):  # load from file
        if not os.path.isfile(data):
            raise FileNotFoundError(f'Volume data file {data} not found')

        try:
            data = brainio.load_any(data)
        except:
            raise ValueError(f"Could not load volume data from file: {data}")

    elif not isinstance(data, np.ndarray):
        raise ValueError(
            f"Data should be a filepath or np array, not: {data.__type__}")

    # Create volume and set transparency range
    vol = Volume(data, alpha=alpha, **kwargs)

    otf = vol.GetProperty().GetScalarOpacity()
    otf.RemoveAllPoints()
    otf.AddPoint(vmin, 0)  # set to transparent
    otf.AddPoint(vmin + .1, alpha)  # set to opaque
    otf.AddPoint(data.max(), alpha)

    return vol
"""Modify a Volume dataset and
colorize voxels individually
"""
from vtkplotter import Text2D, Volume, show
import numpy as np

vol = Volume(shape=(10,11,12), mode=0)
vol.alpha(0.8).jittering(False)
vol.interpolation(0) # nearest neighbour interpolation type

# Overwrite the (sofar empty) voxel data with new data
vol.imagedata().AllocateScalars(3, 4) # type 3 corresponds to np.uint8
arr = vol.getPointArray()
arr[:] = np.random.randint(0,255, (10*11*12,4)).astype(np.uint8)

# For 4 component data, the first 3 directly represent RGB, no lookup table.
# (see https://vtk.org/doc/nightly/html/classvtkVolumeProperty.html):
vol.GetProperty().IndependentComponentsOff()

show(vol, Text2D(__doc__), axes=1)