Convert Transform parameter file to transform matrix

622 views
Skip to first unread message

Xiaoxiao Zhou

unread,
Nov 5, 2020, 11:03:19 AM11/5/20
to elastix-imageregistration

Hi,

I was trying to convert a euler 3D transform parameter file to a transform matrix to manually compute deformation field, by SimpleITK Euler3DTransform. I also used elastix command line to generate a deformation field. However, my manually computed results were always a little bit different from elastix generated.
I was wondering if you would know the possible reasons?
I thought for Euler transform, it only transforms the coordinates by the transform matrix, and no interpolation or other operations is needed, do I understand correctly?

My code and result:

image_origin = np.array([0, -239, 0])
theta_x = -0.094891
theta_y = 0.006870
theta_z = 0.031377
translation = np.array([-9.053901, -23.358420, -8.338770])
rotation_center = np.array([119.5, -119.5, 77])
rigid_euler = sitk.Euler3DTransform(rotation_center, theta_x, theta_y, theta_z, translation)
A1 = np.asarray(rigid_euler.GetMatrix()).reshape(3,3)
c1 = np.asarray(rigid_euler.GetCenter())
t1 = np.asarray(rigid_euler.GetTranslation())

p = np.array([100, 200, 200])
truec = rotation_center - image_origin
p_transform = np.dot(A1, p - truec) + t1 + truec
p_displacement = p_transform - p

result:
p_displacement = array([-11.0793279 , -12.68448335, -16.38891453])
p_displacement_byelastix = DVF[100, 200, 200] = array([-11.518286, -19.103964, -16.620592], dtype=float32)

their difference is: array([-0.43895785, -6.4194805 , -0.23167759])

The transform file:
(Transform "EulerTransform")
(NumberOfParameters 6)
(TransformParameters -0.094891 0.006870 0.031377 -9.053901 -23.358420 -8.338770)
(InitialTransformParametersFileName "NoInitialTransform")
(UseBinaryFormatForTransformationParameters "false")
(HowToCombineTransforms "Compose")

// Image specific
(FixedImageDimension 3)
(MovingImageDimension 3)
(FixedInternalImagePixelType "float")
(MovingInternalImagePixelType "float")
(Size 240 240 155)
(Index 0 0 0)
(Spacing 1.0000000000 1.0000000000 1.0000000000)
(Origin 0.0000000000 -239.0000000000 0.0000000000)
(Direction 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000)
(UseDirectionCosines "true")

// EulerTransform specific
(CenterOfRotationPoint 119.5000000000 -119.5000000000 77.0000000000)
(ComputeZYX "false")

// ResampleInterpolator specific
(ResampleInterpolator "FinalBSplineInterpolator")
(FinalBSplineInterpolationOrder 3)

// Resampler specific
(Resampler "DefaultResampler")
(DefaultPixelValue 0.000000)
(ResultImageFormat "nii.gz")
(ResultImagePixelType "short")
(CompressResultImage "false")

I will appreciate any of your ideas!
Thank you so much!

M.St...@lumc.nl

unread,
Nov 18, 2020, 9:21:58 AM11/18/20
to elastix-imag...@googlegroups.com

Hi Xiaoxiao,


On first glance your code looks right. The only thing I can still see if the ComputeZYX flag is set the same as in elastix, which specifies the order of the rotations. Other than that I think it is a case of debugging!


Best regards,

Marius Staring







From: elastix-imag...@googlegroups.com <elastix-imag...@googlegroups.com> on behalf of Xiaoxiao Zhou <xxzh...@gmail.com>
Sent: 05 November 2020 5:03 PM
To: elastix-imageregistration
Subject: [elastix-imageregistration] Convert Transform parameter file to transform matrix
 
--
You received this message because you are subscribed to the Google Groups "elastix-imageregistration" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elastix-imageregis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elastix-imageregistration/e7e5f166-62c9-4d64-970b-4a8952a9e16en%40googlegroups.com.

Xiaoxiao Zhou

unread,
Nov 18, 2020, 11:54:38 AM11/18/20
to elastix-imageregistration
Dear Marius, 

Thank you so much for your reply! I did some tests and found some other problems.  
According to Elastix manual document 4.9.0, Appendix C.1 Manual rigid registraiton, it said:
  • Knowing that the convention is Euler angles with YXZ, explain how the resulting matrix is computed from the parameters. 

However, when I changed it to YXZ by: 
        rigid_euler.SetRotation(thetay, thetax, thetaz)
rigid_euler = sitk.Euler3DTransform(truec, thetax, thetay, thetaz, trans)
I still cannot obtain exactly the same results as Elastix. 
I also manually tried all combinations of X, Y, Z, but always different.

Besides, I also tested on affine registration with exactly the same 12 parameters, and my manually computed DVF had larger differences from Elastix generated one. 
Then I converted moving image to registered moving image by 2 different DVFs, and visually in 3D slicer.
And I found that Elastix converted image by its own generated DVF looked much better than the image converted by my manually computed DVF. 

Therefore, I was wondering if it is possible that Elastix has some inherit correction or smoothing while generating the DVFs? 

If there is any other data/code needed, please feel free to let me know. 
Thank you so much!
Looking forward to hearing from you.

Best regards,
Xiaoxiao

M.St...@lumc.nl

unread,
Nov 18, 2020, 3:35:34 PM11/18/20
to elastix-imag...@googlegroups.com
Dear Xiaoxiao,

Here you can find exactly what elastix does:


HTH, Marius
Official elastix repository. Contribute to SuperElastix/elastix development by creating an account on GitHub.





Sent: 18 November 2020 5:54 PM
To: elastix-imageregistration
Subject: Re: [elastix-imageregistration] Convert Transform parameter file to transform matrix
 
Message has been deleted

Xiaoxiao Zhou

unread,
Nov 19, 2020, 12:28:49 PM11/19/20
to elastix-imageregistration
Dear Marius,

Thanks for this notes! 
I just tried exactly the same code in python (how it compute rotation x, rotation y, and rotation z), and tried all 6 combinations(ZXY, ZYX... ), unfortunately I still cannot obtain exactly the same results as Elastix, differences are around 0~2 pixels.
I think the main job we are both doing is to convert 3 radius in the 6 parameters to a rotation matrix, and traverse every point and compute with the function. 

The differences between Elastix and my manual computation in affine transformation is actually bigger, and transformed moving image with Elastix generated DVF looks much better than the one converted with my manual computed DVF. 
I am sorry for this problem again!!
I will appreciate any hints!

Thank you so much!

Best regards,
Xiaoxiao
Reply all
Reply to author
Forward

M.St...@lumc.nl

unread,
Nov 25, 2020, 4:30:12 AM11/25/20
to elastix-imag...@googlegroups.com

Dear Xiaoxiao,


I have no further hints for you except to do a step by step debugging of your python code.


Good luck bug hunting!


Best, Marius







Sent: 19 November 2020 6:28 PM

To: elastix-imageregistration
Subject: Re: [elastix-imageregistration] Convert Transform parameter file to transform matrix
 
Dear Marius,

Thanks for this notes! 
I just tried exactly the same code in python (how it compute rotation x, rotation y, and rotation z), and tried all 6 combinations(ZXY, ZYX... ), unfortunately I still cannot obtain exactly the same results as Elastix, differences are around 0~2 pixels.
I think the main job we are both doing is to convert 3 radius in the 6 parameters to a rotation matrix, and traverse every point and compute with the function. 

The differences between Elastix and my manual computation in affine transformation is actually bigger, and transformed moving image with Elastix generated DVF looks much better than the one converted with my manual computed DVF. 
I am sorry for this problem again!!
I will appreciate any hints!

Thank you so much!

Best regards,
Xiaoxiao
Reply all
Reply to author
Forward
On Wednesday, November 18, 2020 at 2:35:34 PM UTC-6 M.St...@lumc.nl wrote:
Reply all
Reply to author
Forward
0 new messages