# Read Transform - Apply to VersorRigid3DTransform

**URL:** <https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737>\
**Category:** Uncategorized\
**Tags:** simpleitk, python\
**Created:** [December 30, 2020, 3:40am UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737 "2020-12-30T03:40:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gattia](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/258eb7/32.png) [@gattia](https://discourse.itk.org/u/gattia)\
**Post date:** [December 30, 2020, 3:40am UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/1 "2020-12-30T03:40:59Z")

</div>

I’ve been trying to read a transform from disk (VersorRigid3DTransform saved previously) and then feed it into the VersorRigid3DTransform class so that I can get access to the original parameters like: Matrix, Translation, etc.

e.g.,

```auto
transform = sitk.ReadTransform('path/to/transform.tfm')
versor = sitk.VersorRigid3DTransform(transform)

```

However, when I run this I get a runtime / sitk error:

```auto
RuntimeError: Exception thrown in SimpleITK new_VersorRigid3DTransform: ../../Code/Common/src/sitkVersorRigid3DTransform.cxx:177:
sitk::ERROR: Transform is not of type Transform!

```

For some reason, sitk is not identifying the transform as a transform.

I can print(transform) and it appears as a itk::simple::Transform as expected.:

```auto
itk::simple::Transform
 CompositeTransform (0x55ba8a2f48f0)
   RTTI typeinfo: itk::CompositeTransform<double, 3u>
   Reference Count: 1
   Modified Time: 993
   Debug: Off
   Object Name: 
   Observers: 
     none
   Transforms in queue, from begin to end:
   >>>>>>>>>
   VersorRigid3DTransform (0x55ba8a3388d0)
     RTTI typeinfo: itk::VersorRigid3DTransform<double>
     Reference Count: 1
     Modified Time: 989
     Debug: Off
     Object Name: 
     Observers: 
       none
     Matrix: 
       0.999942 -0.0101886 0.0035472 
       0.0101895 0.999948 -0.000234057 
       -0.00354463 0.000270188 0.999994 
     Offset: [1.11228, 0.0329762, 0.545213]
     Center: [-66.7903, -3.167, 1.79558]
     Translation: [1.15481, -0.647841, 0.781093]
     Inverse: 
       0.999942 0.0101895 -0.00354463 
       -0.0101886 0.999948 0.000270188 
       0.0035472 -0.000234057 0.999994 
     Singular: 0
     Versor: [0.000126063, 0.00177298, 0.00509461, 0.999985]
   End of MultiTransform.
<<<<<<<<<<
   TransformsToOptimizeFlags, begin() to end(): 
      1 
   TransformsToOptimize in queue, from begin to end:
   End of TransformsToOptimizeQueue.
<<<<<<<<<<
   End of CompositeTransform.
<<<<<<<<<<

```

This syntax also matches a stackoverflow response by @blowekamp:

> <https://stackoverflow.com/questions/52391061/get-matrix-and-translation-from-a-simpleitk-transform>

Any ideas on what might be causing the issue?

---

<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:** [December 30, 2020, 2:30pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/2 "2020-12-30T14:30:22Z")

</div>

Hello @gattia,

You wrote the transform as a `CompositeTransform` inside of which you have a `VersorRigid3DTransform`, so that is why it is failing.

Example code illustrating that saving and reading a `VersorRigid3DTransform` works:

```auto
import SimpleITK as sitk

tx = sitk.VersorRigid3DTransform()
tx.SetTranslation([8,9,10])
tx.SetRotation([0,0,1],3.14)

sitk.WriteTransform(tx, 'my_transform.tfm')
transform = sitk.ReadTransform('my_transform.tfm')
versor = sitk.VersorRigid3DTransform(transform)
print(versor)

```

---

<div class="post-metadata">

**Author:** ![gattia](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/258eb7/32.png) [@gattia](https://discourse.itk.org/u/gattia)\
**Post date:** [December 30, 2020, 2:35pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/3 "2020-12-30T14:35:58Z")

</div>

Thanks, @zivy!

Is there a way to get the original transform out using sitk? Or does it have to be re-saved as a pure VersorRigid3DTransform() in the first place?

---

<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:** [December 30, 2020, 3:22pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/4 "2020-12-30T15:22:27Z")

</div>

Hello @gattia,

Yes, you can read it from the file, just requires a cast to CompositeTransform and getting the last transform from its stack:

```auto
transform = sitk.ReadTransform('my_transform.tfm')
# Cast the Transform to CompositeTransform and get the back transform
# which is a versor 
versor = sitk.CompositeTransform(transform).GetBackTransform()

```

---

<div class="post-metadata">

**Author:** ![gattia](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/258eb7/32.png) [@gattia](https://discourse.itk.org/u/gattia)\
**Post date:** [December 30, 2020, 3:41pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/5 "2020-12-30T15:41:07Z")

</div>

Thanks! And Im guessing this requires sitk \>2.0?. Im currently using an older version and tried something like this, but it doesn’t work since sitk 1.x doesn’t have CompositeTransform (as far as I can tell).

I can “probably” upgrade without other downstream problems. But just want to make sure before I open up that can of worms.

---

<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:** [December 30, 2020, 3:55pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/6 "2020-12-30T15:55:39Z")

</div>

Yes, you are correct, this is with the 2.x SimpleITK interface. The old interface didn’t provide access to internal transformations in the stack of a composite transformation and had other limitations.

I highly recommend moving to the new 2.x releases if you can. Many improvements and bug fixes (see [release notes](https://github.com/SimpleITK/SimpleITK/releases/tag/v2.0.0) for 2.0). We tried to keep the interface backwards compatible, as much as we could. There is a [migration guide](https://simpleitk.readthedocs.io/en/master/migrationGuide2.0.html) online, so take a look at that before you make your decision.

---

<div class="post-metadata">

**Author:** ![gattia](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/258eb7/32.png) [@gattia](https://discourse.itk.org/u/gattia)\
**Post date:** [December 30, 2020, 4:36pm UTC](https://discourse.itk.org/t/read-transform-apply-to-versorrigid3dtransform/3737/7 "2020-12-30T16:36:06Z")

</div>

Thanks for confirming. This might be the final push that makes me switch. Its been on the list of to dos, but wasnt worth the work before. Might need to now :).

Thanks again for helping guide me through this.

Anthony.
