# \[Solved\] BSplineTransformation from displacement vectors

**URL:** https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372
**Category:** Beginner Questions
**Tags:** python, bspline, simpleitk
**Created:** [September 28, 2022, 1:10pm UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372 "2022-09-28T13:10:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Flemingjp](https://discourse.itk.org/user_avatar/discourse.itk.org/flemingjp/32/2954_2.png) [@Flemingjp](https://discourse.itk.org/u/Flemingjp)
#### Post date: [September 28, 2022, 1:10pm UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/1 "2022-09-28T13:10:36Z")

</div>

I have pairs of points that describes the displacement between one image to another image. How can I use these points to construct a BSplineTransform?

An example of these points

```auto
[0 0] -> [18.53 57.52]
[76 0] -> [90.70 58.47]
[152 0] -> [162.26 60.35]
[228 0] -> [234.22 60.67]
[304 0] -> [305.71 61.47]

```

---

<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: [September 28, 2022, 2:27pm UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/2 "2022-09-28T14:27:58Z")

</div>

Hello @Flemingjp,

The relevant class is [LandmarkBasedTransformInitializerFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1LandmarkBasedTransformInitializerFilter.html). [This discussion](https://discourse.itk.org/t/using-landmarkbasedtransforminitializer-for-bspline-registrations/4096/13) is possibly also of interest.

---

<div class="post-metadata">

### Author: ![Flemingjp](https://discourse.itk.org/user_avatar/discourse.itk.org/flemingjp/32/2954_2.png) [@Flemingjp](https://discourse.itk.org/u/Flemingjp)
#### Post date: [September 28, 2022, 3:07pm UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/3 "2022-09-28T15:07:29Z")

</div>

@zivy

Thank you. What isn’t clear here is how to create the landmarks from my currents points

```auto
fixed_points = [[0, 0], [0, 1]]

landmark_tx = sitk.LandmarkBasedTransformInitializerFilter()
landmark_tx.SetFixedLandmarks(fixed_points)

```

This results in the error

```auto
TypeError: in method 'LandmarkBasedTransformInitializerFilter_SetFixedLandmarks', argument 2 of type 'std::vector< double,std::allocator< double > > const &'

```

---

<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: [September 28, 2022, 3:26pm UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/4 "2022-09-28T15:26:25Z")

</div>

Hello @Flemingjp,

The LandmarkBasedTransformInitializer expects the point coordinates in a flat list [x1, y1, x2, y2,…]:

```auto
fixed_points_flat = [c for p in fixed_points for c in p]

```

---

<div class="post-metadata">

### Author: ![Flemingjp](https://discourse.itk.org/user_avatar/discourse.itk.org/flemingjp/32/2954_2.png) [@Flemingjp](https://discourse.itk.org/u/Flemingjp)
#### Post date: [September 29, 2022, 7:34am UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/5 "2022-09-29T07:34:43Z")

</div>

@zivy  
Thank you for your help. This is my current issue

```auto
landmark_tx = sitk.LandmarkBasedTransformInitializer(
        transform = sitk.BSplineTransform(2),
        fixedLandmarks = fixed,
        movingLandmarks = moving,
        referenceImage = img,
        numberOfControlPoints = 4
    )

```

```auto
RuntimeError: Exception thrown in SimpleITK LandmarkBasedTransformInitializer: D:\a\1\sitk-build\ITK\Modules\Core\Common\src\itkPlatformMultiThreader.cxx:238:
ITK ERROR: PlatformMultiThreader(000002203ADDE880): Exception occurred during SingleMethodExecute
d:\a\1\sitk-build\itk-prefix\include\itk-5.2\itkBSplineScatteredDataPointSetToImageFilter.hxx:508:
ITK ERROR: BSplineScatteredDataPointSetToImageFilter(000002203AB10440): The reparameterized point component 1.07741 is outside the corresponding parametric domain of [0, 1).

```

---

<div class="post-metadata">

### Author: ![Flemingjp](https://discourse.itk.org/user_avatar/discourse.itk.org/flemingjp/32/2954_2.png) [@Flemingjp](https://discourse.itk.org/u/Flemingjp)
#### Post date: [September 29, 2022, 10:50am UTC](https://discourse.itk.org/t/solved-bsplinetransformation-from-displacement-vectors/5372/6 "2022-09-29T10:50:47Z")

</div>

To update - the issue was using the incorrect image size compared to the landmark points. The landmark points were beyond the image boundary.

Here is my complete solution

```auto
import numpy as np
import SimpleITK as sitk

# Flatten (n, 2) array of points
fixed = np.array(fixed_points).flatten().astype(float)
moving = np.array(moving_points).flatten().astype(float)

# Create landmark transform
landmark_tx = sitk.LandmarkBasedTransformInitializer(
        transform = sitk.BSplineTransform(2), # Dimension=2 (2D Image)
        fixedLandmarks = fixed,
        movingLandmarks = moving,
        referenceImage = img,
        numberOfControlPoints = 4
    )

# Apply transform to image
rimg = sitk.Resample(img, landmark_tx)

```
