# Single transform from composition of linear transform ?

**URL:** <https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464>\
**Category:** Beginner Questions\
**Created:** [November 29, 2017, 5:21pm UTC](https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464 "2017-11-29T17:21:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)\
**Post date:** [November 29, 2017, 5:21pm UTC](https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464/1 "2017-11-29T17:21:01Z")

</div>

Dear All,  
I have some linear transforms that are packed into a composite transform. Is there a way of calculating a single affine transform that would be the result of the composition ? I haven’t found any function to do this…  
Thanks for your help,  
Yann

---

<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:** [November 29, 2017, 6:59pm UTC](https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464/2 "2017-11-29T18:59:31Z")

</div>

With linear transforms, you could go through all of them using `composite->GetNthTransform`, get their 4x4 matrices, and multiply them together. If you started with an identity 4x4, you will end up with you composite transform in a single 4x4 matrix. Some typecasting to affine transform might be involved.

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [November 29, 2017, 11:02pm UTC](https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464/3 "2017-11-29T23:02:37Z")

</div>

There is no function to do this directly in ITK, but a while ago I wrote a tool to handle some simple operations with ITK transforms ([available here](https://github.com/fbudin69500/ITKTransformTools)). You can compile it and call `compose` if you have only 2 affine transforms to combine, or call `concatenate` for N transforms (this option also handles non-rigid transforms and generates a displacement field (or deformation field?). You can [read the code](https://github.com/fbudin69500/ITKTransformTools/blob/master/ITKTransformTools.cpp#L49-L64) to see the different options or run the program without any option.

---

<div class="post-metadata">

**Author:** ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)\
**Post date:** [December 1, 2017, 3:02pm UTC](https://discourse.itk.org/t/single-transform-from-composition-of-linear-transform/464/4 "2017-12-01T15:02:11Z")

</div>

Thanks both ! I explored a bit further, and found the a `compose()` function in `itk::MatrixOffsetTransformBase`.

So I wrote this simple code that I share in case other people need it:

```auto
#include "itkAffineTransform.h"
#include "itkCompositeTransform.h"

// ... //

//compoTform

typedef itk::AffineTransform< Type, Dimension > AffineType;

AffineType::Pointer tformResult = AffineType::New();
tformTest->SetIdentity();
size_t nTform = compoTform->GetNumberOfTransforms();
for (size_t i = nTform; i-- > 0 ; ) {
    tformResult->Compose( static_cast< const AffineType * >( compoTform->GetNthTransformConstPointer(i) ) );
}

```
