简体   繁体   中英

How to use VTK_array in VTK pipeline (Python)?

I am new to vtk and am trying to do volume rendering with a DICOM file.

When I try to read the DICOM file using VTK with the following code:

import vtk
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(r"C:\Users\I_0001")
reader.Update()

I get the following error:

ERROR: In ..\IO\Image\vtkDICOMImageReader.cxx, line 143
vtkDICOMImageReader (000001DC61D2FED0): Couldn't open C:\Users\I_0001

ERROR: In ..\IO\Image\vtkDICOMImageReader.cxx, line 237
vtkDICOMImageReader (000001DC61D2FED0): Either a filename was not specified or the specified directory does not contain any DICOM images.

ERROR: In ..\Common\ExecutionModel\vtkExecutive.cxx, line 753
vtkCompositeDataPipeline (000001DC5C4A0A90): Algorithm vtkDICOMImageReader(000001DC61D2FED0) returned failure for request: vtkInformation (000001DC5F8E72A0)
  Debug: Off
  Modified Time: 207
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_DATA
  FORWARD_DIRECTION: 0
  ALGORITHM_AFTER_FORWARD: 1
  FROM_OUTPUT_PORT: 0

I know the file is not corrupted as it works when I try to read it with pydicom. 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. Can someone help me to insert my DICOM file images which are VTK arrays into the VTK pipeline?

This is my current code:

# reading the data and converting it to VTK array using numpy_support
fileVolume = r"C:\Users\I_0001"
dicomVolume = pydicom.read_file(fileVolume)
shape = dicomVolume.pixel_array.shape
VTK_data = numpy_support.numpy_to_vtk(num_array=dicomVolume.pixel_array.ravel(), deep=True, array_type=vtk.VTK_FLOAT)


#need to insert VTK data here instead of using the vtk DICOM Image Reader
# Create the reader for the data.
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(r"C:\Users\I_0001")
reader.Update()

mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(reader.GetOutput())

使用VTKWithNumpy 例如通过@Nico Vuaille我联系能我numpy的数组转换成图像VTK对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM