I have 180 .nii fMRI images for each patient and I need to flip them from right to left. I've tried using 'for' statement to batch processing these images. But it seems like The following code only produce one result. Here is the following code:
import nibabel as nib
import numpy as np
import os
NII_DIR='/Users/lena/testtest'
def get_filelist(dir, Filelist):
if os.path.isdir(dir):
for s in os.listdir(dir):
newDir = os.path.join(dir, s)
Filelist.append(newDir)
return Filelist
list = get_filelist(NII_DIR, [])
print(len(list))
type(list)
for i in list:
def read_nii_file1(nii_path):
nii_img=nib.load(nii_path)
return nii_img
nii_img=read_nii_file1(os.path.join(i))
from nilearn.image import new_img_like
print(nii_img.affine.shape)
print(nii_img.affine)
def flip_image(image, axis):
try:
new_data = np.copy(image.get_fdata())
for axis_index in axis:
new_data = np.flip(new_data, axis=axis_index)
except TypeError:
new_data = np.flip(image.get_fdata(), axis=axis)
return new_img_like(image, data=new_data)
new_image = flip_image(nii_img, axis=[0])
affine = new_image.affine.copy()
hdr = new_image.header.copy()
new_data = new_image.get_fdata()
new_nii = nib.Nifti1Image(new_data, affine, hdr)
#the following is the part I have no idea how to write
New_NII_DIR = ('/Users/lena/test_new/001.nii')
nib.save(new_nii, New_NII_DIR)
[here is the example of my data, including 180 phases][1] [1]: https://i.stack.imgur.com/wDh34.png