[Neuroimaging] Using nibabel to merge 3D image pairs into single-file 4D Nifti1

Matthew Brett matthew.brett at gmail.com
Fri Mar 24 17:10:18 EDT 2017


Hi,

On Fri, Mar 24, 2017 at 8:57 PM, Dav Clark <davclark at gmail.com> wrote:
> So, this is a post on stack overflow:
>
> http://stackoverflow.com/questions/33737282/copy-header-when-merging-multiple-mri-images-using-nibabel
>
> I'm doing something more or less equivalent:
>
> ```
> sample_path = 'some_path/some_sub*.hdr'
> fnames = glob(sample_path)
> imgs = [nib.load(fn) for fn in fnames]
>
> img_data = np.stack([img.dataobj for img in imgs], axis=-1)
>
> converted = nib.Nifti1Image(img_data, imgs[0].affine, imgs[0].header)
> converted.to_filename('some_filename.nii.gz')
> ```
>
> But the output ends up having a severely restricted range (thus
> rendering by default as black in fslview) as compared to fslmerge -t.
> fslmerge gets the TR right, and the nibabel version sets TR (pixdim4)
> to 1.0. Moreover imgs[0].header.get_slice_times() results in an error.
>
> Also, while the dimensions appear to be the same, I get different
> shaped time-series (as well as different absolute values, of course)
> from the two different conversion paths. I suspect I'm missing a
> scaling parameter from the files.
>
> For now, I am just using fslmerge -t, but since at least some other
> person out there has also done this (see so question linked above), I
> figure it's worth asking.
>
> If I could get this to work, it becomes very easy to implement in dask
> and take advantage of the spiffy computer I now have access to...

Did you try:

converted = nib.concat_images(fnames)

? I guess the correct TR and slice timing info is correctly set in the
first image header - I mean:

first_header = nib.load(fnames[0]).header

> ps - is there a searchable archive of the list? If not, I have a buddy
> who is pretty facile with mailman listservs...

Actually, no, I don't think we have - would be very glad of help.
We're using the same python.org mailman hosting as scipy and numpy, so
a general solution would be generally useful.

Cheers,

Matthew


More information about the Neuroimaging mailing list