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)

# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
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)
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