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
608 stars 161 forks source link

How can I transform two CTs with a known affine transformation matrix? #499

Closed angolin22 closed 1 week ago

angolin22 commented 1 year ago

hello, thanks for your excellent work! I have a problem: I have two 3D CT images, and I know the affine transformation matrix between them. e. g. [[ 9.92477011e-01 -8.45505280e-02 8.85471061e-02 5.05205949e+01] [ 8.73986445e-02 9.95757283e-01 -2.87907932e-02 -5.85686522e+00] [-8.57371490e-02 3.63130975e-02 9.95655814e-01 -4.96898369e+01] [-0.00000000e+00 -0.00000000e+00 -0.00000000e+00 1.00000000e+00]] How can I transform CT with antspy? Thanks

ntustison commented 1 year ago

help(ants.create_ants_transform)

angolin22 commented 12 months ago

Thanks for your reply! But the result is not right, my code is

matrix = affine_mat[0:3,0:3]
translation = affine_mat[0:3,3:].squeeze()
tx_affine = ants.create_ants_transform("AffineTransform", precision="float", dimension=3, matrix=matrix, translation=translation)
reg_moving = tx_affine.apply_to_image(moving, fixed, 'linear')

In addtion, I don't know these parameters offset , cernter in this function ants.create_ants_transform(), can you see what's wrong?

ntustison commented 12 months ago

How did you derive the original transform parameters?

angolin22 commented 12 months ago

I use the point cloud corresponding to CT to generate the transform parameters. I use the fast global registration in the open3d library. The result of the point cloud is correct, but the CT image is wrong. I guess the order of the transform parameters generated by ants.create_ants_transform is wrong, so I printed out the transformation parameters and it looks like this, this is not the same order as my original transform parameters.

[ 9.92477000e-01 -8.45505297e-02  8.85471031e-02  8.73986408e-02
  9.95757282e-01 -2.87907925e-02 -8.57371464e-02  3.63130979e-02
  9.95655835e-01  5.05205956e+01 -5.85686541e+00 -4.96898384e+01]

my original transform parameters image

SO May I ask what the parameters of the matrix generated by ants.create_ants_transform represent?

ntustison commented 12 months ago

My guess is that the open3d affine parameters aren't directly portable into the ANTs/ITK transformation framework. I would ask over on the ITK discourse forum if anybody has any experience with what you're wanting to do.

angolin22 commented 12 months ago

Ok, thanks! I think it's related to this link

cookpa commented 1 week ago

See also #525. It's necessary to set both the matrix and the center of rotation (set via the "fixed parameters")