# Wrong header when reading a NIFTI file

**URL:** https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [November 23, 2022, 9:03am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520 "2022-11-23T09:03:57Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [November 23, 2022, 9:03am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/1 "2022-11-23T09:03:57Z")

</div>

Hi, I am a beginner in SimpleITK.  
I used SimpleITK to load a nifti image and view its header information. The pixdim[0] header is changed from -1 to 0. I used ITKsnap, nifti-tool and nibabel to see the header information and they all show -1. Can anyone help me with this? I don’t know why this is happening.  
This is the code I used to view the header in Simpleitk

```auto
brain_ncct_img = sitk.ReadImage(ncct_file)
brain_ncct_data = sitk.GetArrayFromImage(brain_ncct_img)

print("header using simpleitk")
for k in brain_ncct_img.GetMetaDataKeys():
    v = brain_ncct_img.GetMetaData(k) 
    print("({0}) = = \"{1}\"".format(k,v))

```

This is the result I am geeting:

```auto
header using simpleitk
(ITK_FileNotes) = = "TE=3e+02;Time=172913.193"
(ITK_original_direction) = = "[UNKNOWN_PRINT_CHARACTERISTICS]
"
(ITK_original_spacing) = = "[UNKNOWN_PRINT_CHARACTERISTICS]
"
(aux_file) = = ""
(bitpix) = = "16"
(cal_max) = = "0"
(cal_min) = = "0"
(datatype) = = "4"
(descrip) = = "TE=3e+02;Time=172913.193"
(dim[0]) = = "3"
(dim[1]) = = "512"
(dim[2]) = = "512"
(dim[3]) = = "30"
(dim[4]) = = "1"
(dim[5]) = = "0"
(dim[6]) = = "0"
(dim[7]) = = "0"
(dim_info) = = "0"
(intent_code) = = "0"
(intent_name) = = ""
(intent_p1) = = "0"
(intent_p2) = = "0"
(intent_p3) = = "0"
(nifti_type) = = "1"
(pixdim[0]) = = "0"
(pixdim[1]) = = "0.443359"
(pixdim[2]) = = "0.443359"
(pixdim[3]) = = "4.79859"
(pixdim[4]) = = "0"
(pixdim[5]) = = "0"
(pixdim[6]) = = "0"
(pixdim[7]) = = "0"
(qform_code) = = "1"
(qform_code_name) = = "NIFTI_XFORM_SCANNER_ANAT"
(qoffset_x) = = "81.2783"
(qoffset_y) = = "99.1478"
(qoffset_z) = = "-85.4728"
(quatern_b) = = "0"
(quatern_c) = = "0.997859"
(quatern_d) = = "0.0654031"
(scl_inter) = = "-1024"
(scl_slope) = = "1"
(sform_code) = = "1"
(sform_code_name) = = "NIFTI_XFORM_SCANNER_ANAT"
(slice_code) = = "0"
(slice_duration) = = "0"
(slice_end) = = "0"
(slice_start) = = "0"
(srow_x) = = "-0.443359 0 -0 81.2783"
(srow_y) = = "-0 0.439566 -0.626342 99.1478"
(srow_z) = = "0 0.05787 4.75754 -85.4728"
(toffset) = = "0"
(vox_offset) = = "352"
(xyzt_units) = = "10"

```

This is the header information from ITKSnap.  
 ![s1](https://discourse.itk.org/uploads/default/original/2X/c/c3fa10feda0256253e42cbf44f2dc59e2b4662a0.png)  
 ![s2](https://discourse.itk.org/uploads/default/original/2X/4/49a9ea4d894218aeeccfc324b6ad07cd343011c8.png)

![s3](https://discourse.itk.org/uploads/default/original/2X/5/5b80427c276437072d8036b2e16a8796f390722f.png)  
 ![s4](https://discourse.itk.org/uploads/default/original/2X/5/5f60776baf96b5d877a6583d173c5154d7c1afd6.png)

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [November 23, 2022, 4:04pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/2 "2022-11-23T16:04:13Z")

</div>

What does SimpleITK say the voxel spacing say? Try this:

```
print(brain_ncct_img.GetSpacing())

```

That’s what SimpleITK actually uses for spacing once the image is loaded.

---

<div class="post-metadata">

### Author: ![cookpa](https://discourse.itk.org/user_avatar/discourse.itk.org/cookpa/32/255_2.png) [@cookpa](https://discourse.itk.org/u/cookpa)
#### Post date: [November 23, 2022, 6:53pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/3 "2022-11-23T18:53:19Z")

</div>

It looks like this was a bug that was recently fixed in ITK

> <https://github.com/InsightSoftwareConsortium/ITK/commit/9eb4c1209d215eed244e5e0a1d8f15942a61cb76>
>
> bug #3241: In f38b1dd I accidentally changed some behaviour. Specifically, the v…alue of pixdim\[0\] in itk::NiftiImageIO metadata is different before and after the change.
> 
> Previous to the change, SetImageIOMetadataFromNIfTI() was populated from the nifti\_1\_header structure. After the change, it is populated from the nifti\_image structure. The latter is created from the former with nifti\_convert\_nhdr2nim(). Most of the fields used in SetImageIOMetadataFromNIfTI() have 1-to-1 correspondences in these two structures. I expected all 8 elements of the pixdim array were the same in both structures, but in fact no; element 0 is special. nifti\_convert\_nhdr2nim() unconditionally sets pixdim\[0\] to 0.0 (by virtue of allocating the structure with calloc).
> 
> In my own app, I was using pixdim\[0\] (retrieved from itk::NiftiImageIO) as the last parameter to nifti\_quatern\_to\_mat44(). I see now looking at niftilib itself that I should be passing qfac as the last parameter. But the itk::MetaDataObject doesn't include it, so this commit adds qfac to the metadata dictionary.
> 
> Additionally, we now include the whole qto\_xyz matrix, so that ITK clients that call nifti\_quatern\_to\_mat44() don't even need to. This plugs a leaky abstraction and allows my own app for example to not even know that ITK is implemented via niftilib, and never need to call down to it.
> 
> Note that the behaviour change of f38b1dd is not being reverted, because it's been four years now, no one else has noticed, and some people may be relying on this behaviour now.

What version of SimpleITK do you have?

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [November 24, 2022, 12:43am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/4 "2022-11-24T00:43:27Z")

</div>

@cookpa I am using version 2.2.0

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [November 24, 2022, 12:49am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/5 "2022-11-24T00:49:35Z")

</div>

@dchen This is the voxel spacing.

 ![s5](https://discourse.itk.org/uploads/default/original/2X/a/a8813af25d2af47a48296ff11714e8687a44c4e2.png)

---

<div class="post-metadata">

### Author: ![cookpa](https://discourse.itk.org/user_avatar/discourse.itk.org/cookpa/32/255_2.png) [@cookpa](https://discourse.itk.org/u/cookpa)
#### Post date: [November 28, 2022, 10:26pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/6 "2022-11-28T22:26:33Z")

</div>

In SimpleITK 2.2.0, I see the qfac metadata that appears to be missing from your image. I’m not sure how to explain that.

```auto
'qfac' in brain_ncct_img.GetMetaDataKeys()

```

returns True for me. If I write the image back out, it sets pixdim[0] to “-1”. However, internally, the pixdim array has pixdim[0] = 0.

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [November 29, 2022, 1:43am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/7 "2022-11-29T01:43:45Z")

</div>

The qfac is stored in pixdim[0], right? I don’t want to write the image. I am more concerned with the pixdim[0]. Is there any way we can solve this?

---

<div class="post-metadata">

### Author: ![cookpa](https://discourse.itk.org/user_avatar/discourse.itk.org/cookpa/32/255_2.png) [@cookpa](https://discourse.itk.org/u/cookpa)
#### Post date: [November 29, 2022, 1:54pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/8 "2022-11-29T13:54:02Z")

</div>

I’m not sure I understand why `pixdim[0]` is important outside of I/O. Once you have read the image, it has an ITK transform that can take points to and from the physical space.

[https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html](https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html)

Is that transform not correct for your images?

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [November 29, 2022, 2:04pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/9 "2022-11-29T14:04:32Z")

</div>

I have nifti data with header pixdim[0] “-1” and when I read it using simpleitk and plot it using matplotlib, the image is upside down. I am guessing it has to do something with pixdim[0] and I wanted to test theory but while reading simpleitk is making pixdim[0] “0” and if pixdim[0] is 0, i think we use qfac is 1.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [November 29, 2022, 2:26pm UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/10 "2022-11-29T14:26:16Z")

</div>

It seems like ITK/SimpleITK might be re-doing the image orientation information. But, as Philip is asking, the orientation might work out to be the same. And if you’re using matplotlib to display an image, are you even using that orientation information?

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [December 4, 2022, 9:09am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/11 "2022-12-04T09:09:17Z")

</div>

I am actually using the array from this code sitk.GetArrayFromImage(brain\_ncct\_image) to create montage and sometimes the images in the montage is upside down but when I view it using ITK-snap it looks ok.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [December 5, 2022, 11:53am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/12 "2022-12-05T11:53:01Z")

</div>

Accessing the image as a numpy array also ignores any orientation information. Perhaps you need to look at the orientation matrix for each image and decide if you need to flip the pixels.

---

<div class="post-metadata">

### Author: ![niruta](https://discourse.itk.org/user_avatar/discourse.itk.org/niruta/32/3558_2.png) [@niruta](https://discourse.itk.org/u/niruta)
#### Post date: [January 25, 2023, 7:35am UTC](https://discourse.itk.org/t/wrong-header-when-reading-a-nifti-file/5520/13 "2023-01-25T07:35:26Z")

</div>

Thanks @dchen . I was able to solve the issue.
