# Saving Linear Transformation Matrix

**URL:** <https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192>\
**Category:** Support\
**Created:** [October 6, 2017, 3:21pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192 "2017-10-06T15:21:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anna\_Morelli](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Anna\_Morelli](https://discourse.slicer.org/u/Anna_Morelli)\
**Post date:** [October 6, 2017, 3:21pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/1 "2017-10-06T15:21:41Z")

</div>

Operating system: Windows 10  
Slicer version: 4.6.2  
Expected behavior: Matrix in ijk coordinates  
Actual behavior: Matrix in unknown reference system

Hi guys,  
I’m a beginner in Slicer and I’m in trouble saving the transformation matrix. I obtained this matrix with the Fiducial registration module and is expressed w.r.t. RAS reference system, I think. When I save it in other formats, such as .h5 or .txt or .mat, the values are different. I think that automatically the reference frame w.r.t. it is expressed has changed; is it based of IJK or LPS frames?  
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:** [October 7, 2017, 4:53am UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/2 "2017-10-07T04:53:21Z")

</div>

I think this topic should answer your questions: [Displayed transform vs .tfm content](https://discourse.slicer.org/t/displayed-transform-vs-tfm-content/270)

---

<div class="post-metadata">

**Author:** ![Anna\_Morelli](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Anna\_Morelli](https://discourse.slicer.org/u/Anna_Morelli)\
**Post date:** [October 7, 2017, 2:52pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/3 "2017-10-07T14:52:33Z")

</div>

Thank you @lassoan for your answer. Yes, the question is the same, but I’m sorry I didn’t understand what I should do to obtain the transformation matrix in xyz coordinates.

---

<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:** [October 7, 2017, 4:24pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/4 "2017-10-07T16:24:33Z")

</div>

What anatomical axes your xyz axes correspond to: LPS or RAS?

---

<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:** [October 7, 2017, 4:25pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/5 "2017-10-07T16:25:22Z")

</div>

Do you process your images in Matlab?

---

<div class="post-metadata">

**Author:** ![Anna\_Morelli](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Anna\_Morelli](https://discourse.slicer.org/u/Anna_Morelli)\
**Post date:** [October 12, 2017, 1:05pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/6 "2017-10-12T13:05:21Z")

</div>

Maybe I was not clear in the question, I’m sorry but I was a little bit confused.  
I didn’t understand how to deal with the matrix in the txt file, so w.r.t. LPS axes (e.g.I have to do its inverse, to pre or post multiply it for a certain matrix ,…) to obtain the matrix that appears in Slicer. How is it possible?  
Then I suppose that applying this matrix to my moving dataset, e.g. I can apply it using Matlab, I can obtain the same results that I obtained applying the tranformation matrix in Slicer and saving the result.  
If I’m not clear, please let me know.

---

<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:** [October 12, 2017, 2:32pm UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/7 "2017-10-12T14:32:43Z")

</div>

Here is the conversion done by Slicer’s MatlabBridge extension:

> <https://github.com/PerkLab/SlicerMatlabBridge/blob/f7d4511ffe9424e827362942beccb29c7007b526/MatlabCommander/commandserver/cli_lineartransformread.m#L25-L30>

If you need the values that are displayed in Slicer, use the computation in the ‘slicer’ branch.

Note that Slicer’s MatlabBridge extension allows you to run Matlab functions directly from the Slicer GUI. So, if you are forced to use Matlab for certain processing, you can still do that, while keeping the GUI and rest of your workflow in Slicer/Python. See more information about the MatlabBridge extension here: [https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge)

---

<div class="post-metadata">

**Author:** ![park](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/park/32/5717_2.png) [@park](https://discourse.slicer.org/u/park)\
**Post date:** [January 12, 2022, 2:35am UTC](https://discourse.slicer.org/t/saving-linear-transformation-matrix/1192/8 "2022-01-12T02:35:53Z")

</div>

This is full python code to get the transform matix same in Slicer

The important thing in python  
We can not use

`transfrom_itk = np.reshape(transform_raw, (3,4))`  
or  
`transfrom_itk = np.reshape(transform_raw, (3,4), order='F')`

```auto
reader = pd.read_table("Your path\\LinearTransform_3.txt", header=None)
transform_raw = np.array(list(map(float, reader[0][3].split(':')[-1].split(' ')[1:])))

transform_itk = np.zeros((4,4))
transform_itk[:3,:3] = np.reshape(transform_raw [:9], (3, 3))
transform_itk[:3, 3] = transform_raw [9:12]
transform_itk[3,3] = 1

lps2ras = np.diag([-1, - 1, 1, 1])
ras2lps = np.diag([-1, - 1, 1, 1])
transform_slicer = lps2ras @ np.linalg.inv(transform_itk) @ ras2lps

```
