ANTsX/ANTsPy

Big boss, help me out!

Closed this issue · 9 comments

when I'm given a [2,3] affine matrix (which I've already changed like this, so I don't need to set fixed_parameters)

my code to get affine matrix refer to https://github.com/ANTsX/ANTs/wiki/ITK-affine-transform-conversion
image

How I use the affine matix:
tx = ants.new_ants_transform(dimension=2, transform_type="AffineTransform")
tx.set_parameters(mat_par)
without_fix_warped_img_Temp=tx.apply_to_image(m_img,reference=f_img)
m_img,f_img are all 2d and their
components=1,
direction=array([[1., 0.],[0., 1.]])
has_components = false
origin=(0.0, 0.0)
orientation=None
physical_shape=(350.0, 740.0)
pixeltype ='unsigned char'
spacing=(1.0,1.0)
is_rgb = false
At first, I thought I got the correct one by "ea_antsmat2mat", but although I got the correct image by using tx.apply_to_image, when I used this affine matrix to calculate the displacement field, and further calculated the deformation field, and the deform the img, I found that the graph did not conform to the result I wanted.
moving and fixed img
image
use fixed_parameters and AffineTransform_float_2_2 get the true warp image
image
use new affine matixs after "ea_antsmat2mat", it's also right
image
but when i use the new affine matixs to get displacement field and further calculated the deformation field, and the deform the img.I got the wrong one .
image
Do you know why?
Could you please tell me when I do so, "apply_to_image" call which function in which file of Ants?
I want to know how, when given a transformation matrix, the new coordinates are created behind it. Because my purpose is to calculate the displacement field through affine matrix, and then calculate the deformation field, so as to obtain the deformation image by using SpatialTransformer. Of course if you have a better way for me to get the displacement field, I would be very grateful.

I don't 100% follow but if you want to see how ANTsPy interprets an affine as a displacement field, you can do

ants.apply_transforms(
    fixed=fixed, 
    moving=moving,
    transformlist='movingToFixed0GenericAffine.mat',
    whichtoinvert=[False],  # Ensure the transform is not inverted
    compose='output_displacement_',  # Save the displacement field directly
    interpolator='nearestNeighbor'
)

I don't 100% follow but if you want to see how ANTsPy interprets an affine as a displacement field, you can do

ants.apply_transforms(
    fixed=fixed, 
    moving=moving,
    transformlist='movingToFixed0GenericAffine.mat',
    whichtoinvert=[False],  # Ensure the transform is not inverted
    compose='output_displacement_',  # Save the displacement field directly
    interpolator='nearestNeighbor'
)

hi @cookpa
Thank you very much for your answer, but maybe I wasn't clear.My goal is to know, when I given movingToFixed0GenericAffine mat, "apply_to_image" is how to calculate the deformation image, the coordinates of corresponding and sampling (how to calculate the coordinates is the most important to me, Because What I most want to know is the displacement of each pixel of the deformed image relative to the X-axis and Y-axis coordinates of the moved image)

hi@cookpa Some of my supplementary notes, so that I can explain my questions
MovingToFixed0GenericAffine.mat contains "AffineTransform_float_2_2" and "fixed" two object, including AffineTransform_float_2_2 array size is 6 * 1, fixed is a 2 * 1 array. With "ants.apply_to_image". I can transform the image correctly like that:
mat_data = sio.loadmat(mmtf['fwdtransforms'][0])
affine_params = mat_data["AffineTransform_float_2_2"] # mat_data["AffineTransform_float_2_2"] size 6 * 1
fixed_point = np.array([mat_data['fixed'][0][0],mat_data['fixed'][1][0]]) # mat_data['fixed'] is [ [..],[..]]
tx = ants.new_ants_transform(dimension=2, transform_type="AffineTransform")
tx.set_parameters(affine_params)
tx.set_fixed_parameters(fixed_point)
ture_warped_img_Temp=tx.apply_to_image(m_img,reference=f_img)
true_warped_img_Temp_np = ture_warped_img_Temp.numpy(single_components=False)
the true warped img is
image

But this requires "AffineTransform_float_2_2" and "fixed" two objects. And what I want to do is I just need an affine matrix represent the affine transformation relationship between the moving image and the target image, allowing me to calculate the amount of coordinate displacement from the moving image to the deformed image. So I recalculated a new affine transform like https://sourceforge.net/p/advants/discussion/840261/thread/9fbbaab7/?limit=25#1783. I got a new affine martix
and i can achieve the same transformation by ants.apply_to_image just like
tx = ants.new_ants_transform(dimension=2, transform_type="AffineTransform")
tx.set_parameters(mat_par)#mat_par is the new affine matrix
warped_img_Temp=tx.apply_to_image(m_img,reference=f_img)
i can get the right warped_img_Temp like
image

I thought the real affine transformation matrix would be this, However, the image obtained by using the affine transform to calculate the displacement field , SpatialTransformer sampling is different from true warp img
image
how I calculate the displacement field , SpatialTransformer sampling are:
image
image
Would you mind telling me why?

Does anyone know why, or does anyone know how I can get the affine matix so that I can use cv2.warpaffine for normal transformation

@cookpa @stnava @ntustison @tfmoraes Do you have a minute to help me out? I would appreciate it very much!

I am closing this issue. please start a new issue with reproducible examples:

https://stackoverflow.com/questions/59437658/how-do-i-make-a-minimal-and-reproducible-example-for-neural-networks

I am closing this issue. please start a new issue with reproducible examples:

https://stackoverflow.com/questions/59437658/how-do-i-make-a-minimal-and-reproducible-example-for-neural-networks

hi @stnava Actually, I think I've described my problem pretty well. Could you please help to look at it? It may take some time. Thank you very much for your reply!

I don't 100% follow but if you want to see how ANTsPy interprets an affine as a displacement field, you can do

ants.apply_transforms(
    fixed=fixed, 
    moving=moving,
    transformlist='movingToFixed0GenericAffine.mat',
    whichtoinvert=[False],  # Ensure the transform is not inverted
    compose='output_displacement_',  # Save the displacement field directly
    interpolator='nearestNeighbor'
)

@cookpa
You're right. I made a stupid mistake in writing the code. Thank you so much for all your work

The affine transform parameterization issue is something of an FAQ, so I made a wiki page

https://github.com/ANTsX/ANTsPy/wiki/ANTs-transform-concepts-and-file-formats