Cannot read recent DICOM files with Python

Hi there. I’ve been using some code from the example section to read DICOM files. My code looks very similar to the one from this example. I’ve been using it successfully with DICOM files from the internet. Then I moved to our own DICOM files captured less than a month ago. And it fails with the error:

2021-10-25 13:07:26.402 (   9.416s) [                ]vtkDICOMImageReader.cxx:325    ERR| vtkDICOMImageReader (0000016FC5505BA0): There was a problem retrieving data from: C:/tmp/00000005/00000001.dcm
2021-10-25 13:07:26.476 (   9.491s) [                ]       vtkExecutive.cxx:753    ERR| vtkCompositeDataPipeline (0000016FC832DAB0): Algorithm vtkDICOMImageReader(0000016FC5505BA0) returned failure for request: vtkInformation (0000016FC8252100)
  Debug: Off
  Modified Time: 211
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_DATA
  FORWARD_DIRECTION: 0
  ALGORITHM_AFTER_FORWARD: 1
  FROM_OUTPUT_PORT: 0

I am confident that the DICOM files are not corrupted because I can open them and plot them using pydicom library. But I’d like to use VTK rendering. That’s why I tried to read the files with pydicom into a numpy array and then use the numpy_support.numpy_to_vtk to convert into a vtk array. But then I don’t know how to insert those images into my vtk pipeline that uses the reader.GetOutputPort() to initiate the pipeline:

reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName("C:/tmp/00000005")
reader.Update()

selectTissue = vtk.vtkImageThreshold()
.....
selectTissue.SetInputConnection(reader.GetOutputPort())
gaussian = vtk.vtkImageGaussianSmooth()
......
gaussian.SetInputConnection(selectTissue.GetOutputPort())

Someone could help me to understand the error or to fix my vtk pipeline?
Thanks a lot.

Hi, I’m also facing a similar issue, when I try to read the DICOM file, I get an empty pop up screen. But it works if I use only a single image from the DICOM file but of course, volume rendering does not work then.

Did you manage to fix it or understand how to insert the vtk arrays into the vtk pipeline?

Hi, I just succeed to read my dicom files with pydicom. Compute HU units, normalize, sample and export to a numpy file as done here. And then import it into VTK using some code like this:

imgs_to_process = np.load(path_to_your_numpy_export).astype(np.float64) 
num,x,y=imgs_to_process.shape

imageData = vtk.vtkImageData()
depthArray = numpy_support.numpy_to_vtk(num_array=imgs_to_process.ravel(), deep=True, array_type=vtk.VTK_FLOAT)
imageData.SetDimensions((x, y,num))
imageData.SetSpacing([1,1,1])
imageData.SetOrigin([0,0,0])
_=imageData.GetPointData().SetScalars(depthArray)

An then, you can already use imageData with VTK methods as:

    selectTissue = vtk.vtkImageThreshold()
    selectTissue.ThresholdBetween(ThrIn,ThrOut)
    selectTissue.ReplaceInOn()
    selectTissue.SetInValue(255)
    selectTissue.ReplaceOutOn()
    selectTissue.SetOutValue(0)
    selectTissue.SetInputData(imageData)
    selectTissue.Update()

But I didn’t succeed to insert those images in the original pipeline like this:

selectTissue.SetInputConnection(reader.GetOutputPort())

But at least It worked. Hope it helps…

Hi, thank you for your reply. Maybe this example might help you, I was able to use the numpy arrays in the VTK pipeline with it: https://kitware.github.io/vtk-examples/site/Python/Utilities/VTKWithNumpy/

Thanks mate. I will give it a try.