Direction/Orientation matrix DICOM vs Nifti

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")
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:

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

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

(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!!


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.

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.


There was a PR which was aimed at improving this about a month ago. Are you using ITK version before or after this was merged?


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:

@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 ?

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

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.