# Transformation matrix

**URL:** https://discourse.itk.org/t/transformation-matrix/6004
**Category:** Uncategorized
**Created:** [June 21, 2023, 7:57am UTC](https://discourse.itk.org/t/transformation-matrix/6004 "2023-06-21T07:57:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![feeling](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b5e925/32.png) [@feeling](https://discourse.itk.org/u/feeling)
#### Post date: [June 21, 2023, 7:57am UTC](https://discourse.itk.org/t/transformation-matrix/6004/1 "2023-06-21T07:57:57Z")

</div>

Hello everyone:  
I am using SimpleITK to align a set of images, let’s say A is a fixed image and B is a moving image, I have done various alignments including Translation, Euler, Affine and got a series of TransformParameters.txt, I want to get a total transformation matrix X, directly from AX-\>B ,X for the rest of my subsequent research, how can I get X please? I have interpreted the TransformParameters but I found that it seems that it is not simply a matter of multiplying the matrices of the three sets of transformations, does anyone know how this is to be calculated?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 21, 2023, 1:20pm UTC](https://discourse.itk.org/t/transformation-matrix/6004/2 "2023-06-21T13:20:07Z")

</div>

Hello,

Have you stored the series of transforms into a `CompositeTransform`? This should be the first step to compose the series into a single working ITK transform.

Next work be to reduce the series into a affine matrix and a translations. For this to be possible `IsLinear` must be true on the composite transform ( which implies its true for all transforms in the compost ). The computation the composite translation `composite_transform->TransformPoint([0,0,0])` can be used. And to get the affine matrix, it can be assembled from the series to transforming the identity basis vectors e.g. `composite_transform->TransformVector([1,0,0]) ... `

Hope this helps.

---

<div class="post-metadata">

### Author: ![feeling](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b5e925/32.png) [@feeling](https://discourse.itk.org/u/feeling)
#### Post date: [June 25, 2023, 1:11am UTC](https://discourse.itk.org/t/transformation-matrix/6004/3 "2023-06-25T01:11:17Z")

</div>

I’m sorry I didn’t understand what you meant, I’m not sure about how the composite transform should be stored, can you give further hints, here is part of my code.

```auto
        parameterMap = sitk.GetDefaultParameterMap('translation')

        parameterMap2 = sitk.GetDefaultParameterMap('rigid')

        parameterMap3 = sitk.GetDefaultParameterMap('affine')

        sitk.PrintParameterMap(parameterMap)

        elastixImageFilter = sitk.ElastixImageFilter()
        elastixImageFilter.SetFixedImage(fixed_image)
        elastixImageFilter.SetMovingImage(moving_image)

        elastixImageFilter.SetParameterMap(parameterMap)
        elastixImageFilter.AddParameterMap(parameterMap2)  
        elastixImageFilter.AddParameterMap(parameterMap3) 

        elastixImageFilter.SetParameter("WriteIterationInfo", ["true"])  
        elastixImageFilter.SetOutputDirectory('../log')  
        elastixImageFilter.PrintParameterMap()  
        elastixImageFilter.Execute()  

        transformixImageFilter = sitk.TransformixImageFilter()
        transformixImageFilter.SetMovingImage(moving_image)
        transformixImageFilter.SetTransformParameterMap(elastixImageFilter.GetTransformParameterMap())
        transformixImageFilter.ComputeDeformationFieldOn()
        transformixImageFilter.LogToConsoleOn()
        transformixImageFilter.SetOutputDirectory('../log2')
        transformixImageFilter.Execute()
        sitk.WriteImage(transformixImageFilter.GetDeformationField(), "../output/deformation.nii.gz")

```

---

<div class="post-metadata">

### Author: ![feeling](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b5e925/32.png) [@feeling](https://discourse.itk.org/u/feeling)
#### Post date: [June 25, 2023, 1:57am UTC](https://discourse.itk.org/t/transformation-matrix/6004/4 "2023-06-25T01:57:05Z")

</div>

I have compiled my detailed verification steps, not sure if they are accurate, hope you can correct them.  
-1、Align the file to get TransformParameters.  
-2、Read the matrix parameters in TransformParameters to build the transformation matrix.  
-3、Read the image position, image orientation, image resolution in the Dicom file, and get the spatial coordinates of each pixel by mapping.  
-4、Transform the spatial coordinates of each plane pixel (multiply the transform matrix) to get the transformed spatial coordinates.  
-5. Multiply the transformed spatial coordinates by the inverse matrix of the mapping matrix (the inverse matrix of the mapping matrix cannot be found here, so a pseudo-inverse matrix is used) to get the new plane coordinates.  
-6, assign the values under the original coordinates to the values of the new coordinates.  
-7. Since the size of each slice is 512\*512, the new plane coordinates are filtered to remove the pixels whose coordinates are not within [512,512].  
-8、Fuse the original slice map with the new slice map to display and view the alignment result.
