Convert Slicer data to be compatible with MagicaVoxel

Hello,
I am currently creating a VR project to visualize an MRI in a VR environment and allow a patient and a doctor to connect to one another to analyze it together. The functionality for this is available in Unreal Engine specifically using the voxel plugin. The current plan is to create a 3D model of the MRI in slicer and then transfer it to either Unreal directly or into MagicaVoxel which has built in compatibility with Unreal’s plugin.
If anyone knows a method that can allow slicer data to be saved in a voxel format it would be greatly appreciated.
Thank you.

This is very cool. So, it seems 3D Slicer’s Segment Editor module is actually an powerful voxel art editor.

You can create a .vox file in Slicer quite easily:

  • Segment what you would like yo display using Segment Editor module
  • Export segmentation to labelmap
  • Crop and resample as needed using Crop volume module (to reduce file size)
  • Convert to unsigned char using Cast scalar volume
  • Export to .vox file by copy-pasting this code snippet into the Python console:
# This only needed once, may take a couple of minutes
pip_install('py-vox-io')

# Write labelmap volume to vox file
labelmapVolumeNode = getNode('Segmentation-Segment_1-label cropped')
outputFilePath = r'c:\Users\andra\VoxEdit\test\test2.vox'
import numpy as np
from pyvox.models import Vox
from pyvox.writer import VoxWriter
a = arrayFromVolume(labelmapVolumeNode)
vox = Vox.from_dense(a)
VoxWriter(outputFilePath, vox).write()

Here is how bones segmented from CTChest sample data look like in MagicaVoxel:

As you can see, the voxelized look is interesting, quite cool. However, probably you are much better off just saving the segmentations as .obj or export to .gltf (using SlicerOpenAnatomy extension) and visualize that, because the voxelization does not reproduce smooth surfaces very well.

As a comparison, the original smooth segmentation is rendered in Slicer like this:

Note that Slicer can directly display the scene content in a 3D headset and some collaborative virtual reality features are in the works, too. If you are interested in virtual reality then you may join related discussions and breakout sessions at the upcoming Slicer Project week (starting on Monday).

1 Like

Hello, I have been messing around with your code and I noticed that getNode is a function not defined in py-vox-io. How did you define that function? Is there another library you have used?

getNode and arrayFromVolume functions are in slicer.util package.

The code snippet is meant to be run in 3D Slicer Python environment (e.g., in the Python console that to can open by hitting Ctrl+3) to convert a labelmap volume node that is loaded into the application.