Automating .vtp export to NIFTI with Slicer Modules

@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!