# MetaIO image rotation conventions

**URL:** https://discourse.itk.org/t/metaio-image-rotation-conventions/4525
**Category:** Uncategorized
**Tags:** rotation
**Created:** [October 26, 2021, 3:42pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525 "2021-10-26T15:42:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ingmar.voigt](https://discourse.itk.org/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@ingmar.voigt](https://discourse.itk.org/u/ingmar.voigt)
#### Post date: [October 26, 2021, 3:42pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/1 "2021-10-26T15:42:29Z")

</div>

For the TransformMatrix / Orientation / Rotation tags - which are storing the image rotation in the MetaIO (MHD/MHA) format - the actual serialization convention (row-major vs. column major) does not seem to be documented anywhere (at least I could not really find anything beyond [https://itk.org/Wiki/ITK/MetaIO/Documentation#Associated\_transformations](https://itk.org/Wiki/ITK/MetaIO/Documentation#Associated_transformations)).

As far as I could see from [MetaIO/metaObject.cxx at 5e87f062ea796204b84fc3e5e9100cd3ead2e015 · Kitware/MetaIO · GitHub](https://github.com/Kitware/MetaIO/blob/5e87f062ea796204b84fc3e5e9100cd3ead2e015/src/metaObject.cxx#L419) and [MetaIO/metaObject.cxx at 5e87f062ea796204b84fc3e5e9100cd3ead2e015 · Kitware/MetaIO · GitHub](https://github.com/Kitware/MetaIO/blob/5e87f062ea796204b84fc3e5e9100cd3ead2e015/src/metaObject.cxx#L648) the reference implementation seems to store it row-major (assuming that `_i` refer to rows and `_j` to columns respectively in `TransformMatrix(int _i, int _j`).

On the other hand, experiments with a mesh and an associated image (TransformMatrix = 0 0 1 1 0 0 0 1 0 - i.e. not invariant to transposition) in 3D Slicer suggest that the orientation is actually stored / interpreted as a column-major.

Finally, I actually wanted to do the same experiment with ParaView, which unfortunately seems to completely ignore the image orientation.

Would anyone be able to confirm what is the standard convention and where this is documented? I did find [22\_Transforms](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/22_Transforms.html) saying that “Matrices are represented by vector-like data types in row major order” but I am unsure whether this applies for the MetaIO/MHD/MHA file format or is only internal to SimpleITK? I guess this should be highly relevant for registration topics, no?

---

<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: [October 26, 2021, 3:44pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/2 "2021-10-26T15:44:24Z")

</div>

@Stephen_Aylward might be able to answer this.

---

<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: [October 26, 2021, 4:52pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/3 "2021-10-26T16:52:27Z")

</div>

Hello @ingmar.voigt,

The storage in the meta-io image is in column major order.

SimpleITK’s convention of representing a matrix is in row major order, so `[0,0,-1,0,1,0,1,0,0]` represents the matrix \left[\begin{array}{ccc} 0 & 0 & -1\\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{array}\right].  
Trust but verify:

```auto
import SimpleITK as sitk

image = sitk.Image([2]*3, sitk.sitkUInt8)
image.SetDirection([0,0,-1,0,1,0,1,0,0])
print(image) # look at the Direction matrix
sitk.WriteImage(image, 'image.mhd')

```

Content of header (.mhd) file:

```auto
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 0 0 1 0 1 0 -1 0 0
Offset = 0 0 0
CenterOfRotation = 0 0 0
AnatomicalOrientation = IAL
ElementSpacing = 1 1 1
DimSize = 2 2 2
ElementType = MET_UCHAR
ElementDataFile = image.raw

```

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [October 26, 2021, 8:36pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/4 "2021-10-26T20:36:38Z")

</div>

Hi @ingmar.voigt

Perhaps I am misreading it, but I believe that the code you cited is also column major ordering, i.e., the column variable (j) is in the inner loop. So, I think it is consistent, but you’re right that it isn’t documented. I’m adding it to the wiki page you cited.

Thanks!  
Stephen

---

<div class="post-metadata">

### Author: ![ingmar.voigt](https://discourse.itk.org/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@ingmar.voigt](https://discourse.itk.org/u/ingmar.voigt)
#### Post date: [October 27, 2021, 7:12am UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/5 "2021-10-27T07:12:58Z")

</div>

Great, thank you all for your responses and for extending the wiki page!

Meanwhile we had btw. also found another confirmation from earlier [https://itk.org/pipermail/community/2014-October/007437.html](https://itk.org/pipermail/community/2014-October/007437.html) and double checked with ITKSnap (via “Reorient image” dialog box) and 3DSlicer (loading associated mesh)

---

<div class="post-metadata">

### Author: ![ingmar.voigt](https://discourse.itk.org/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@ingmar.voigt](https://discourse.itk.org/u/ingmar.voigt)
#### Post date: [October 27, 2021, 3:13pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/6 "2021-10-27T15:13:58Z")

</div>

@Stephen_Aylward just wondering: doesn’t column variable j in inner loop mean that it’s in row major order? Or is `m_TransformMatrix` already column major too (i.e. following he OpenGL convention I believe)?

From the signature `TransformMatrix(int _i, int _j ..)` I assumed `_i` would be rows and `_j` would be columns

```auto
MetaObject::TransformMatrix(int _i, int _j, double _value)
{
  m_TransformMatrix[_i * m_NDims + _j] = _value;
}

```

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [October 27, 2021, 3:38pm UTC](https://discourse.itk.org/t/metaio-image-rotation-conventions/4525/7 "2021-10-27T15:38:11Z")

</div>

Ah - sorry for the confusion.

m\_TransformMatrix just stores the serialization prior to writing it - so, yes, it too is column major.

s
