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

不想你离开。 提交于 2021-01-29 05:57:06

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/54160097/how-do-i-change-the-axis-simpleitkimageserieswriter-using

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