简体   繁体   English

Python中如何改变SimpleITK图像的方向

[英]How to change the orientation of SimpleITK image in Python

I used this code to read a series for dicom images into python我使用这段代码将一系列 dicom 图像读入 python

series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
series_reader = sitk.ImageSeriesReader()
series_reader.SetFileNames(series_file_names)
series_reader.LoadPrivateTagsOn()
image3D=series_reader.Execute()
size = image3D.GetSize()

currently, its orientation is the following:目前,其方向如下:

在此处输入图像描述

I need to change the orientation to the following orientation:我需要将方向更改为以下方向:

在此处输入图像描述

Is there any command in python to be able to change the orientation of SimpleITK image? python 中是否有任何命令能够更改 SimpleITK 图像的方向?

Extending your code to perform this I would look at building a new image from your loaded in one and then adding a custom direction to it.扩展你的代码来执行这个我会考虑从你加载的一个图像中构建一个新图像,然后向它添加一个自定义方向。 Althernatively ITK has a orientimage filter in the python wrapping available.或者,ITK 在可用的 python 包装中有一个 orientimage 过滤器。 This is not in simpleitk but might solve your problem这不是简单的,但可能会解决您的问题

import SimpleITK as sitk

series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
series_reader = sitk.ImageSeriesReader()
series_reader.SetFileNames(series_file_names)
series_reader.LoadPrivateTagsOn()
image3D=series_reader.Execute()
size = image3D.GetSize()

#get image data
image_out = sitk.GetImageFromArray(sitk.GetArrayFromImage(img))

#setup other image characteristics
image_out.SetOrigin(img.GetOrigin())
image_out.SetSpacing(img.GetSpacing())
#set to RAI
image_out.SetDirection(tuple(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0))
sitk.WriteImage(image_out, 'test.mha')

I needed to reorient NIfTI images (files with.nii or.nii.gz extension) and the above solution didn't work for me.我需要重新定向 NIfTI 图像(扩展名为 .nii 或 .nii.gz 的文件),而上述解决方案对我不起作用。 After some research in the SimpleITK library I found the function sitk.DICOMOrient() which worked for me.在 SimpleITK 库中进行一些研究后,我发现 function sitk.DICOMOrient()对我有用。

Adjusting to your example (Tested in version 2.1.1 of SimpleITK and Python 3.7.1):调整您的示例(在 SimpleITK 2.1.1版和 Python 3.7.1 中测试):

# Given a sitk image instance -> img

reoriented = sitk.DICOMOrient(img, 'LPS')

It is also possible to test multiple permutations of the orientation labels, check this doc to understand the about the meaning of the labels.也可以测试方向标签的多个排列,查看此文档以了解标签的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM