How to convert 4D nifti file to 3D

I have functional images which I need to average them across time. How can I do that and reduce the dimension from 4D to 3D?

Hi @maryam.riazi,

In Python:

import nibabel as nib

# Assuming your data is stored in a NIfTI file
img = nib.load('your_functional_images.nii.gz')

# Get the data array
data = img.get_fdata()

# Calculate the mean along the time axis (axis=3 for 4D data)
average_data = np.mean(data, axis=3)

# Create a new nibabel image with the averaged data
average_img = nib.Nifti1Image(average_data, img.affine)

# Save the averaged image to a new NIfTI file
nib.save(average_img, 'averaged_functional_images.nii.gz')

Or using FSL:

fslmaths input_func_image.nii.gz -Tmean output_average_image.nii.gz

Best,
Steven

thanks Steven.
That was helpful.

because google search keeps giving this page: here is also the nibabel function to turn a 4D image into a list of 3D:

https://nipy.org/nibabel/reference/nibabel.funcs.html#four-to-three