I’m trying to create a segmentation (externally) in Python and then import it into Slicer through the data module, but Slicer keeps crashing. As far as I can tell, this seems to be related to how SimpleITK converts NumPy array to Images, but I don’t know what to do differently to make it work with Slicer.
For example, given a segmentation NRRD saved from Slicer, Segmentation.seg.nrrd
, the following code works:
import SimpleITK as sitk
seg_vol = sitk.ReadImage(DATA_DIR + 'Segmentation.seg.nrrd')
writer = sitk.ImageFileWriter()
writer.Execute(seg_vol, DATA_DIR + 'Segmentation_v2.seg.nrrd', True)
… and I import Segmentation_v2.seg.nrrd
into Slicer without problems.
However, if I convert the sitk image to a Numpy array and back again…
seg_vol = sitk.ReadImage(DATA_DIR + 'Segmentation.seg.nrrd')
# there and back again:
seg_nda = sitk.GetArrayFromImage(seg_vol)
seg_reverse = sitk.GetImageFromArray(seg_nda)
seg_reverse.CopyInformation(seg_vol)
writer = sitk.ImageFileWriter()
writer.Execute(seg_vol, DATA_DIR + 'Segmentation_v3.seg.nrrd', True)
… then Slicer crashes when I try to import the segmentation, and I get a bunch of nasty error messages in my terminal:
Warning: In /Volumes/Dashboards/Preview/Slicer-0/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.cxx, line 586
vtkMRMLSegmentationStorageNode (0x7faecf985ed0): ReferenceImageExtentOffset attribute was not found in NRRD segmentation file. Assume no offset.
Warning: In /Volumes/Dashboards/Preview/Slicer-0/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.cxx, line 663
vtkMRMLSegmentationStorageNode (0x7faecf985ed0): Segment ID is missing for segment 0 adding segment with ID: SegmentAuto
Warning: In /Volumes/Dashboards/Preview/Slicer-0/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.cxx, line 674
vtkMRMLSegmentationStorageNode (0x7faecf985ed0): Segment name is missing for segment 0
Warning: In /Volumes/Dashboards/Preview/Slicer-0/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.cxx, line 724
vtkMRMLSegmentationStorageNode (0x7faecf985ed0): Segment extent is missing for segment 0
Segmentation fault: 11
Am I missing something obvious? Any solutions/suggestions welcome.