# Bspline Free form deformable registration

**URL:** https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586
**Category:** Algorithms
**Created:** [November 16, 2021, 5:54pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586 "2021-11-16T17:54:39Z")
**Posts on this page:** 20
**Page:** 5

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [December 18, 2021, 7:38pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/83 "2021-12-18T19:38:41Z")

</div>

So after iterating my bspline FFD my output looks like this where I’m getting a rounding error .  
14408.027  
Optimizer’s stopping condition, A rounding error occurred or line-search steps have an insufficient reduction  
Iteration: 50  
Metric value: 14402.493636195337

My optimizer conditions are as follows  
registration\_method.SetOptimizerAsLBFGS2(solutionAccuracy=1e-4, numberOfIterations=100, deltaConvergenceTolerance=0.01)

Can you provide some insights of what might be causing this .

This code is equivalent to the POPI model . I tried adjusting the solution accuracy but I’m still getting this optimizer stop condition .

---

<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: [December 20, 2021, 1:39pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/84 "2021-12-20T13:39:04Z")

</div>

> [@VeerBal](#):
>
> A rounding error occurred or line-search steps have an insufficient reduction

This might very well mean convergence has been reached.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [December 22, 2021, 3:47pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/86 "2021-12-22T15:47:54Z")

</div>

So I was performing the Bspline FFD registration same as the POPI model . My fixed and moving images are the two CT lung ct scans at different timepoint . And I generated 100 random fixed and moving points from the Ct images for TRE . The optimizer that I used was registration\_method.SetOptimizerAsLBFGS2(solutionAccuracy=1e-2, numberOfIterations=100,deltaConvergenceTolerance=0.01) . And for metric sampling I did not used any mask as it was giving me an error of VirtualSampledPointSet must have 1 or more points.

These were the results I got which does not seem to be significant .

![image](https://discourse.itk.org/uploads/default/original/2X/1/155f8710d21b4a82bc156373c10633d770cd04cb.png)

![image](https://discourse.itk.org/uploads/default/original/2X/e/e22bc86fc21d692866296c80dcd83ef60e2f1f7c.png)

So my question is … Is there a way I could improve these results and is there a better way to use fixed and moving points from the images for TRE .

---

<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: [December 22, 2021, 5:12pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/87 "2021-12-22T17:12:05Z")

</div>

If you are tired of tuning the registration yourself, it is worth trying [ITKElastix](https://github.com/InsightSoftwareConsortium/ITKElastix). I don’t know whether it is available in SimpleITK, you would need to use ITK Python wrapping. [Examples](https://github.com/InsightSoftwareConsortium/ITKElastix/tree/master/examples) are available.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 6, 2022, 9:23pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/88 "2022-01-06T21:23:19Z")

</div>

Hi.  
I have a quick question about the composite transformation .

So I did a bslpline FFD transformation on the lung CT scans pre and post for the same subject and my fixed and moving points are the airway bifurcation points ( x, y ,z) , but my fixed mask is set to NONE

So for the TRE for Bspline I have  
Initial alignment errors for subjects 20 and 21 in millimeters, mean(std): 133.73(38.81), max: 259.89  
and  
Final alignment errors in millimeters, mean(std): 65.73(42.72), max: 166.35

But I also did an affine registration on the lung mask prior to this Bspline FFD and therefore did a composite transformation using  
Composite\_tx = sitk.CompositeTransform([Affine\_transform,tx])

and then I looked at the TRE which is larger than , when I only performed Bspline FFD

Final alignment errors for composite for subjects 20 and 21 in millimeters, mean(std): 73.66(44.36), max: 183.80

Can you provide some insights if I’m doing something wrong because from my understanding a composition of affine (rigid) and bspline (non rigid) is a better registration and hence the errors should be lower than when I did just the Bspline FFD .

Thank you

---

<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: [January 7, 2022, 3:30pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/89 "2022-01-07T15:30:45Z")

</div>

Steps should be:

1. do affine registration using lung masks
2. create a composite transform of affine from step 1 and default-initialized BSpline
3. do free-form registration using the above composite transform making sure you are [optimizing only the last transform](https://itk.org/Doxygen/html/classitk_1_1CompositeTransform.html#a732489fb29e67a1b6bdb91fe004a9cec) (BSpline)

If you are optimizing BSpline separately from affine, it is no wonder that their combination is worse than BSpline alone.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 9, 2022, 1:41pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/90 "2022-01-09T13:41:27Z")

</div>

Thank you so much. Is there a code that I could refer to . I’m working in python .

Thank you

---

<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: [January 9, 2022, 2:51pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/91 "2022-01-09T14:51:32Z")

</div>

I don’t think there is a Python example. It should not be hard to translate [DeformableRegistration15](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/DeformableRegistration15.cxx), or relevant parts of it, into Python.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 10, 2022, 5:10pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/92 "2022-01-10T17:10:53Z")

</div>

So if I correctly understood  
First do a composite of the affine transform that has been generated as tfm and default-initialized bspline

registration\_method = sitk.ImageRegistrationMethod()  
#code for grid physical\_spacing , initial\_physical size, mesh\_size

initialized\_bspline = sitk.BSplineTransformInitializer(image1 = fixed\_image,  
transformDomainMeshSize = mesh\_size, order=3)  
composite\_tx = sitk.CompositeTransform([Affine\_transform, initialized bspline])

and then use this as an initial b-spline  
R.SetInitialTransform(composite\_tx)  
and then for optimizing the last transform  
use  
R.SetOnlyMostRecentTransformToOptimizeOn();  
R.SetOptimizerAsLBFGS2(solutionAccuracy=1e-2, numberOfIterations=100, deltaConvergenceTolerance=0.01)

Thank you once again

---

<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: [January 10, 2022, 6:25pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/93 "2022-01-10T18:25:57Z")

</div>

Sounds about right.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 12, 2022, 7:18pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/94 "2022-01-12T19:18:22Z")

</div>

I am getting an attribute error using ‘SetOnlyMostRecentTransformToOptimizeOn’  
Error Message

composite.SetOnlyMostRecentTransformToOptimizeOn()

AttributeError: ‘CompositeTransform’ object has no attribute ‘SetOnlyMostRecentTransformToOptimizeOn’

Sample code

registration\_method = sitk.ImageRegistrationMethod()  
#code for grid physical\_spacing , initial\_physical size, mesh\_size

```
initial_transform = sitk.BSplineTransformInitializer(
    image1=fixed_image, transformDomainMeshSize=mesh_size, order=3
)

# initialize the composite transform by affine and initital B-spline transform
# initializer 
composite = sitk.CompositeTransform([Affine_transform,initial_transform])

# optimize the most recent transform ( B-spline) but still using the previous affine 
composite.SetOnlyMostRecentTransformToOptimizeOn()
# set the initial transform as the composite of affine and iniital transform
registration_method.SetInitialTransform(composite)

```

So it looks like this composite transformation doesn’t have the attribute to optimize the only last transform , is there another way to do the optimization for only the last transform.

Can you provide some insights on what might be causing this issue . Thank you

---

<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: [January 12, 2022, 7:19pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/95 "2022-01-12T19:19:26Z")

</div>

> [@VeerBal](#):
>
> composite.SetOnlyMostRecentTransformToOptimizeOn()

Maybe `composite.OnlyMostRecentTransformToOptimizeOn()` or `composite.SetOnlyMostRecentTransformToOptimize(True)`?

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 17, 2022, 5:30pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/96 "2022-01-17T17:30:15Z")

</div>

Using both of these approaches are yielding me same error  
of composite transform doesn’t have the attribute to optimize the last transform.  
AttributeError: ‘CompositeTransform’ object has no attribute ‘OnlyMostRecentTransformToOptimizeOn’

Is there another approach to optimizing the most recent transform .

Thank you 🙂

---

<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: [January 17, 2022, 6:11pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/97 "2022-01-17T18:11:30Z")

</div>

@blowekamp or @zivy might be able to help with syntax.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [January 17, 2022, 9:38pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/98 "2022-01-17T21:38:18Z")

</div>

Hello @VeerBal,

In SimpleITK “The only parameters of the transform at the back of the queue are exposed and optimizable for registration.” (see [CompositeTransform doxygen](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1CompositeTransform.html#details)), so nothing needs to be set and hence this method doesn’t exist.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 17, 2022, 10:51pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/99 "2022-01-17T22:51:59Z")

</div>

Thank you  
So this means I can carry out my deformable registration simply by initializing it with composite of my affine transformation and initial B-spline and then carrying out the free form deformation on it without using the function SetOnlyMostRecentTransformToOptimizeOn() .

---

<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: [January 18, 2022, 2:42pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/100 "2022-01-18T14:42:53Z")

</div>

Correct.

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 21, 2022, 10:02pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/101 "2022-01-21T22:02:08Z")

</div>

So I performed the deformable registration . But when I overlay the fixed and the registered image , registration works great for the axial view . But for the coronal and sagittal view , its not that great .  
 ![image](https://discourse.itk.org/uploads/default/original/2X/0/0be087880dfbb9c5d1806cb5c5a5f8cc83b98960.jpeg)  
 ![image](https://discourse.itk.org/uploads/default/original/2X/5/502692b8a62f5f3ab2db6ccd16f34019ee6022fd.jpeg)  
 ![image](https://discourse.itk.org/uploads/default/original/2X/2/2f6a7cd7e4f3bbb5110e7bfde494ebbe208cfbcb.png)

Can you provide some insights of why this might be happening .

Thank you once again .

---

<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: [January 21, 2022, 11:52pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/102 "2022-01-21T23:52:21Z")

</div>

Could you be mixing up laterality (left/right) in coronal and sagittal overlay?

---

<div class="post-metadata">

### Author: ![VeerBal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VeerBal](https://discourse.itk.org/u/VeerBal)
#### Post date: [January 22, 2022, 2:11am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/103 "2022-01-22T02:11:01Z")

</div>

I double checked and the overlay doesn’t look mixed up . Could there be any other issue going on.

[Previous page](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586.md?page=4)

[Next page](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586.md?page=6)
