Automating .vtp export to NIFTI with Slicer Modules

Hi there, I am trying to convert a .vtp segmentation to a compressed NifTi file. I was able to do it with the Slicer GUI via the following steps:

  1. Add data → Choose files → select .vtp file
  2. Right click on the imported model and convert it to a segmentation node
  3. Switch into the Segmentations editor, load the active segmentation, select destination folder, file format as NifTi, select “use compression”, export.

These steps work fine in the Slicer GUI, but I need to do this for a significant number of models, and was hoping to write a Python script to automate this process. I’m quite new to the Python-scripting side of Slicer, so am unsure of the Slicer modules I would need to import and use for the steps outlined above. The paths for the input and destination files are not an issue, but translating the Slicer GUI steps to the Slicer API is what I am confused about.

If it helps at all, ideally this program would be executed via Slicer.exe --python-script path/to/script.py.

Thank you!

@jag - there’s some good material on this topic on this forum but it can be hard to find with the default search function so I pasted your question into our experimental chatbot (we don’t expose it to everyone since it costs some money to use). Have a look at the material below and let us know if it answers your question and if not ask some followups. The links may not be working right but the instructions seem reasonable and you may get further details from google or another chatbot. -Steve

claude_3_opus_botForum Helper

Here are some potentially relevant topics from searching the Slicer discourse forum:

Converting model to labelmap for deep learning segmentation - Discusses converting a model (like VTP) to a segmentation labelmap volume using a Python script in Slicer. Shows example code for going from model node to segmentation node to labelmap volume node.

Conversion between surface models and segmentations - Covers converting between model (like VTP surfaces) and segmentation representations using Python scripting. Gives code snippets for various conversion scenarios.

Segment editor manual/tutorial? - The Segment Editor module is used when converting models to segmentations in the Slicer GUI. This topic links to tutorials and documentation on using the module that may help with understanding the steps to replicate in Python.

How can I save segments into a specific folder - Covers saving out segmentations to NRRD/NRRD files from the GUI and has some discussion on specifying output paths that is relevant for automation.

Based on those, here is a rough outline of the key steps in Python:

  1. Load the VTP file as a model node using slicer.util.loadModel()
  2. Create a new segmentation node
  3. Import the model into the segmentation using segmentationNode.AddSegmentFromClosedSurfaceRepresentation
  4. Create a new labelmap volume node
  5. Convert the segmentation to a labelmap and set the labelmap node as the output using segmentationNode.GetBinaryLabelmapRepresentation
  6. Set compression and other output options on the labelmap node
  7. Save the labelmap node to a NIFTI file using slicer.util.saveNode()

The main Slicer Python APIs you’ll need are:

  • slicer.util for loading and saving nodes
  • slicer.vtkMRMLModelNode() and slicer.vtkMRMLSegmentationNode() to create model and segmentation nodes
  • Methods on the vtkMRMLSegmentationNode to convert representations
  • vtkMRMLLabelMapVolumeNode and its compression settings

Let me know if you need any other information!