Search code examples
pythonimagingmedicalsimpleitk

How Do I Change the Axis SimpleITK::ImageSeriesWriter Using?


The SimpleITK::ImageSeriesWriter default to slice given 3D volume along Z-axis and write slices of 2D images in XY view.

How do I change the axis so that the output is in XZ or YZ view?

In another word, if the default Z axis slices are in Axial view, how do I get the slices of Coronal and Sagittal view?

I tried the GitHub:FNNDSC/med2image's output xyz function. But the images array are blindly written, so sometimes the X and Y are transposed, or one of the axis are reversed(flipped). So I feel the need to write my own code to have full control.

def slice(dcm_folder, output_stem):
    print('Reading Dicom directory:', path.abspath(dcm_folder))
    reader = sitk.ImageSeriesReader()

    dicom_names = reader.GetGDCMSeriesFileNames(dcm_folder)
    reader.SetFileNames(dicom_names)

    image = reader.Execute()

    # cast the bit depth to PNG compatible "unsigned char"
    image = sitk.Cast(sitk.RescaleIntensity(image), sitk.sitkUInt8)

    size = image.GetSize()
    print( "Image size:", size[0], size[1], size[2] )

    # need Z filenames to write
    series_filenames = list([output_stem + '-slice' + str(i).zfill(3) + '.png' for i in range(size[2])])

    print('Writing {} image slices'.format(size[2]))
    writer = sitk.ImageSeriesWriter()
    writer.SetFileNames( series_filenames )
    writer.Execute(image)

The code above will write out slices of Z axis successfully. How do I modify the code so that I can get the slices of another 2 views?


Solution

  • You should be able to use the PermuteAxesImageFilter to swap the axes of your volume. Here's the documentation for that filter:

    https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1PermuteAxesImageFilter.html

    Or if you prefer a procedural interface (as I do), you can use the PermuteAxes function.