# Save transformation matrix programmatically after landmark registration

**URL:** <https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732>\
**Category:** Development\
**Tags:** registration\
**Created:** [March 18, 2020, 1:19pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732 "2020-03-18T13:19:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 18, 2020, 1:19pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/1 "2020-03-18T13:19:45Z")

</div>

Operating system: Win10  
Slicer version:4.10.2  
Expected behavior:  
Actual behavior:

Hi guys,  
I am new to slicer programming. I am loading two volume data( fixed and moving one). Then using slicer’s landmark registration module and selecting some landmark.  
After registration is done I want to save the transformation matrix programmatically. If possible getting the transformation matrix after every landmark is selected.  
can you please provide an example to do that?

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:** [March 18, 2020, 1:25pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/2 "2020-03-18T13:25:19Z")

</div>

You can save node to file using `slicer.util.saveNode()` method. Slicer saves transforms in ITK transform file format (binary .h5 file or text .txt file).

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 18, 2020, 1:56pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/3 "2020-03-18T13:56:24Z")

</div>

> [@lassoan](#):
>
> slicer.util.saveNode()

I used it like below lines:  
\>\>\> slicer.util.saveNode(transformNode,“C:\Users\Johar\Desktop\CheckIT\transmat.txt”)  
\> False  
And as you can see, I get nothing. Can you correct me and show, how to get the transform node correctly and then save?

---

<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:** [March 18, 2020, 2:09pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/4 "2020-03-18T14:09:30Z")

</div>

Use forward slashes not backslashes 😉

---

<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:** [March 18, 2020, 2:15pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/5 "2020-03-18T14:15:06Z")

</div>

> [@Tokai](#):
>
> slicer.util.saveNode(transformNode,“C:\Users\Johar\Desktop\CheckIT\transmat.txt”)

You can also use raw strings (add an `r` character before the string literal), to prevent interpreting backslash as an escape character:

`slicer.util.saveNode(transformNode,r"C:\Users\Johar\Desktop\CheckIT\transmat.txt")`

---

<div class="post-metadata">

**Author:** ![Tokai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tokai/32/4851_2.png) [@Tokai](https://discourse.slicer.org/u/Tokai)\
**Post date:** [March 18, 2020, 2:39pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/6 "2020-03-18T14:39:23Z")

</div>

Hi @lassoan and @pieper  
I tried both of your ideas. But I think I am not catching the transform node correctly.

```
>>> slicer.util.saveNode("Transform",r"C:/Users/transmat.txt")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Slicer 4.10.2\bin\Python\slicer\util.py", line 512, in saveNode
    properties["nodeID"] = node.GetID();
AttributeError: 'str' object has no attribute 'GetID'  

```

This is how transform matrix looks like after landmark registration:

 ![Unbenannt](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/9/39c5a678fe074db472402c22d915c92eb7bf66e1.png)

---

<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:** [March 18, 2020, 2:42pm UTC](https://discourse.slicer.org/t/save-transformation-matrix-programmatically-after-landmark-registration/10732/7 "2020-03-18T14:42:24Z")

</div>

> [@Tokai](#):
>
> slicer.util.saveNode(“Transform”,r"C:/Users/transmat.txt")

“Transform” is the node’s name. You can use `slicer.util.getNode()` method to get the node object from its name:

`slicer.util.saveNode(slicer.util.getNode("Transform"),r"C:/Users/transmat.txt")`

Please complete one of the [Slicer programming tutorials](https://www.slicer.org/wiki/Documentation/Nightly/Training#PerkLab.27s_Slicer_bootcamp_training_materials). All these basic concepts are described there.
