ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
632 stars 161 forks source link

Transformation Matrix discrepancy #478

Closed roohy466 closed 1 year ago

roohy466 commented 1 year ago

Hi,

I have create an image manually rotated 15 degree from an original image! Then I tried to extract affine rigid transformation using antspy output:

mytx = ants.registration(fixed=fi, moving=mi, type_of_transform = 'DenseRigid' )

tx = ants.read_transform(mytx['fwdtransforms'][0]) print(tx.parameters)

the 12 parameters are as follow! [ 1.00000000e+00 -4.19393618e-05 -6.32358933e-07 4.03456470e-05 9.65903223e-01 -2.58903295e-01 1.14690365e-05 2.58903295e-01 9.65903223e-01 -1.80268765e-03 6.23846436e+00 5.96463013e+00]

seems there is an unexpected displacement in y , z direction using below code!

from scipy.spatial.transform import Rotation as R Rm= [tx.parameters[:3].tolist(),tx.parameters[3:6].tolist(),tx.parameters[6:9].tolist()] r1 = R.from_matrix(Rm).inv() print(np.round(Rm, decimals=2)) print('Rotations:') print(np.round(r1.as_euler('zxy', degrees=True),decimals=1)) print('\n') print('Translations:') print(np.round(tx.parameters[9:12],decimals=1))

Rotations: [ -0. -15. 0.]

Translations: [-0. 6.2 6. ]

however when I apply the transformation to the moving image (rotated one) the output is perfect! mywarpedimage = ants.apply_transforms( fixed=fi, moving=mi, transformlist=mytx['fwdtransforms'] )

How is it possible that the transformation matrix is wrong but the output image is perfect? Could you please explain why it is happening?

best Rouhi

ntustison commented 1 year ago

The ants.registration call initializes with a translation to align the images before optimizing the DenseRigid transform.

roohy466 commented 1 year ago

If there is no translation in the moving image, why should it generate any translation? that is confusing!

ntustison commented 1 year ago

How are you determining that there is "no translation in the moving image"? From what you have posted, there is definitely a translation component which you might not be able to notice visually.

roohy466 commented 1 year ago

Because I have generated the image by rotating 15 degree only!

cookpa commented 1 year ago

Could it be the center of the applied rotation differs from how ITK encodes rotations?

ntustison commented 1 year ago

Then you're misunderstanding what is happening. The registration first calculates the center of mass between the two images, calculates the translation to align those centers of mass, and then optimizes the specified transform.

roohy466 commented 1 year ago

Ok! then it explains what I am seeing! Thank you very much!

roohy466 commented 1 year ago

Could it be the center of the applied rotation differs from how ITK encodes rotations?

I could regenerate the translation values using ants. ants.get_center_of_mass command! np.round(np.array(ants.get_center_of_mass(fi)) - np.array(ants.get_center_of_mass(mi)), decimals=1) The problem is solved, thank you very much!