# concatenating dense deformation fields

**URL:** https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238
**Category:** Uncategorized
**Created:** [September 10, 2019, 3:08am UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238 "2019-09-10T03:08:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [September 10, 2019, 3:08am UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/1 "2019-09-10T03:08:48Z")

</div>

I was wondering if it is possible to concatenate two dense deformation fields in ITK python or SimpleITK.  
Having

> B = T2 ( T1 ( A(x) ) )

In which T1, and T2 are both dense deformation fields (both with size ofImage A and with vector type voxels), is it possible to derive

> B = T ( A(x) )

which would be concatenation of T1 and T2.  
I know there is a **CompositeTransform** class but does **AddTransform** , add the deformation fields or it applies the first one and then the second one.

My reason for concatenation of dense fields is that in my registration framework I predict a dense deformation field at each iteration, but I don’t want to resample the images to avoid interpolation artifacts as a result of multiple resampling.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 10, 2019, 3:24pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/2 "2019-09-10T15:24:34Z")

</div>

That might be as simple as adding the deformation fields together using [`AddImageFilter`](https://itk.org/Doxygen/html/classitk_1_1AddImageFilter.html). If that doesn’t work, report. Also, someone else with more experience with deformation fields might offer more/better adivce.

---

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [September 10, 2019, 3:32pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/3 "2019-09-10T15:32:11Z")

</div>

Problem is adding two deformation fields is not the same as concatenating them. AddImageFilter doesn’t work

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 10, 2019, 3:35pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/4 "2019-09-10T15:35:22Z")

</div>

You could iterate through all of the points of the final deformation field, and calculate its transform vector via `CompositeTransform`'s `TransformPoint` method.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [September 10, 2019, 4:19pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/5 "2019-09-10T16:19:57Z")

</div>

> [@sedghi](#):
>
> I know there is a **CompositeTransform** class but does **AddTransform** , add the deformation fields or it applies the first one and then the second one.

The SimpleITK/ITK composite transform does not combine the transform. It applies one transformation then the other.

> [@dzenanz](#):
>
> You could iterate through all of the points of the final deformation field, and calculate its transform vector via `CompositeTransform` 's `TransformPoint` method.

I believe this can be accomplished with `ResampleImageFilter` with one transform’s displacement field image being the input image and the other being transform. Careful attention is needed to determine the proper order.

---

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [September 10, 2019, 5:05pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/6 "2019-09-10T17:05:11Z")

</div>

> [@blowekamp](#):
>
> I believe this can be accomplished with `ResampleImageFilter` with one transform’s displacement field image being the input image and the other being transform. Careful attention is needed to determine the proper order.

Thanks for your comment  
I suppose `ResampleImageFilter` can handle vector type input image and proper interpolation. right?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [September 10, 2019, 5:15pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/7 "2019-09-10T17:15:36Z")

</div>

> [@sedghi](#):
>
> Thanks for your comment  
> I suppose `ResampleImageFilter` can handle vector type input image and proper interpolation. right?

Yes it can!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [September 11, 2019, 4:29am UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/8 "2019-09-11T04:29:21Z")

</div>

> [@blowekamp](#):
>
> The SimpleITK/ITK composite transform does not combine the transform. It applies one transformation then the other.

Applying one transformation on the result of the other is the correct way of combining/concatenating two transforms.

ITK handles these composite transforms correctly as long as you only need to compute forward transform and you only try to compute the displacement in a region that is inside of all displacement fields in the transform. Inverse transform computation, especially near the region boundaries may not work reliably (and no inverse can be computed outside the displacement field regions).

Composite transforms are very well supported in 3D Slicer (ITK composite transforms can be read, written, visualized, applied to any kind of data types, resampled to a combined displacement field, etc.), so you can use 3D Slicer for testing, validation, and even batch processing from Python.

---

<div class="post-metadata">

### Author: ![sedghi](https://discourse.itk.org/user_avatar/discourse.itk.org/sedghi/32/317_2.png) [@sedghi](https://discourse.itk.org/u/sedghi)
#### Post date: [September 11, 2019, 1:07pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/10 "2019-09-11T13:07:57Z")

</div>

> [@lassoan](#):
>
> Applying one transformation on the result of the other is the correct way of combining/concatenating two transforms.

Thanks for your comment,  
Is there any correct way to avoid multiple resampling of the image as a result of applying multiple transformation? So, instead of `T2 ( T1( A(x) )` we go straight to `T( A(x) )` since in the former we are resampling twice (and even more, if we have more than 2 dense deformations) and in the latter we are resampling once (no matter how many deformations)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [September 11, 2019, 1:11pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/11 "2019-09-11T13:11:16Z")

</div>

Yes, ITK and Slicer do the resampling correctly - resampling the image only once, with the combined transform.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [September 11, 2019, 1:42pm UTC](https://discourse.itk.org/t/concatenating-dense-deformation-fields/2238/12 "2019-09-11T13:42:28Z")

</div>

Here is a SimpleITK example which creates a composite transform of an affine transformed followed by a displacement field: [https://simpleitk.readthedocs.io/en/master/Examples/ImageRegistrationMethodDisplacement1/Documentation.html](https://simpleitk.readthedocs.io/en/master/Examples/ImageRegistrationMethodDisplacement1/Documentation.html)

At the end resampling is done once, with the combination of the transform. So the pretense of needing to manually combine transforms into one to avoid multiple resampling is not correct.

But if you need to combine two displacement field transforms into one: T\_2( T\_1(\vec{p}))=T(\vec{p}), then resampling T\_2's displacement field image with the T\_1 transform will yield the same results on the defined region as using a composite transform. The same linear interpolation and sampling of T\_2 with will occur when the fixed image’s domain ( the set of \vec{p} ) , and the displacement field images of T\_1 and T\_2 are the same. This resampling can be done to update T\_2, so as the T\_1 displacement field approaches zero, T\_2 will change less and less.
