如何使用更改轴SimpleTk::ImageSeriesWriter?

2024-10-03 06:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

ImageSeriesWriter默认为沿Z轴剖切给定的三维体积,并在XY视图中写入二维图像切片。在

如何更改轴以使输出在XZ或YZ视图中?在

换言之,如果默认的Z轴切片是轴向视图,如何获得冠状和矢状视图的切片?在

我尝试了GitHub:FNNDSC/med2image的输出xyz函数。 但是图像数组是盲目写入的,所以有时X和Y会被转置,或者其中一个轴被反转(翻转)。 所以我觉得有必要写我自己的代码来完全控制。在

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)

上面的代码将成功地写出Z轴的切片。 如何修改代码,以便可以获得另两个视图的切片?在


Tags: 代码图像image视图size切片slicefolder