# 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:** 1

<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:** [November 16, 2021, 5:54pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/1 "2021-11-16T17:54:39Z")

</div>

Hello .  
I’m trying to register two lung CT images from the same patient (baseline and post ) using the Bspline transformation algorithm . I have the dicom images as well as the lung and the airway masks . I have already segmented these scans . So my question is  
Should I be using the dicom images or mask files or saving the dicom images into nrrd file and then using both the nrrd file along with the mask files .

Thank you for your help.

---

<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:** [November 16, 2021, 7:07pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/2 "2021-11-16T19:07:26Z")

</div>

While using airways would provide a great deal of information to work with, I am unaware of a ready way to use them.

You could use lung masks to do rigid or affine registration, and use that to initialize BSpline which would operate on CT intensities. Take a look at [DeformableRegistration15 example](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/DeformableRegistration15.cxx).

---

<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:** [November 16, 2021, 7:14pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/3 "2021-11-16T19:14:56Z")

</div>

> [@dzenanz](#):
>
> ng masks to do rigid or affine registration, and use that to initialize BSpline which would operate on CT intensities. Take a look at [DeformableRegistration15 example](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/DeformableRegistration15.cxx).

Thank you so much . This is the pipeline that I would be using eventually . Can I incorporate this example in python . Is there an example that you could guide me to for python .

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:** [November 16, 2021, 7:20pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/4 "2021-11-16T19:20:41Z")

</div>

For Python registration code, take a look at [Perform2DTranslationRegistrationWithMeanSquares](https://itk.org/ITKExamples/src/Registration/Common/Perform2DTranslationRegistrationWithMeanSquares/Documentation.html). It has associated C++ code, so it should be helpful in translating `DeformableRegistration15.cxx` 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:** [November 16, 2021, 7:27pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/5 "2021-11-16T19:27:39Z")

</div>

Thank you so much for your help. Really appreciated .

---

<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:** [November 16, 2021, 7:48pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/6 "2021-11-16T19:48:35Z")

</div>

So is there a way I could use the affine registration example along with the bspline non rigid example as shown in simpleITK-notebooks/python for my registration process.  
So far I performed affine registration on the lung masks . This is what I have for that iteration  
 ![fixed nd moving](https://discourse.itk.org/uploads/default/original/2X/1/18e087e034d920a3dccf79f647b14d88b731fb30.png)  
 ![iter](https://discourse.itk.org/uploads/default/original/2X/2/26716ba536c7a96f9072ed477ec14ac2e8a231bd.png)

---

<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:** [November 16, 2021, 10:18pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/7 "2021-11-16T22:18:53Z")

</div>

You could save to file the transform you obtain from that step, and then load it for BSpline transform. This allows you to remove the code from the example which does the rigid transform.

---

<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:** [November 17, 2021, 12:15am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/8 "2021-11-17T00:15:52Z")

</div>

Okay perfect . Thank you . I will try that .  
I am trying to wrap my head around the output of my affine transformation. So the below figure for the affine transformation is blank . Can you provide some insights of why this might be happening .

Thank you so much again . ![fig](https://discourse.itk.org/uploads/default/original/2X/1/14039135e038921b03c7831db0c10bb1ab6ecf94.png)

 ![output](https://discourse.itk.org/uploads/default/original/2X/3/3e4a3afa8ca4bcc3082e311307b4b6a5c4428403.png)

---

<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:** [November 17, 2021, 2:26am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/9 "2021-11-17T02:26:12Z")

</div>

Note that you are using a mask that prevents the registration algorithm to use the very strong and clear outer boundary of the lungs. This may make the registration less accurate and less robust. I would recommend to dilate the lung mask by 5-10mm to include a bit of the surrounding tissues and use that for registration.

If you register images of the same patient, same breathing phase, and time between acquiring the images is not too long (weeks or months) then you may get even better results by tight cropping with a 3D box and not using masking at all.

---

<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:** [November 17, 2021, 2:30pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/10 "2021-11-17T14:30:41Z")

</div>

If your masks have a value of `1`, then you might need to adjust your visualization parameters. Or change your masks to use the value of `255`, which visualize without issues.

---

<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:** [November 17, 2021, 4:00pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/11 "2021-11-17T16:00:37Z")

</div>

> [@dzenanz](#):
>
> of `255`, which visualize witho

Okay thank you so much . I will try that . 🙂

---

<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:** [November 17, 2021, 4:41pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/12 "2021-11-17T16:41:12Z")

</div>

I used this function for setting the upper bounds for my fixed and moving images to 255 .  
#set all values that are 1 to 255  
sitk.Clamp(Moving\_image\_sitk,upperBound=255)  
sitk.Clamp(Fixed\_image\_sitk,upperBound=255)

I’m still getting the black screen for visualization . This is the final result for my affine transformation which seems to work .

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

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:** [November 17, 2021, 7:26pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/13 "2021-11-17T19:26:31Z")

</div>

What does `interact` do? Can you write `moving_resampled` to disk and visualize it using Slicer? How does the file look like?

---

<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:** [November 17, 2021, 9:08pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/14 "2021-11-17T21:08:21Z")

</div>

That is actually a good idea. Thanks .

---

<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:** [November 17, 2021, 9:40pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/15 "2021-11-17T21:40:36Z")

</div>

Looks like saving the moving\_resampled onto the drive and opening in slicer made it work .

 ![image](https://discourse.itk.org/uploads/default/original/2X/9/9879376a8ae63362ac4fb6055cfee218fe680a18.png)

Thank you .

I have another question regarding initializing the affine transformation center points . I want to initialize the center point of affine transformation at carina point in the fixed image . And the initial transform as difference between the carina points in moving and fixed image .

I have imparted the carina points as a list of vectors [x, y , z] for both fixed and moving image .  
And right now the code only uses this as initial transform  
initial\_transform = sitk.CenteredTransformInitializer(Fixed\_image\_sitk,  
Moving\_image\_sitk,  
sitk.AffineTransform(3),  
sitk.CenteredTransformInitializerFilter.MOMENTS)  
And  
final\_transform = sitk.AffineTransform(initial\_transform) as final transform .

Is there a way I could use something like  
Fixed\_img\_carina\_points = [x,y,z]

final\_transform = sitk.AffineTransform(initial\_transform.GetMatrix,initial\_transform.GetTranslation,Fixed\_img\_carina\_points) to compute the final transform .

I hope it makes sense .

Thank you once again  
Its just I’m new to registration and SimpleItk toolkit is really helping me

---

<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:** [November 18, 2021, 2:54pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/16 "2021-11-18T14:54:03Z")

</div>

There is [LandmarkBasedTransformInitializer](https://itk.org/Doxygen/html/classitk_1_1LandmarkBasedTransformInitializer.html), but it needs 4 points or more.

You could keep the current transform initializer, but then do this trick:

> <https://github.com/KitwareMedical/HASI/blob/dd61fcb989533bb477bcdf8e253f6f9c9adb7b05/examples/normalizedPoseMesh.cxx#L155-L158>

and then use this adjusted transform to initialize the affine.

---

<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:** [November 18, 2021, 4:22pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/17 "2021-11-18T16:22:36Z")

</div>

Thank you so much . I will try this approach .

---

<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:** [November 18, 2021, 5:18pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/18 "2021-11-18T17:18:11Z")

</div>

Now I have a final affine transformation saved as a .tfm file . I want to initialize this for my B spline FFD transformation . The code that I’m using is

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

I have my fixed and moving images as well as fixed image mask = lung airway mask file .

So my question is how do I initialize my already obtained Affine transformation for Bspline .  
I’m reading my affine transformation under the variable  
affine\_transformation = sitk.ReadTransform(os.path.join(output\_dir\_affine, “AffineTransforamtion\_20\_21.tfm”))

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:** [November 18, 2021, 8:18pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/19 "2021-11-18T20:18:38Z")

</div>

Make a composite transform of that affine transform and the BSpline you want to work with, see the code here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2.1/Examples/RegistrationITKv4/DeformableRegistration15.cxx#L456-L462>

then pass `compositeTransform` to the registration instead of BSpline.

---

<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:** [November 19, 2021, 2:04pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/20 "2021-11-19T14:04:23Z")

</div>

Thank you . I will do that 🙂

So I’m trying to do a Bspline registration where my moving and fixed images are .mha format and my fixed mask is .img ( mask for airway tree )

I’m getting an error

File “C:\Users\singhgau\Anaconda3\lib\site-packages\SimpleITK\SimpleITK.py”, line 10387, in Execute  
return \_SimpleITK.ImageRegistrationMethod\_Execute(self, fixed, moving)

RuntimeError: Exception thrown in SimpleITK ImageRegistrationMethod\_Execute: D:\a\1\sitk\Code\Registration\src\sitkImageRegistrationMethod.cxx:791:  
sitk::ERROR: Filter does not support fixed image type: 32-bit signed integer

Do I need to cast the image to something else or am I not using the moving and fixed images correctly .

Thank you

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