[Neuroimaging] change nifti header in nibabel

Ze Wang redhatw at gmail.com
Wed Jun 16 15:43:26 EDT 2021


That is my final solution now.
Btw, I have figured it out to trick SPM to read the other volumes by 
changing the volume field after using spm_vol to read the header of the 
nii.gz file. A simple trick is below (if anyone is interested):
v=spm_vol('wrong4dfile.nii.gz');
v.dt=[n 1];  % read the n-th volume into memory
dat=spm_read_vols(v);

Thanks
Ze

------ Original Message ------
From: "Nate Vack" <njvack at wisc.edu>
To: "Ze Wang" <redhatw at gmail.com>; "Neuroimaging analysis in Python" 
<neuroimaging at python.org>
Sent: 6/16/2021 2:28:14 PM
Subject: Re: [Neuroimaging] change nifti header in nibabel

>I might be mistaken here, but your easiest path forward might be to use 
>your existing C++ code to to fix the dimensionality in the header and 
>then read the files normally with nibabel.
>
>-n
>
>On Wed, Jun 16, 2021 at 9:34 AM <redhatw at gmail.com> wrote:
>>Hi,   Does anyone know how to change the dimension of the nifti image 
>>and save back to disk?   My nifti files should be a 4D image but my 
>>c++ code wrongly set the dimensionality to be 3 rather than 4.  The 
>>data were saved correctly and can be read using mricron. Certainly, 
>>FSL and SPM and nibabel can all only read the first volume.
>>I tried the following way to read the data using nibabel:
>>v=nib.load('file.nii.gz')
>>v.header['dim'][0]=4
>>dat=v.get_fdata()
>>But it still failed to read the 4D data.  My question is: 1. how to 
>>let nibabel know that the updated header information should be used 
>>when read data?  2. how to save the new header back to the file 
>>without loading the data?
>>
>>Thanks
>>Ze
>>_______________________________________________
>>Neuroimaging mailing list
>>Neuroimaging at python.org
>>https://mail.python.org/mailman/listinfo/neuroimaging
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20210616/1bd50187/attachment.html>


More information about the Neuroimaging mailing list