0

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?

kaltu
  • 330
  • 5
  • 17

2 Answers2

2

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.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18
  • It would be nicer if you could provide a small code snippet to explain how `PermuteAxes` work. Appreciated! – kaltu Feb 02 '19 at 17:27
0

Well, I think you've fixed your issue. But what I've done is just importing a .mha file (or another extension supported by simple ITK) and converting it to a 3D array. Then what you need to do is just slice this array in different axis at a time. Take a look (python code):

import SimpleITK as sitk #importing package
path = '/current/folder/mha/file'
ct = sitk.ReadImage(path) #var_type is SimpleITK.Image
ndarray = sitk.GetArrayFromImage(ct) #converting from SimpleITK.Image to numpy ndarray
# Axial view:
plt.imshow(ndarray[100,:,:], cmap='gray') # plotting 100º image from axial view
#Coronal view:
plt.imshow(ndarray[:,100,:], cmap='gray') # plotting 100º image from coronal view
#Sagittal view:
plt.imshow(ndarray[:,:,100], cmap='gray') # plotting 100º image from sagittal view
Luan Souza
  • 159
  • 1
  • 1
  • 11