Search code examples
pythonvtkdicom

Using vtkVolume16Reader without FilePrefix


I'm trying to visualize series of dicom images following example Medical3.py from official VTK repo. All examples I have found in python uses class vtkVolume16Reader which expects defined filenames pattern <%s>.<%d>. Does it mean that there are no API for reading image series with other name patterns?

In C++ there are example ReadDICOMSeries that doesn't require specific filename pattern. It would be great if Python wrapper has some analogue.


Solution

  • Based on C++ ReadDICOMSeries example I have rewritten analogue with using Observer for moving between slices (it is much better to customize vtkInteractorStyleImage class). Here is simple example where you start on 50th slice and can to move to 60th by pressing 'k' or to 40th by pressing 'l':

    import sys
    import numpy as np
    import vtk
    
    
    PathDicomDir = "/path/to/dicom/dir"
    reader = vtk.vtkDICOMImageReader()
    reader.SetDirectoryName(PathDicomDir)
    reader.Update()
    
    viewer = vtk.vtkImageViewer2()
    viewer.SetInputData(reader.GetOutput())
    viewer.SetSlice(50)
    
    def Keypress(obj, event):
        key = obj.GetKeySym()
        if key == "e":
            obj.InvokeEvent("DeleteAllObjects")
            sys.exit()
        elif key == "k":
            viewer.SetSlice(60)
            print("next")
        elif key == "l":
            print("prev")
            viewer.SetSlice(40)
    
    iren = vtk.vtkRenderWindowInteractor()
    iren.AddObserver("KeyPressEvent", Keypress)
    
    viewer.SetupInteractor(iren)
    
    viewer.Render()
    viewer.GetRenderer().ResetCamera()
    viewer.Render()
    iren.Start()
    

    P.S. There are two windows appear on run. Please let me know how to remove one of them.