[Neuroimaging] Cannot edit some NIfTI header fields

Matthew Brett matthew.brett at gmail.com
Thu Aug 27 12:09:11 CEST 2015


Thanks for re-posting, that's kind of you.

On Tue, Aug 25, 2015 at 7:26 PM, Horea Christian <h.chr at mail.ru> wrote:
> Hi, I am experiencing some issues with editing my NIfTI headers.
> Apparently I can save some tags after I edit them, but not others.
> I have pasted all my outputs here: https://neurostars.org/p/3362/
> Can you help me out?
> I have received the following suggestion:
>>Just guessing, but I wonder if you are trying to edit the fields
>>supporting the affines, and then saving the image?
>>If you are doing this, then the image save takes the image affine and
>>applies it to the header, overwriting any changes you made the qform
>>fields, including pixdim.
>>If you want to change the qform / sform affine in the image header,
>>use the ``img.set_qform`` and `img.set_sform`` methods.
> I have looked into these functions, and I cannot figure out how I could
> use them to effect the changes I waqnt in the header. The most
> important thing right now would be to be able to edit pixdim[3]. For
> some reason this is set to the slice spacing (which is 0.5mm) but I
> want it set to (and I believe it *should* be set to) the slice
> thickness, which is 0.3mm.

I don't think that pixdim[3] is meant to be slice thickness - but did
you have some reference to say that is should be?

I ask because the pixdims are often used to construct the image
affine, and this definitely does refer to the slice spacing rather
than the slice thickness.

In particular, the pixdims are used to construct the 'qform' affine,
and therefore also get set when setting the qform affine.

In fact, you're running into trouble here because the image object
takes some care to set the qform and sform affine into the header, on
save, unless you tell it not to somehow.

In this case,  your simplest option is to:

* tell the image that the qform is not defined, and therefore should
not be set when saving the image;
* set the pixdims as you like.

Something like:

img.header['pixdim'][3] = 0.3
nib.save(img, 'my_image.nii')

Does that work?



More information about the Neuroimaging mailing list