简体   繁体   English

如何使用 Python 检查 NIFTI 图像是否处于正确的方向/位置

[英]How to check NIFTI image is in the right orientation/position with Python

I am new here.我刚来这地方。 Currently learning about medical imaging data on Python and wondering if it is possible to identify the NIFti image is in the right orientation and how to do the transformation.目前正在学习 Python 上的医学成像数据,想知道是否可以识别 NIFti 图像的方向正确以及如何进行转换。 I looked up online and found some algorithm to 'rotate' the imaging data (in JPG format mainly) via affine transformation by certain angle but there's no documentation on how to do it on nifti image instead and save it as a new nifti image via nibabel.我在网上查找了一些算法,通过仿射变换将成像数据(主要以 JPG 格式)“旋转”一定角度,但没有关于如何在 nifti 图像上执行此操作并通过 nibabel 将其保存为新 nifti 图像的文档.

To illustrate what I meant.为了说明我的意思。 I would like to learn how to change this image 1 into this image我想学习如何将此图像 1更改为此图像

For your first question, you should check the orientation of your NIfTI image and then rotate it in the direction that you want.对于您的第一个问题,您应该检查 NIfTI 图像的方向,然后将其旋转到您想要的方向。 For checking orientation we use nibabel.orientations.aff2axcodes https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.aff2axcodes为了检查方向,我们使用nibabel.orientations.aff2axcodes https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.aff2axcodes

For the rotation we will use nibabel.orientations.flip_axis https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.flip_axis对于旋转,我们将使用nibabel.orientations.flip_axis https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.flip_axis

def check_orientation(ct_image, ct_arr):
    """
    Check the NIfTI orientation, and flip to  'RPS' if needed.
    :param ct_image: NIfTI file
    :param ct_arr: array file
    :return: array after flipping
    """
    x, y, z = nib.aff2axcodes(ct_image.affine)
    if x != 'R':
        ct_arr = nib.orientations.flip_axis(ct_arr, axis=0)
    if y != 'P':
        ct_arr = nib.orientations.flip_axis(ct_arr, axis=1)
    if z != 'S':
        ct_arr = nib.orientations.flip_axis(ct_arr, axis=2)
    return ct_arr

Note that in your case you need to flip only the z-axis, but try to use the function above and change to your situation in order to check your data.请注意,在您的情况下,您只需要翻转 z 轴,请尝试使用上面的 function 并更改您的情况以检查您的数据。

The last thing is to save the new NIfTI scan (NumPy array) as a nii image.最后一件事是将新的 NIfTI 扫描(NumPy 数组)保存为nii图像。 In order to do it, use Nifti1Image to create nii object, and then nibabel.save to save it to a file.为此,请使用Nifti1Image创建nii object,然后使用nibabel.save将其保存到文件中。

new_nifti = nib.Nifti1Image(<ct_arr>.astype(np.float), nii_original_scan.affine)
nib.save(new_nifti, f'< path to new scan >.nii.gz')

The ct_arr is your numpy array (make sure is in float type). ct_arr是您的 numpy 数组(确保是浮点类型)。 The second argument is the original NIfTI scan after loading.第二个参数是加载后的原始 NIfTI 扫描。 In the same function, make sure you add in the file name the suffix .nii or .nii.gz在同一个 function 中,确保在文件名中添加后缀.nii.nii.gz

Hope it's clear:)希望清楚:)

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

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