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

<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 29, 2021, 8:56pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/41 "2021-11-29T20:56:11Z")

</div>

You could find some inspiration from [IterativeClosestPoint1.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/IterativeClosestPoint1.cxx) example.

---

<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 30, 2021, 3:48am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/42 "2021-11-30T03:48:28Z")

</div>

Thank you so much . I will definitely look into it.

---

<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 30, 2021, 7:59pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/43 "2021-11-30T19:59:41Z")

</div>

So I’m trying to convert my moving point list to the pointsets with ITK.F type and I’m getting this runtime error  
TypeError: in method ‘itkPointF3\_\_\_setitem\_\_’, argument 3 of type ‘float’

so basically I’m predefining defining a list p with these parameters  
PixelType = itk.F  
Dimension = 3

PointSetType = itk.PointSet[PixelType, Dimension]

and then looping this list over the length of my moving points and adding those points onto this predefined list .  
Is there a better and efficient way . Its just I don’t know C++ , so everything is python based .

Thanks a lot

---

<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 30, 2021, 8:13pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/44 "2021-11-30T20:13:13Z")

</div>

> [@VeerBal](#):
>
> looping this list over the length of my moving points and adding those points onto this predefined list

That sounds right.

> [@VeerBal](#):
>
> TypeError: in method ‘itkPointF3\_\_\_setitem\_\_’, argument 3 of type ‘float’

What code line triggers this?

---

<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 30, 2021, 8:21pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/45 "2021-11-30T20:21:21Z")

</div>

so I have something like this

```auto
p = [] # creates an empty list 
p = itk.Point[PixelType, Dimension]()
count = 0
for i in range(0,len(moving_points)):
    p[i] = moving_points[i] # this line triggers it 

```

moving points are a list of floating points with size of (70,3)

---

<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 30, 2021, 9:09pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/46 "2021-11-30T21:09:50Z")

</div>

This [example](https://itk.org/ITKExamples/src/Core/Common/CreateAPointSet/Documentation.html) has a python version. You need to set each x, y and z coordinate separately to an `itk.Point`. Then insert those into an `itk.PointSet`.

---

<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 1, 2021, 3:23am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/47 "2021-12-01T03:23:20Z")

</div>

Thank you so much . Looks like its working .

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

Is there a way to read the values inside it .When I open it its getting me an error of the variable not being packable .  
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:** [December 1, 2021, 3:26am UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/48 "2021-12-01T03:26:28Z")

</div>

So when I print those points and p list I get this

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

So can I directly use these p points as my moving image points for b-spline ffd .

Thanks

---

<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 1, 2021, 3:35pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/49 "2021-12-01T15:35:06Z")

</div>

I think you should be able to use the variable you call `PointSet` directly in the registration.

---

<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 1, 2021, 5:57pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/50 "2021-12-01T17:57:25Z")

</div>

So when I used that variable I’m getting this error message

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

Can you please provide some insights .  
Thanks

---

<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 1, 2021, 7:04pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/51 "2021-12-01T19:04:07Z")

</div>

You should be using [PointSetToPointSetMetric](https://itk.org/Doxygen/html/classitk_1_1PointSetToPointSetMetric.html), not `ImageToImageMetric` family. As you are not sharing your code, I can only guess what you are doing.

---

<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 1, 2021, 7:42pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/52 "2021-12-01T19:42:38Z")

</div>

Sorry my bad . This is the code I’m using

```auto
def bspline_intra_modal_registration(
    fixed_image,
    moving_image,
    fixed_image_mask=None,
    fixed_points=None,
    moving_points=None,
):

    registration_method = sitk.ImageRegistrationMethod()

    # Determine the number of BSpline control points using the physical spacing we want for the control grid.
    grid_physical_spacing = [50.0, 50.0, 50.0] # A control point every 50mm
    image_physical_size = [
        size * spacing
        for size, spacing in zip(fixed_image.GetSize(), fixed_image.GetSpacing())
    ]
    mesh_size = [
        int(image_size / grid_spacing + 0.5)
        for image_size, grid_spacing in zip(image_physical_size, grid_physical_spacing)
    ]

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

    registration_method.SetMetricAsMeanSquares()
    # Settings for metric sampling, usage of a mask is optional. When given a mask the sample points will be
    # generated inside that region. Also, this implicitly speeds things up as the mask is smaller than the
    # whole image.
    registration_method.SetMetricSamplingStrategy(registration_method.RANDOM)
    registration_method.SetMetricSamplingPercentage(0.01)
    if fixed_image_mask:
        registration_method.SetMetricFixedMask(fixed_image_mask)

    # Multi-resolution framework.
    registration_method.SetShrinkFactorsPerLevel(shrinkFactors=[4, 2, 1])
    registration_method.SetSmoothingSigmasPerLevel(smoothingSigmas=[2, 1, 0])
    registration_method.SmoothingSigmasAreSpecifiedInPhysicalUnitsOn()

    registration_method.SetInterpolator(sitk.sitkLinear)
    registration_method.SetOptimizerAsLBFGSB(
        gradientConvergenceTolerance=1e-5, numberOfIterations=100
    )

    # If corresponding points in the fixed and moving image are given then we display the similarity metric
    # and the TRE during the registration.
    if fixed_points and moving_points:
        registration_method.AddCommand(
            sitk.sitkStartEvent, rc.metric_and_reference_start_plot
        )
        registration_method.AddCommand(
            sitk.sitkEndEvent, rc.metric_and_reference_end_plot
        )
        registration_method.AddCommand(
            sitk.sitkIterationEvent,
            lambda: rc.metric_and_reference_plot_values(
                registration_method, fixed_points, moving_points
            ),
        )

    return registration_method.Execute(fixed_image, moving_image)

```

---

<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 1, 2021, 7:47pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/53 "2021-12-01T19:47:26Z")

</div>

Correspondingly, you should use [PointSetToPointSetRegistrationMethod](https://itk.org/Doxygen/html/classitk_1_1PointSetToPointSetRegistrationMethod.html), not `ImageRegistrationMethod`.

---

<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 1, 2021, 7:49pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/54 "2021-12-01T19:49:29Z")

</div>

Thank you so much . Really Appreciated . I will look into 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:** [December 1, 2021, 8:21pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/55 "2021-12-01T20:21:54Z")

</div>

Is there a Python version of this registration .  
Its just I thought I was doing B-spline FFD image registration which had fixed and moving image , fixed mask ( which I used airway mask ) and fixed and moving points (which are the coordinates of my moving and fixed airway masks at each bifurcations ) .

Sorry for all the trouble I’m giving  
Thanks

---

<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 1, 2021, 10:55pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/56 "2021-12-01T22:55:21Z")

</div>

Oh, it looks like it is [not exposed in Python](https://github.com/InsightSoftwareConsortium/ITK/tree/v5.3rc02/Modules/Registration/Common/wrapping) like most of the other registration methods and metrics. Good news: adding it is not hard, see e.g. [itkImageRegistrationMethod.wrap](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.3rc02/Modules/Registration/Common/wrapping/itkImageRegistrationMethod.wrap).

---

<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 1, 2021, 11:08pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/57 "2021-12-01T23:08:24Z")

</div>

I just made a PR which should add required Python support:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/2914>
>
> \## PR Checklist
> \- \[x\] No \[API changes\](https://github.com/InsightSoftwareConsor…tium/ITK/blob/master/CONTRIBUTING.md#breaking-changes) were made (or the changes have been approved)
> \- \[x\] No \[major design changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#design-changes) were made (or the changes have been approved)
> \- \[\] Added test (or behavior not changed)
> \- \[\] Updated API documentation (or API not changed)
> \- \[\] Added \[license\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/KWStyle/ITKHeader.h) to new files (if any)
> \- \[x\] Added Python wrapping to new files (if any) as described in \[ITK Software Guide\](https://itk.org/ItkSoftwareGuide.pdf) Section 9.5
> \- \[\] Added \[ITK examples\](https://github.com/InsightSoftwareConsortium/ITKSphinxExamples) for all new major features (if any)

---

<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 2, 2021, 4:21pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/58 "2021-12-02T16:21:39Z")

</div>

Thank you .  
Sorry , But I don’t see the PR on GitHub .

---

<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 2, 2021, 4:35pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/59 "2021-12-02T16:35:25Z")

</div>

I converted it into a draft PR, because I realized that my initial attempt does not work ☹

---

<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 2, 2021, 4:57pm UTC](https://discourse.itk.org/t/bspline-free-form-deformable-registration/4586/60 "2021-12-02T16:57:15Z")

</div>

Oh okay . So my question about the Bspine FFD image registration is  
What exactly are those fixed and moving points in this example are .  
def bspline\_intra\_modal\_registration(  
fixed\_image,  
moving\_image,  
fixed\_image\_mask=None,  
fixed\_points=None,  
moving\_points=None,  
):

```
registration_method = sitk.ImageRegistrationMethod()

# Determine the number of BSpline control points using the physical spacing we want for the control grid.
grid_physical_spacing = [50.0, 50.0, 50.0] # A control point every 50mm
image_physical_size = [
    size * spacing
    for size, spacing in zip(fixed_image.GetSize(), fixed_image.GetSpacing())
]
mesh_size = [
    int(image_size / grid_spacing + 0.5)
    for image_size, grid_spacing in zip(image_physical_size, grid_physical_spacing)
]

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

registration_method.SetMetricAsMeanSquares()
# Settings for metric sampling, usage of a mask is optional. When given a mask the sample points will be
# generated inside that region. Also, this implicitly speeds things up as the mask is smaller than the
# whole image.
registration_method.SetMetricSamplingStrategy(registration_method.RANDOM)
registration_method.SetMetricSamplingPercentage(0.01)
if fixed_image_mask:
    registration_method.SetMetricFixedMask(fixed_image_mask)

# Multi-resolution framework.
registration_method.SetShrinkFactorsPerLevel(shrinkFactors=[4, 2, 1])
registration_method.SetSmoothingSigmasPerLevel(smoothingSigmas=[2, 1, 0])
registration_method.SmoothingSigmasAreSpecifiedInPhysicalUnitsOn()

registration_method.SetInterpolator(sitk.sitkLinear)
registration_method.SetOptimizerAsLBFGSB(
    gradientConvergenceTolerance=1e-5, numberOfIterations=100
)

# If corresponding points in the fixed and moving image are given then we display the similarity metric
# and the TRE during the registration.
if fixed_points and moving_points:
    registration_method.AddCommand(
        sitk.sitkStartEvent, rc.metric_and_reference_start_plot
    )
    registration_method.AddCommand(
        sitk.sitkEndEvent, rc.metric_and_reference_end_plot
    )
    registration_method.AddCommand(
        sitk.sitkIterationEvent,
        lambda: rc.metric_and_reference_plot_values(
            registration_method, fixed_points, moving_points
        ),
    )

return registration_method.Execute(fixed_image, moving_image)

```

For me I thought I was using this approach where my fixed and moving points are fixed mask image and moving mask image bifurcation points . OR is there a way to implement this approach on my dataset .

Sorry I’m just new to registration and trying to wrap my head around it . Thanks for understanding

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

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