Skip to content

Commit

Permalink
Merge pull request #692 from ANTsX/fix_apply_transform_to_image
Browse files Browse the repository at this point in the history
Fix apply transform to image with displacement fields
  • Loading branch information
cookpa authored Aug 7, 2024
2 parents 4079ac1 + e7d2697 commit 1e0ca7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/antsTransform.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ AntsTransform<TransformType> antsTransformFromDisplacementField( AntsImage<Vecto
}

template <typename TransformType, typename VectorImageType, typename PrecisionType, unsigned int Dimension>
AntsImage<VectorImageType> antsTransformToDisplacementField( AntsTransform<TransformType> & xfrm,
AntsImage<VectorImageType> antsTransformToDisplacementField( AntsTransform<TransformType> & xfrm,
AntsImage<itk::Image<PrecisionType, Dimension>> & ref )
{
//typedef itk::Transform<PrecisionType,Dimension,Dimension> TransformType;
Expand Down Expand Up @@ -246,6 +246,16 @@ void local_antsTransform(nb::module_ &m) {
m.def("transformImage", &transformImage<itk::Transform<double,3, 3>, itk::Image<double, 3>>);
m.def("transformImage", &transformImage<itk::Transform<double,4, 4>, itk::Image<double, 4>>);

// displacement field transforms
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 2>, itk::Image<unsigned char, 2>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 3>, itk::Image<unsigned char, 3>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 2>, itk::Image<unsigned int, 2>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 3>, itk::Image<unsigned int, 3>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 2>, itk::Image<float, 2>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 3>, itk::Image<float, 3>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 2>, itk::Image<double, 2>>);
m.def("transformImage", &transformImage<itk::DisplacementFieldTransform<float, 3>, itk::Image<double, 3>>);

m.def("inverseTransform", &inverseTransform<itk::Transform<float, 2, 2>, itk::Transform<float, 2, 2>>);
m.def("inverseTransform", &inverseTransform<itk::Transform<float, 3, 3>, itk::Transform<float, 3, 3>>);
m.def("inverseTransform", &inverseTransform<itk::Transform<float, 4, 4>, itk::Transform<float, 4, 4>>);
Expand Down Expand Up @@ -286,7 +296,7 @@ void local_antsTransform(nb::module_ &m) {
m.def("antsTransformToDisplacementField", &antsTransformToDisplacementField<itk::DisplacementFieldTransform<float,2>, itk::VectorImage<float,2>,float,2>);
m.def("antsTransformToDisplacementField", &antsTransformToDisplacementField<itk::DisplacementFieldTransform<float,3>, itk::VectorImage<float,3>,float,3>);


nb::class_<AntsTransform<itk::DisplacementFieldTransform<float,2>>>(m, "AntsTransformDF2");
nb::class_<AntsTransform<itk::DisplacementFieldTransform<float,3>>>(m, "AntsTransformDF3");
nb::class_<AntsTransform<itk::Transform<float, 2, 2>>>(m, "AntsTransformF22");
Expand Down
9 changes: 9 additions & 0 deletions tests/test_core_ants_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ def test_apply_ants_transform_to_image(self):
tx.set_parameters((0.9,0,0,1.1,10,11))
img2 = ants.apply_ants_transform_to_image(tx, img, img)

def test_apply_ants_transform_to_image_displacement_field(self):
img = ants.image_read(ants.get_ants_data("r27")).clone('float')
img2 = ants.image_read(ants.get_ants_data("r16")).clone('float')
reg = ants.registration(fixed=img, moving=img2, type_of_transform="SyN")
tra = ants.transform_from_displacement_field(ants.image_read(reg['fwdtransforms'][0]))
deformed = tra.apply_to_image(img2, reference=img)
deformed_aat = ants.apply_transforms(img, img2, reg['fwdtransforms'][0], singleprecision=True)
nptest.assert_allclose(deformed.numpy(), deformed_aat.numpy(), atol=1e-6)

def test_invert_ants_transform(self):
img = ants.image_read(ants.get_ants_data("r16")).clone('float')
tx = ants.new_ants_transform(dimension=2)
Expand Down

0 comments on commit 1e0ca7e

Please sign in to comment.