How to convert a 3D vtkDataSet into a numpy array?

冷暖自知 提交于 2019-12-07 12:05:00

问题


I am trying to read a series of DICOM files and render it into a 3D Model. I have accomplished this. However I also want to be able to remove certain slices before rendering. This is where I am lost. I am unable to find a way to access each file loaded and remove the ones I want. I have read that I could convert the the dataset to a numpy array. However I am uncertain as to how I could do that.

    reader = vtkDICOMImageReader()
    reader.SetDirectoryName('loads a directory of DICOM files')
    reader.Update()

    imageDataGeometryFilter = vtkImageDataGeometryFilter()
    imageDataGeometryFilter.SetInputConnection(reader.GetOutputPort())
    imageDataGeometryFilter.Update()

    mapper = vtkPolyDataMapper()
    mapper.SetInputConnection(imageDataGeometryFilter.GetOutputPort())

    '''Here is where I want to take my vtkDataSet and convert it into a numpy array.
       Once in array form I can remove data that I do not want (ie DICOM slices).
       Then I want to convert it back into a vtkDataSet to continue program.
    '''



    actor = vtkActor()
    actor.SetMapper(mapper)

    ren = vtkRenderer()
    ren.AddActor(actor)
    ren.SetBackground(0.1,0.3,0.1)

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren)

    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renWin)

    renWin.Start()

    interactor.Initialize()
    interactor.Start()

回答1:


    pixelspace = reader.GetPixelSpacing()
    spacing = image.GetSpacing()

    vtk_data = image.GetPointData().GetScalars()

    numpy_data = numpy_support.vtk_to_numpy(vtk_data)

    numpy_data = numpy_data.reshape(dims[0], dims[1], dims[2])
    numpy_data = numpy_data.transpose(2,1,0)

    dims = numpy_data.shape

    dataImporter = vtkImageImport()
    dataImporter.SetDataScalarTypeToFloat() 
    dataImporter.SetNumberOfScalarComponents(1)
    dataImporter.SetDataExtent(0, dims[2]-1, 0, dims[1]-1, 0, dims[0]-1)
    dataImporter.SetWholeExtent(0, dims[2]-1, 0, dims[1]-1, 0, dims[0]-1)
    dataImporter.SetDataSpacing(spacing[0],spacing[1],spacing[2])
    dataImporter.CopyImportVoidPointer(numpy_data, numpy_data.nbytes)


来源:https://stackoverflow.com/questions/12754971/how-to-convert-a-3d-vtkdataset-into-a-numpy-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!