Main Content

volshow

Display volume

    Description

    Numeric Array

    example

    vol = volshow(V) creates a Volume object that displays the 3-D volume V. You can rotate and zoom in and out on the display interactively using the mouse. Use vol to query and modify properties of the Volume object after you create the object. For a list of properties, see Volume Properties.

    vol = volshow(V,config) displays the 3-D volume V using the values of the Volume object properties specified by the config structure.

    vol = volshow(V,Name=Value) modifies the appearance of the volume using one or more name-value arguments. For example, volshow(V,RenderingStyle="Isosurface") displays the 3-D volume V and sets the rendering style as "Isosurface".

    Blocked Image Volume

    Since R2023a

    example

    bVol = volshow(bim) creates a BlockedVolume object that displays the 3-D blocked image bim. You can rotate and zoom in and out on the display interactively using the mouse. Use bVol to query and modify properties of the BlockedVolume object after you create the object. For a list of properties, see BlockedVolume Properties.

    bVol = volshow(bim,Name=Value) modifies the appearance of the blocked volume using one or more name-value arguments. For example, ResolutionLevel="coarse" specifies the resolution level to display as the coarsest resolution level.

    Note

    Medical Imaging Toolbox™ extends the functionality of the volshow (Image Processing Toolbox™) function to display a medicalVolume (Medical Imaging Toolbox) object in the patient coordinate system. For more information, see volshow (Medical Imaging Toolbox).

    Examples

    collapse all

    Load MRI data into the workspace and remove the singleton dimension.

    load mri
    V = squeeze(D);

    Generate a colormap and transparency (alpha) map suitable for MRI images.

    intensity = [0 20 40 120 220 1024];
    alpha = [0 0 0.15 0.3 0.38 0.5];
    color = [0 0 0; 43 0 0; 103 37 20; 199 155 97; 216 213 201; 255 255 255]/255;
    queryPoints = linspace(min(intensity),max(intensity),256);
    alphamap = interp1(intensity,alpha,queryPoints)';
    colormap = interp1(intensity,color,queryPoints);

    This MRI scan has a non-uniform, or anisotropic, voxel size of 1-by-1-by-2.5 mm. Specify the transformation matrix that scales the image to the correct voxel dimensions.

    sx = 1;
    sy= 1;
    sz = 2.5;
    A = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1];

    Create an affinetform3d object that performs the scaling.

    tform = affinetform3d(A);

    View the volume with the custom colormap, transparency map, and transformation. Drag the mouse to rotate the volume. Use the scroll wheel to zoom in and out of the volume.

    vol = volshow(V,Colormap=colormap,Alphamap=alphamap,Transformation=tform);

    This example uses a subset of the Medical Segmentation Decathlon data set [1]. The subset of data includes two CT chest volumes and corresponding label images, stored in the NIfTI file format.

    Run this code to download the MedicalVolumNIfTIData.zip file from the MathWorks® website, then unzip the file. The size of the data file is approximately 76 MB.

    zipFile = matlab.internal.examples.downloadSupportFile("medical", ...
        "MedicalVolumeNIfTIData.zip");
    filepath = fileparts(zipFile);
    unzip(zipFile,filepath)

    The folder dataFolder contains the downloaded and unzipped data.

    dataFolder = fullfile(filepath,"MedicalVolumeNIfTIData");

    Specify the filenames of the volume and label image used in this example.

    dataFile = fullfile(dataFolder,"lung_043.nii.gz");
    labelDataFile = fullfile(dataFolder,"LabelData","lung_043.nii.gz");

    Read the image data and the metadata from the image file.

    V = niftiread(dataFile);
    info = niftiinfo(dataFile);

    Define a transparency map and color map for this volume. The values used in this example were determined using manual trial and error.

    alpha = [0 0 0.7 1.0];
    color = [0 0 0; 200 140 75; 231 208 141; 255 255 255] ./ 255;
    intensity = [-3024 -700 -400 3071];
    queryPoints = linspace(min(intensity),max(intensity),256);
    alphamap = interp1(intensity,alpha,queryPoints)';
    colormap = interp1(intensity,color,queryPoints);

    This MRI scan has a nonuniform, or anisotropic, voxel size. Extract the voxel spacing from the file metadata, and define the transformation to display the volume with correct dimensions.

    voxelSize = info.PixelDimensions;
    sx = voxelSize(2);
    sy= voxelSize(1);
    sz = voxelSize(3);
    A = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1];

    Create an affinetform3d object that performs the scaling.

    tform = affinetform3d(A);

    View the volume as a 3-D object. Specify the rendering style as "CinematicRendering". The cinematic rendering style displays the volume based on the specified color and transparency for each voxel,with iterative postprocessing that produces photorealistic shadows and lighting.

    viewer = viewer3d;
    vol = volshow(V,Parent=viewer, ...
        RenderingStyle="CinematicRendering", ...
        Colormap=colormap, ...
        Alphamap=alphamap, ...
        Transformation=tform);

    Figure contains an object of type images.ui.graphics3d.viewer3d.

    Pause to apply all postprocessing iterations before updating the display in Live Editor.

    pause(3)
    drawnow

    References

    [1] Medical Segmentation Decathlon. "Lung." Tasks. Accessed May 10, 2018. http://medicaldecathlon.com/. The Medical Segmentation Decathlon data set is provided under the CC-BY-SA 4.0 license. All warranties and representations are disclaimed. See the license for details.

    Load a grayscale volume into the workspace and display the volume using volshow.

    load("spiralVol.mat")
    h = volshow(spiralVol);
    viewer = h.Parent;
    hFig = viewer.Parent;
    drawnow

    Specify the name of the GIF file in which to save the animation.

    filename = "animatedSpiral.gif";

    Aim the camera at the center of the volume.

    sz = size(spiralVol);
    center = sz/2 + 0.5;
    viewer.CameraTarget = center;

    Specify the number of frames in the animation, then create an array of camera positions in a circle around the center of the volume.

    numberOfFrames = 12;
    vec = linspace(0,2*pi,numberOfFrames)';
    dist = sqrt(sz(1)^2 + sz(2)^2 + sz(3)^2);
    myPosition = center + ([cos(vec) sin(vec) ones(size(vec))]*dist);

    At each camera position, update the display and write the frame to the GIF file. You can play the file in a video viewer.

    for idx = 1:length(vec)
        % Update the current view
        viewer.CameraPosition = myPosition(idx,:);
        % Capture the image using the getframe function
        I = getframe(hFig);
        [indI,cm] = rgb2ind(I.cdata,256);
        % Write the frame to the GIF file
        if idx==1
            % Do nothing. The first frame displays only the viewer, not the
            % volume.
        elseif idx == 2
            imwrite(indI,cm,filename,"gif",Loopcount=inf,DelayTime=0)
        else
            imwrite(indI,cm,filename,"gif",WriteMode="append",DelayTime=0)
        end
    end

    animatedSpiral.gif

    This example creates a large 500-by-500-by-2500 image volume. If your machine does not have enough memory to create and store the 2.5 GB volume, decrease imSize before running this example.

    imSize = [500,500,2500];

    Create a simulated 3-D image of bubbles, V. This can take several minutes.

    V = rand(imSize,"single");
    BW = false(size(V));
    BW(V < 0.000001) = true;
    V = bwdist(BW);
    V(V <= 20) = 1;
    V(V > 20) = 0;

    If you try to display V directly, volshow returns an error that the volume is too large. Instead, create a blockedImage object that points to V and has a block size of 500-by-500-by-500 voxels.

    bim = blockedImage(V,BlockSize=[500,500,500]);

    Display the blockedImage using volshow. The volshow function reads blocks into memory one at a time and stitches individual block renderings to produce the final volume.

    bVol = volshow(bim);

    Input Arguments

    collapse all

    3-D volume, specified as a numeric array.

    Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical | char

    Rendering information exported by Volume Viewer, specified as a structure.

    Data Types: struct

    Blocked image volume, specified as a blockedImage object that reads 3-D blocks of grayscale, RGB, or RGBA data. The blocked image can have a single resolution level or multiple resolution levels.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: volshow(V,RenderingStyle="Isosurface") displays the 3-D volume V and sets the rendering style as "Isosurface".

    Note

    The properties listed here are only a subset. For a full list of in-memory volume properties, see Volume Properties. For a full list of blocked volume properties, see BlockedVolume Properties.

    Parent of the Volume or BlockedVolume object, specified as a Viewer3D object. You can create a Viewer3D object using the viewer3d function. When you call volshow without specifying a parent, the function creates a new Viewer3D object and sets that object as the parent. A Volume or BlockedVolume object cannot be reparented.

    Rendering style, specified as one of the values in the table.

    ValueDescription
    "VolumeRendering"View the volume based on the specified color and transparency for each voxel.
    "CinematicRendering"

    View the volume based on the specified color and transparency for each voxel, with iterative postprocessing that produces photorealistic shadows and lighting.

    This rendering style is useful for displaying opaque volumes.

    "LightScattering"

    View the volume using a volumetric light scattering model that simulates the absorption, inscattering, and outscattering of light through the volume.

    This rendering style is useful for displaying translucent volumes that do not have large intensity gradients, such as smoke, fog, and clouds.

    "MaximumIntensityProjection"View the voxel with the largest intensity value for each ray projected through the data. For RGB volumes, view the voxel with the largest luminance in the CIE 1976 L*a*b* color space.
    "MinimumIntensityProjection"View the voxel with the smallest intensity value for each ray projected through the data. For RGB volumes, view the voxel with the smallest luminance in the CIE 1976 L*a*b* color space.
    "GradientOpacity"

    View the volume based on the specified color and transparency with an additional transparency applied if the voxel is similar in intensity (for grayscale volumes) or luminance (for RGB volumes) to the previous voxel along the viewing ray.

    When a volume with uniform intensity is rendered using "GradientOpacity", the internal portion of the volume appears more transparent than the "VolumeRendering" rendering style, enabling better visualization of the intensity or luminance gradients in the volume.

    "Isosurface"

    View an isosurface of the volume specified by the value in the IsosurfaceValue property.

    "SlicePlanes"

    View three orthogonal slice planes.

    Transparency map for the volume content, specified as an n-element column vector with values in the range [0, 1]. The maximum length of the vector is 256. When viewing RGB volumes, the object uses the luminance of the voxel in the CIE 1976 L*a*b* color space to assign a transparency from the transparency map. When the AlphaData property is nonempty, the Alphamap property has no effect.

    Colormap of grayscale volume data, specified as an n-by-3 numeric matrix with values in the range [0, 1]. The maximum number of colors n is 256. This property has no effect when viewing RGB volumes.

    Overlay data to blend with the object data during rendering, specified as one of these values:

    • When displaying an in-memory volumetric array, V, specify OverlayData as a numeric array.

    • When displaying a blocked image volume, bim, specify OverlayData as a blockedImage object that reads blocks of 3-D grayscale data.

    The viewer shows the overlay only when the RenderingStyle property value is "SlicePlanes", "VolumeRendering", or "GradientOpacity". You can modify the appearance of the overlay by changing the OverlayRenderingStyle, OverlayColormap, and OverlayAlphamap properties.

    Overlay rendering style, specified as one of the values in the table.

    ValueDescription
    "LabelOverlay"

    View the overlay based on the color and transparency of each labeled region. Use this rendering style to visualize ordinal data, like binary or semantic segmentation results, on top of your data.

    "VolumeOverlay"

    View the overlay based on the specified color and transparency for each voxel.

    "GradientOverlay"

    View the overlay based on the color and transparency for each voxel with an additional transparency applied based on the difference between the current voxel and the previous voxel along the viewing ray.

    Output Arguments

    collapse all

    Volume, returned as a Volume object. For more information about modifying aspects of the volume, see Volume Properties.

    Blocked volume, returned as a BlockedVolume object. For more information about modifying aspects of the volume, see BlockedVolume Properties.

    More About

    collapse all

    Events

    To receive notification from a Volume or BlockedVolume object when certain events happen, set up listeners for these events. You can specify a callback function that executes when one of these events occurs. When the object notifies your application through the listener, it returns data specific to the event. Look at the event class for the specific event to see what is returned.

    Event NameTriggerEvent DataEvent Attributes
    ClippingPlanesChanging

    An object clipping plane is being interactively moved. This event does not execute if the clipping plane is programmatically moved.

    images.ui.graphics3d.events.ClippingPlanesChangedEventData

    NotifyAccess: private

    ListenAccess: public

    ClippingPlanesChangedAn object clipping plane stops being interactively moved. This event does not execute if the clipping plane is programmatically moved.images.ui.graphics3d.events.ClippingPlanesChangedEventData

    NotifyAccess: private

    ListenAccess: public

    SlicePlanesChanging

    An object slice plane is being interactively moved. This event does not execute if the slice plane is programmatically moved.

    images.ui.graphics3d.events.SlicePlanesChangedEventData

    NotifyAccess: private

    ListenAccess: public

    SlicePlanesChangedAn object slice plane stops being interactively moved. This event does not execute if the slice plane is programmatically moved.images.ui.graphics3d.events.SlicePlanesChangedEventData

    NotifyAccess: private

    ListenAccess: public

    DataReadStartedA BlockedVolume object is sending blocks of data to be rendered in the scene. This event is not applicable for Volume objects.event.EventData

    NotifyAccess: private

    ListenAccess: public

    DataReadFinishedThe BlockedVolume object has finished sending all blocks of data that are visible in the scene. This event is not applicable for Volume objects.event.EventData

    NotifyAccess: private

    ListenAccess: public

    Version History

    Introduced in R2018b

    expand all