Here’s what’s happening (at the Slicer Python console):
import SimpleITK as sitk
sitk.WriteImage(slices[0], "C:\\Users\\username\\test\\0000.mhd") # works fine
sitk.WriteImage(slices[0], "C:\\Users\\username\\test\\0000.nrrd") # works fine
sitk.WriteImage(slices[0], "C:\\Users\\username\\test\\0000.dcm") # does nothing, gives no error message, just does nothing
# using sitk.ImageFileWriter().Execute() gives the same behavior, incidentally
Overall, this is a very frustrating situation, because I had written a script that outputs DICOM files using SimpleITK (I am converting some MHDs to DICOM for further processing), and since SimpleITK is a part of Slicer, I assumed that I could run it in Slicer. And if I am doing something wrong, then I feel that I at least deserve some kind of notice like: “ERROR: We disabled writing DICOM because [reason]!”.
The script works perfectly on my laptop with a standalone SimpleITK library, but on my work desktop I cannot install standalone SimpleITK (ugh) so I thought I would try to run it inside of Slicer, which I am allowed to install. Now I am afraid I will have to rewrite the script in order to use Slicer’s internal DICOM modules, which will require a few days of reading, probably. And all because of this one function!
I am curious to know:
-
Has anyone else used SimpleITK ImageFileWriter/WriteImage to write DICOM files from Slicer scripts? Is there a way to fix the problem?
-
Is there a good way to export a SimpleITK image object as DICOM from Slicer? Or more generally to convert an .mhd/.raw file pair to a DICOM series (I have the tags stored separately)?
-
Can someone please consider adding an error message to be printed when
SimpleITK.WriteImage()fails?
The only script example for exporting an image as DICOM is this, which starts with object types I don’t recognize and uses a bunch of functionality I was hoping to avoid:
volumeNode = getNode("CTChest")
outputFolder = "c:/tmp/dicom-output"
# Create patient and study and put the volume under the study
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
patientItemID = shNode.CreateSubjectItem(shNode.GetSceneItemID(), "test patient")
studyItemID = shNode.CreateStudyItem(patientItemID, "test study")
volumeShItemID = shNode.GetItemByDataNode(volumeNode)
shNode.SetItemParent(volumeShItemID, studyItemID)
import DICOMScalarVolumePlugin
exporter = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
exportables = exporter.examineForExport(volumeShItemID)
for exp in exportables:
exp.directory = outputFolder
exporter.export(exportables)
