Splitting and Merging NII Files

I’m doing some DTI preprocessing, and need to split the nii file into chunks, do some processing, and then reassemble everything.

Basically, I want to slice off the 0-6 volumes, and then average them. Similarly, I want to take the remaining volumes (7-96) and also merge them into a separate file.

The below function worked well enough, and I could write something similar for the remaining volumes, but it’s kind of ugly…

So I am curious what’s the cleanest way to “slice” a volume and output the resulting slices using FSL tools… or any other NiPype wrapped functions I may not be aware of…

def getB0images( input_files ):
“”“This will return an image stack for merging “””
import os
b0_Images = []
b0_image_suffixes = [ ‘vol0000.nii.gz’,‘vol0001.nii.gz’,‘vol0002.nii.gz’,‘vol0003.nii.gz’,‘vol0004.nii.gz’,
‘vol0005.nii.gz’,‘vol0006.nii.gz’]
for fwp in input_files: #file w path
if os.path.basename(fwp) in b0_image_suffixes:
b0_Images.append(fwp) ###means I found one of the b0 image files I wanted
return b0_Images ### List of images to merge

getB0images_interface = Function(input_names=[“input_files”], output_names=[“b0_image_list”], function=getB0images)

1 Like

I’d recommend fslroi for this (ExtractROI in nipype)

Thanks! I knew there was a simple function to do it, just couldn’t remember what it was…