# Direction/Orientation matrix DICOM vs Nifti

**URL:** <https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289>\
**Category:** Algorithms\
**Tags:** dicom, nifti, simpleitk, registration, elastix\
**Created:** [July 16, 2020, 5:59am UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289 "2020-07-16T05:59:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcorredorj](https://discourse.itk.org/user_avatar/discourse.itk.org/rcorredorj/32/37_2.png) [@rcorredorj](https://discourse.itk.org/u/rcorredorj)\
**Post date:** [July 16, 2020, 5:59am UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/1 "2020-07-16T05:59:25Z")

</div>

Hi everyone,

I’m having **discrepancies between the orientation matrix read from DICOMs or from a Nifti** generated from the same DICOM series. I’d like to understand what is going on.

I have a DICOM series that I read in the normal way using the ImageSeriesReader:

```
itkreader = sitk.ImageSeriesReader()
dicom_names = itkreader.GetGDCMSeriesFileNames(r"C:\tmp\DICOM_series")
itkreader.SetFileNames(dicom_names)
itkImage = itkreader.Execute()

```

Then, I save this image as Nifti and MHD using a ImageFileWriter and I read again using a ImageFileReader. I get the following values for the matrices:

**DICOM** :  
(0.0003620451755701781, -0.04382704694809889, -0.9990390677441499, 0.9967569623264217, -0.08037690612819795, 0.003887288909150347, -0.08047003775999609, -0.9958005537842747, 0.04365581411263355)

**MHD** :  
(0.0003620451755701781, -0.04382704694809889, -0.9990390677441499, 0.9967569623264217, -0.08037690612819795, 0.003887288909150347, -0.08047003775999609, -0.9958005537842747, 0.04365581411263355)

**Nifti** :  
(0.00036201611129199813, -0.0438270716099914, -0.9990390665949941, 0.9967569603844949, -0.08037693021218177, 0.0038872631696397915, -0.0804700619447843, -0.9958005507548984, 0.04365584270235757)

You may notice that MHD preserves the orientation matrix that was originally in the DICOM. However, the Nifti is changing the values. It’s usually from the 6th/7th decimal, but unfortunately registration tools such as elastix are sensitive to these changes and produce little differences in the final result (even while setting the Tolerance to 6 decimals).

Does anyone know how to preserve the same orientation in the Nifti ? Does ITK recomputes the affine matrix from the quaternions and this produces the differencesi in the matrices?

Thanks in advance!!

RaC

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 16, 2020, 1:10pm UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/2 "2020-07-16T13:10:01Z")

</div>

Hello @rcorredorj,

Looking at the ITK nifti code, what you are observing is due to loss of precision. It is a feature of the nifti format where the qform is stored in single precision in nifti and in DICOM/mha the information is in double precision. The ITK code mentions this [here](https://github.com/InsightSoftwareConsortium/ITK/blob/85bf4d98c636bd41fae90df8789168a0f04e6c1b/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L1924).

Unfortunately, what you are seeing is a limitation of the nifti file format. You will have to use another format if you require this level of precision.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 16, 2020, 2:52pm UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/3 "2020-07-16T14:52:41Z")

</div>

There was a [PR](https://github.com/InsightSoftwareConsortium/ITK/pull/1868) which was aimed at improving this about a month ago. Are you using ITK version before or after this was merged?

---

<div class="post-metadata">

**Author:** ![rcorredorj](https://discourse.itk.org/user_avatar/discourse.itk.org/rcorredorj/32/37_2.png) [@rcorredorj](https://discourse.itk.org/u/rcorredorj)\
**Post date:** [July 16, 2020, 3:09pm UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/4 "2020-07-16T15:09:51Z")

</div>

Thank you both for your replies.

@zivy A bit scary considering that Nifti is becoming more and more the standard for processing pipelines. Even BIDS that is taking a lot of attention nowadays is basically based on Niftis. I opened a related post to this in the BIDS Discourse (Neurostar) related to this topic: [https://neurostars.org/t/direction-orientation-matrix-dicom-vs-nifti/14382](https://neurostars.org/t/direction-orientation-matrix-dicom-vs-nifti/14382)

@dzenanz Much before … We are using ITK 4.11.0 and tricky to move to ITK 5 as it is already used/linked in multiple tools running with 4.11. Thanks for the info anyways! Basically, does this patch increase the precision in the Niftis q\_form to make it more compatible with the original values in the itkImage ?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 16, 2020, 3:38pm UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/5 "2020-07-16T15:38:46Z")

</div>

> [@rcorredorj](#):
>
> does this patch increase the precision in the Niftis q\_form

No. It uses sform in some situations where it is possible, hoping it would be more precise.

---

<div class="post-metadata">

**Author:** ![rcorredorj](https://discourse.itk.org/user_avatar/discourse.itk.org/rcorredorj/32/37_2.png) [@rcorredorj](https://discourse.itk.org/u/rcorredorj)\
**Post date:** [July 16, 2020, 3:41pm UTC](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289/6 "2020-07-16T15:41:50Z")

</div>

Ok. Understood. Thanks for the info. For the mean time, I may use the MHD then for parts in the pipelines that might be sensitive to this numerical issues.
