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

FIX: compose multi type transforms #652

Closed ncullen93 closed 1 month ago

ncullen93 commented 1 month ago

This PR lets you compose transforms of multiple types. There are really only two types of transforms wrapped by antspy though: itk::Transform and itk::DisplacementFieldTransform.

This example should work:

import ants
import numpy as np
image = ants.image_read(ants.get_ants_data("r16"))

linear_transform = ants.create_ants_transform(transform_type="AffineTransform", precision='float', 
        dimension=image.dimension)

translation = (30,30)
translation_matrix = np.array([[1, 0, translation[0]], [0, 1, translation[1]]])
linear_transform.set_parameters(translation_matrix)

displacement_field = ants.simulate_displacement_field(image, field_type="bspline", 
        number_of_random_points=10000, sd_noise=20.0, enforce_stationary_boundary=True, 
        number_of_fitting_levels=4, mesh_size=2, sd_smoothing=4.0)

displacement_field_xfrm = ants.transform_from_displacement_field(displacement_field)

xfrm = ants.compose_ants_transforms([linear_transform, displacement_field_xfrm])
image2 = xfrm.apply_to_image(image)

image2.plot() # should see translation and deformation
cookpa commented 1 month ago

Nice. Is xfrm here a composite transform, that could be written to a .h5 file?

ncullen93 commented 1 month ago

Yes. Writing to h5 then reading back and applying works fine.

Printing:

ANTsTransform
         Type       : CompositeTransform
         Dimension  : 2
         Precision  : float
cookpa commented 1 month ago

Great! Can composite transforms themselves be composed? Eg, I have subjectToTemplate.h5 and then templateToAnotherztemplate.h5

ncullen93 commented 1 month ago

Yes that appears to work since they're stored as a general itk::Transform like all the other types besides the deformation field transform.

coveralls commented 1 month ago

Coverage Status

coverage: 81.315%. remained the same when pulling a930d72c23ca43888adf1b0cc27a42dbbd8d9ae2 on fix-compose-multi-transforms into 34536fcbccdaed7efc315a1167a6a92b4a060c3d on master.