[英]How to check NIFTI image is in the right orientation/position with Python
對於您的第一個問題,您應該檢查 NIfTI 圖像的方向,然后將其旋轉到您想要的方向。 為了檢查方向,我們使用nibabel.orientations.aff2axcodes
https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.aff2axcodes
對於旋轉,我們將使用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
請注意,在您的情況下,您只需要翻轉 z 軸,但請嘗試使用上面的 function 並更改您的情況以檢查您的數據。
最后一件事是將新的 NIfTI 掃描(NumPy 數組)保存為nii
圖像。 為此,請使用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')
ct_arr
是您的 numpy 數組(確保是浮點類型)。 第二個參數是加載后的原始 NIfTI 掃描。 在同一個 function 中,確保在文件名中添加后綴.nii
或.nii.gz
希望清楚:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.