# Converting transform.h5 to cmtk .xform

**URL:** <https://discourse.slicer.org/t/converting-transform-h5-to-cmtk-xform/11747>\
**Category:** Support\
**Created:** [May 28, 2020, 2:00pm UTC](https://discourse.slicer.org/t/converting-transform-h5-to-cmtk-xform/11747 "2020-05-28T14:00:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hugotrentesaux/32/6404_2.png) [@HugoTrentesaux](https://discourse.slicer.org/u/HugoTrentesaux)\
**Post date:** [May 28, 2020, 2:00pm UTC](https://discourse.slicer.org/t/converting-transform-h5-to-cmtk-xform/11747/1 "2020-05-28T14:00:26Z")

</div>

Is there a builtin way for converting the transform.h5 file made by 3DSlicer to a xform file as produced by cmtk?

Here is an example of _the same transformation_ in both formats:

```auto
>> h5read('slicer_transform.h5', '/TransformGroup/0/TranformParameters')

ans =

    1.0430
   -0.0198
   -0.0326
    0.0144
    1.0304
   -0.1652
    0.0353
    0.1646
    1.0300
  -97.7032
 -194.5304
 -243.7022

```

```bash
$ cat cmtk_transform.xform/registration 
! TYPEDSTREAM 2.4

registration {
	reference_study "target.nrrd"
	floating_study "source.nrrd"
	affine_xform {
		xlate -101.8 -205.9 -143.7 
		rotate 9.031949273 -1.921484041 -0.7885958359 
		scale 1.04498569 1.044250032 1.040359656 
		shear -8.001045526e-05 -0.0002715663214 -0.001942363876 
		center 282.2 512.7 187.7 
	}
}

```

(note the ‘s’ of “transform” is really missing in the hdf5 dataset name)

Here, the cmtk transform was produced by mapping the source image on the target image produced by slicer resampling. The fit is perfect.

---

<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:** [May 28, 2020, 4:41pm UTC](https://discourse.slicer.org/t/converting-transform-h5-to-cmtk-xform/11747/2 "2020-05-28T16:41:14Z")

</div>

cmtk should be able to read a linear transform from a 4x4 matrix. I have never used cmtk, but found this method by a quick web search, which may work: [https://rdrr.io/cran/nat/man/affmat2cmtkparams.html](https://rdrr.io/cran/nat/man/affmat2cmtkparams.html).

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hugotrentesaux/32/6404_2.png) [@HugoTrentesaux](https://discourse.slicer.org/u/HugoTrentesaux)\
**Post date:** [May 29, 2020, 3:31pm UTC](https://discourse.slicer.org/t/converting-transform-h5-to-cmtk-xform/11747/3 "2020-05-29T15:31:07Z")

</div>

You’re right. This is a bash script using `cmtk mat2dof` for somebody who would like to do the same thing (thanks @vbormuth):

```bash
#!/bin/bash
# 1) Perform a landmark registration in 3D slicer.
# 2) Save the transformation as *.txt file.
# 3) Call slicer2cmtk as follows
# slicer2cmtk Slicer_Transformation.txt Slicer2cmtk.xform

if [$# -ne 2]
then
echo "Usage: slicer2cmtk Slicer_Transformation.txt Slicer2cmtk.xform
Transforms the slicer transformation matrix into a cmtk xform"
else
awk 'NR==4 {print $2 , $5 , -$8 , "0\n" , $3 , $6 , -$9 , "0\n" , -$4 , -$7 , $10, "0\n" , -$11 , -$12 , $13 , "1"}' < $1 | cmtk mat2dof -o ${2}/registration
fi

```

(note the minus sign on some values)
