# BUG when reading mha file with AnatomicalOrientation

**URL:** <https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038>\
**Category:** Development\
**Tags:** file-export, file-import\
**Created:** [June 5, 2019, 4:15pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038 "2019-06-05T16:15:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [June 5, 2019, 4:15pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/1 "2019-06-05T16:15:35Z")

</div>

I’m working on adding support for orientation in VTK image data and what depends on it (filters, mappers, IO… more details [here](https://gitlab.kitware.com/vtk/vtk/issues/17480)).

I was working on the [MetaIO](https://itk.org/Wiki/ITK/MetaIO/Documentation) (.mha, .mhd) reader and tested using a sample dataset with the following header:

```auto
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = True
CompressedDataSize = 1486837
TransformMatrix = 1 7.5286998857393448e-16 4.3888503942213362e-16 7.4766581814600425e-16 -1 -3.8487811825327185e-47 0 0 -1
Offset = -9.360019529721862e-29 360.00000000000006 332.5
CenterOfRotation = 0 0 0
AnatomicalOrientation = RPS
ElementSpacing = 1.4062499999999947 1.4062500000000002 2.4999999999999813
DimSize = 256 256 133
ElementType = MET_UCHAR
ElementDataFile = LOCAL

```

Note that the anatomical orientation for this file is `RPS`, spacing is ~ [1.4, 1.4, 2.5] and the orientation matrix is, after rounding:  
1 0 0  
0 -1 0  
0 0 -1

In VTK the system is XYZ=LPS, so after flipping the first axis, the orientation matrix becomes, in LPS:  
-1 0 0  
0 -1 0  
0 0 -1

Now… I use 3DSlicer as the ground truth in term of orientation when I test things for VTK, and the data read in my VTK example with the MHA reader was flipped along the Sagittal axis (L/R) compared to 3D Slicer…

I then proceed to export the dataset to NRRD from Slicer, and this is the resulting header:

```auto
NRRD0004
# Complete NRRD file format specification at:
# http://teem.sourceforge.net/nrrd/format.html
type: unsigned char
dimension: 3
space: left-posterior-superior
sizes: 256 256 133
space directions: (1.4062499999999947,1.0587234214320913e-15,6.171820866873731e-16) (1.0514050567678187e-15,-1.4062500000000002,-5.4123485379366359e-47) (0,0,-2.4999999999999813)
kinds: domain domain domain
encoding: gzip
space origin: (-9.360019529721862e-29,360.00000000000006,332.5)

```

With rounding, the direction matrix here is:  
1.4 0 0  
0 -1.4 0  
0 0 -2.5

If we extract the spacing (1.4, 1.4, 2.5) we get:  
1 0 0  
0 -1 0  
0 0 -1

That’s the same as in the MHA header… however, the `space` is now `left-posterior-superior` instead of `RPS` in MHA: here we find the flip along the X-axis again.

So two possible reasons:

1. MHA reader is incorrect
2. NRRD writer is incorrect

Given that my VTK implementation gives me a flip from the one loaded by Slicer, my guess is on number 1. I followed up by looking at [itkMetaImageIO](https://github.com/InsightSoftwareConsortium/ITK/blob/0b1f2da/Modules/IO/Meta/src/itkMetaImageIO.cxx) and there are numerous mentions of `AnatomicalOrientation` in [`MetaImageIO::Write`](https://github.com/InsightSoftwareConsortium/ITK/blob/0b1f2da/Modules/IO/Meta/src/itkMetaImageIO.cxx#L820-L1062), but none in [`MetaImageIO::Read`](https://github.com/InsightSoftwareConsortium/ITK/blob/0b1f2da/Modules/IO/Meta/src/itkMetaImageIO.cxx#L390-L402).

Based on all this, would you all agree we are indeed ignoring the anatomical orientation when reading MHA/MHD files?

cc: @dzenanz @thewtex

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 5, 2019, 6:05pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/2 "2019-06-05T18:05:09Z")

</div>

Hi @agirault - yes, this has come up a couple of times. I believe the post from Andras sums up the current understanding.

> [@Import mhd data](https://discourse.slicer.org/t/import-mhd-data/3389/2):
>
> Slicer uses ITK image reader to load metaimage files. This reader intentionally ignores AnatomicalOrientation (I’ve raised this question before to ITK developers and Kitware and they confirmed that this is not a bug and they will not change the behavior) and considers the image to be in LPS coordinate system. Slicer converts the image to RAS when it is loaded into the scene, and Slicer converts it back to LPS when it is saved to file. If you want to specify coordinate system using anatomical ax…

---

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [June 5, 2019, 6:44pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/3 "2019-06-05T18:44:35Z")

</div>

Right on, thank you Steve.

> [@pieper](#):
>
> This reader intentionally ignores AnatomicalOrientation (I’ve raised this question before to ITK developers and Kitware and they confirmed that this is not a bug and they will not change the behavior)

Well… we probably won’t have that in VTK, and I’m afraid that is going to confuse more than one. I don’t understand why it has to be like this in ITK: any of its maintainers would like to explain the argument (@dzenanz, @thewtex)?

Thanks!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 5, 2019, 7:08pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/4 "2019-06-05T19:08:06Z")

</div>

I agree, I don’t like having this standard field for “informational purposes” only (that readers should ignore), especially because similar field in nrrd is actually used by the readers.

However, I understand the reluctance in investing more time into improving the file format, as there are already too many file formats (metaio, nrrd, nifti, mgz, …) for storing biomedical images in research applications. It would be probably better if we would focus on maintenance and improvement of a single file format.

---

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [June 6, 2019, 6:58pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/6 "2019-06-06T18:58:01Z")

</div>

> [@Import mhd data](https://discourse.slicer.org/t/import-mhd-data/3389/2):
>
> > Does the **TransformationMatrix** specify rotation between (i,j,k) and the RAS coordinate?
> 
> Yes.

@lassoan in the other thread linked above, you mention that the TransforMatrix (is that different from the Orientation field?) is a rotation from ijk to RAS. However, when saved as a NRRD, the transformation matrix is the same, but in LPS, as shown above. So either:

1. the actual transform is IJKtoRAS but IJKtoLPS
2. is is IJKtoRAS but we read it as IJKtoLPS

Would you agree?

Also, I’m currently updating those readers & writers in VTK. What do you suggest I do for MetaIO?

1. Same as NRRD (use the AnatomicalOrientation as the target coordinate system)
2. enforce as LPS (do nothing)
3. enforce as RAS (flip I and J)

---

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [June 6, 2019, 7:57pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/7 "2019-06-06T19:57:32Z")

</div>

After discussion with @thewtex and @aylward, we agreed that the current integration of MetaIO inside ITK is incorrect, namely:

1. The reader ignores the anatomical orientation and assumes the data is always in LPS. It should instead adjust the orientation matrix described in the file to go from IJK to LPS if the anatomical orientation differs from LPS
2. The writer uses the direction matrix to infer an anatomical orientation (somehow?). It should instead write LPS and the associated direction matrix in that space.

As a result:

- We’ve agreed that this will need to be fixed in ITK.
- VTK will properly use those fields when adding orientation support.

---

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [June 12, 2019, 10:24pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/8 "2019-06-12T22:24:40Z")

</div>

FYI: an attempt at updating the MetaIO reader and writer in VTK can be found here: [https://gitlab.kitware.com/vtk/vtk/merge\_requests/5656](https://gitlab.kitware.com/vtk/vtk/merge_requests/5656)

---

<div class="post-metadata">

**Author:** ![agirault](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/agirault/32/74_2.png) [@agirault](https://discourse.slicer.org/u/agirault)\
**Post date:** [August 12, 2020, 1:33pm UTC](https://discourse.slicer.org/t/bug-when-reading-mha-file-with-anatomicalorientation/7038/9 "2020-08-12T13:33:03Z")

</div>

For the sake of cross-referencing/documenting, the change was NOT implemented as suggested in the last two comments, following a discussion that occurred in the ITK issue tracker: [https://github.com/InsightSoftwareConsortium/ITK/issues/1017](https://github.com/InsightSoftwareConsortium/ITK/issues/1017)
