Combine STLs and export as NRRD

Hi @lassoan,

I am able to export .stl file to a single binary label map (nrrd file) very well.

However, I have two .stl files (gland and tumor) that I need to combine them first and then export them as NRRD file.

I wrote the code based on the help from the segmentation examples in script repo and my understandings. However, I get this error and I think I am missing something.

I would appreciate, if you help me out with this.

import SampleData
import numpy as np
import SimpleITK as sitk
import sitkUtils

outputPath ="/Volumes/Mac_Partition/output_dir/"
# Input nodes
volumeNode   = slicer.util.loadVolume('/Volumes/Mac_Partition/files/MRI_0001.nii.gz')
prostateNode = slicer.util.loadSegmentation('/Volumes/Mac_Partition/files/STLs/GlandSurface.STL')

# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode1 = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(prostateNode, labelmapVolumeNode1, volumeNode)

targetNode   = slicer.util.loadSegmentation('/Volumes/Mac_Partition/files/STLs/Target.STL')
# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode2 = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(targetNode, labelmapVolumeNode2, volumeNode)

# 'a' and 'b' are numpy arrays,
a = slicer.util.arrayFromVolume(labelmapVolumeNode1)
b = slicer.util.arrayFromVolume(labelmapVolumeNode2)
# combined using any numpy array operations to produce the result array 'c'
c = b-a

segmentationNode = slicer.modules.volumes.logic().CloneVolume(volumeNode, "Difference")
slicer.util.updateVolumeFromArray(segmentationNode, c)
setSliceViewerLayers(background=segmentationNode)

# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
print(segmentationNode)
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)

# Masking
voxels = slicer.util.arrayFromVolume(volumeNode)
mask = slicer.util.arrayFromVolume(labelmapVolumeNode)
maskedVoxels = np.copy(voxels)  # we don't want to modify the original volume
maskedVoxels[mask==0] = 0

# Write masked volume to volume node and save it to disk.
maskedVolumeNode = slicer.modules.volumes.logic().CloneVolume(volumeNode, "Masked")
slicer.util.updateVolumeFromArray(maskedVolumeNode, maskedVoxels)
slicer.util.setSliceViewerLayers(maskedVolumeNode)
filepath = outputPath + "/" + volumeNode.GetName()+"-label.nrrd"
slicer.util.saveNode(labelmapVolumeNode, filepath)

This is the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
In [19]:
Line 32:

TypeError: ExportVisibleSegmentsToLabelmapNode argument 1: method requires a vtkMRMLSegmentationNode, a vtkMRMLScalarVolumeNode was provided.
---------------------------------------------------------------------------

Never mind, I figured out the solution and corrected the code. Now I am able to combine STLs and export them as NRRD.

1 Like

Hi @sulaimanvesal, I am trying to perform the same task as the one you shared in this post. I am still getting the same errors as yours, but I saw you solved the problem.
Do you mind share your code with the solution ?
thanks in advande for helping,
Sabrina

The code above is incorrect. It sets a volume node in the variable called segmentationNode. Then this volume node is used as an input in ExportVisibleSegmentsToLabelmapNode, which expects a segmentation node as input. I would recommend to complete the Scripting and module development tutorial, study the segmentation section in the documentation and examples in the script repository, which all work correctly. Then, once you have a good understanding of what’s happening, you can have a look at the script above and make it work.