# Transformation matrix

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?

1 Like

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.

1 Like

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.

``````        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.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")
``````

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.