[Python] Python 旋转、flip Nifti三维数据 (Python Rotate and Flip Nifti volume)

[python]  view plain  copy
  1. __author__ = 'xin yang'  
  2.   
  3.   
  4. import os  
  5. import nibabel as nib  
  6. import numpy as np  
  7. import math  
  8.   
  9.   
  10. src_us_folder = 'G:/Temp/Data/src/us'  
  11. src_seg_folder = 'G:/Temp/Data/src/seg'  
  12.   
  13. aug_us_folder = 'G:/Temp/Data/aug/us'  
  14. aug_seg_folder = 'G:/Temp/Data/aug/seg'  
  15.   
  16. img_n= 10  
  17. rotate_theta = np.array([0, math.pi/2])  
  18.   
  19. # augmentation  
  20. aug_cnt = 0  
  21. for k in range(img_n):  
  22.     src_us_file = os.path.join(src_us_folder, (str(k) + '.nii'))  
  23.     src_seg_file = os.path.join(src_seg_folder, (str(k) + '_seg.nii'))  
  24.     # load .nii files  
  25.     src_us_vol = nib.load(src_us_file)  
  26.     src_seg_vol = nib.load(src_seg_file)  
  27.     # volume data  
  28.     us_vol_data = src_us_vol.get_data()  
  29.     us_vol_data = (np.array(us_vol_data)).astype('uint8')  
  30.     seg_vol_data = src_seg_vol.get_data()  
  31.     seg_vol_data = (np.array(seg_vol_data)).astype('uint8')  
  32.     # get refer affine matrix  
  33.     ref_affine = src_us_vol.affine  
  34.   
  35.     ############### flip volume ###############  
  36.     flip_us_vol = np.fliplr(us_vol_data)  
  37.     flip_seg_vol = np.fliplr(seg_vol_data)  
  38.     # construct new volumes  
  39.     new_us_vol = nib.Nifti1Image(flip_us_vol, ref_affine)  
  40.     new_seg_vol = nib.Nifti1Image(flip_seg_vol, ref_affine)  
  41.     # save  
  42.     aug_us_file = os.path.join(aug_us_folder, (str(aug_cnt) + '.nii'))  
  43.     aug_seg_file = os.path.join(aug_seg_folder, (str(aug_cnt) + '_seg.nii'))  
  44.     nib.save(new_us_vol, aug_us_file)  
  45.     nib.save(new_seg_vol, aug_seg_file)  
  46.   
  47.     aug_cnt = aug_cnt + 1  
  48.   
  49.     ############### rotate volume ###############  
  50.     for t in range(len(rotate_theta)):  
  51.         print 'rotating %d theta of %d volume...' % (t, k)  
  52.         cos_gamma = np.cos(t)  
  53.         sin_gamma = np.sin(t)  
  54.         rot_affine = np.array([[1000],  
  55.                                [0, cos_gamma, -sin_gamma, 0],  
  56.                                [0, sin_gamma, cos_gamma, 0],  
  57.                                [0001]])  
  58.         new_affine = rot_affine.dot(ref_affine)  
  59.         # construct new volumes  
  60.         new_us_vol = nib.Nifti1Image(us_vol_data, new_affine)  
  61.         new_seg_vol = nib.Nifti1Image(seg_vol_data, new_affine)  
  62.         # save  
  63.         aug_us_file = os.path.join(aug_us_folder, (str(aug_cnt) + '.nii'))  
  64.         aug_seg_file = os.path.join(aug_seg_folder, (str(aug_cnt) + '_seg.nii'))  
  65.         nib.save(new_us_vol, aug_us_file)  
  66.         nib.save(new_seg_vol, aug_seg_file)  
  67.   
  68.         aug_cnt = aug_cnt + 1  


[Note]

This NiBabel toolbox based rotation only changes the affine matrix information in the header, that is, the volume data is not affected. When you use ITK-Snap to load the .nii file, the data will be transformed according to the affine matrix. That means, the transformation is done by the ITK-Snap software.

This kind of transformation can't be recognised by Nifti toolbox in Matlab, when you use 'load_nii' function to load the file generated above. You will encounter the problem like:

'Non-orthogonal rotation or shearing found inside the affine matrix in this NIfTI file.'

You need to really 'reslice' the volume data under the affine matrix, as the following code shows:

[plain]  view plain  copy
  1. rot_nii_path = 'G:\Temp\Data\aug\0.nii';  
  2. res_rot_nii_path = 'G:\Temp\Data\aug\0_res.nii';  
  3. reslice_nii(rot_nii_path, res_rot_nii_path, [], [], [], 2);  
You can refer to the source file of  Nifti toolbox in Matlab  for the parameter setting of 'reslice_nii' function. You should pay attention to the last parameter for interpolation. 'Nearest' and 'Linear' will generate different data types.
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值