# Get Jacobian wrt to Position for BSplineTransform

**URL:** <https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504>\
**Category:** Algorithms\
**Tags:** itk\
**Created:** [April 24, 2025, 3:57pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504 "2025-04-24T15:57:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [April 24, 2025, 3:57pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/1 "2025-04-24T15:57:26Z")

</div>

As far as I can tell, the function `ComputeJacobianWithRespectToPosition` is not implemented for BSplineTransform (as it is for DisplacementFieldTransform) but only `ComputeJacobianWithRespectToParameters`, for example using this snippet:

```cpp
using BSplineTransformType = itk::BSplineTransform<double, 3, 3>;
using JType = BSplineTransformType::JacobianType;
using PType = BSplineTransformType::InputPointType;

// [...] read transform into tfm from file
const BSplineTransformType::Pointer bspline = static_cast<BSplineTransformType *>(tfm.GetPointer());

auto J = JType();
// some point p
auto p = PType({4, 5, 6});
// get Jacobian at point {4, 5, 6}
bspline->ComputeJacobianWithRespectToParameters(p, J);

```

Is there a way to get the Jacobian matrix from the Jacobian of the Parameters or is the easiest way to use a DisplacementFieldTransform for that? I guess I can apply the chain rule to get the correct derivatives, but how to get to the missing derivates… I have no idea 😅  
I have searched for a while for an example or some usage in the code but could not find any - or I’m overlooking something…

---

<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:** [April 25, 2025, 1:19pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/2 "2025-04-25T13:19:34Z")

</div>

One option is to convert the bspline transform to displacement field transform and use its `ComputeJacobianWithRespectToParameters` method.

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [April 25, 2025, 5:18pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/3 "2025-04-25T17:18:24Z")

</div>

Yep, I did that so far. It requires me to build the displacement field first, which takes some amount of memory and time. Calculating it directly from the parametrization would be much more efficient.

Maybe it isn’t that easy and thus the `ComputeJacobianWithRespectToPosition` isn’t implemented?

---

<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:** [April 26, 2025, 2:40pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/4 "2025-04-26T14:40:36Z")

</div>

If you want to compute the derivatives at random points then you can use `vtkGridTransform`. It is pretty amazing, it can compute derivatives of both forward and inversre transform at any point, very quickly. We use VTK transform classes in 3D Slicer exactly because ITK requires computing a dense displacement field for many operations, which is just way too slow.

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [April 27, 2025, 9:39am UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/5 "2025-04-27T09:39:48Z")

</div>

That one sounds extremely interesting! However, why does it not compute the derivative of the inverse:

> Calculate the inverse transform as well as the derivative of the forward transform (that’s correct: the derivative of the forward transform, not of the inverse transform)

[VTK: vtkGridTransform Class Reference](https://vtk.org/doc/nightly/html/classvtkGridTransform.html#a3f4a1f4b85146d52a3eb983ec02c7100)

---

<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:** [April 27, 2025, 5:00pm UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/6 "2025-04-27T17:00:50Z")

</div>

This is actually a good thing! It means when you implement a new transform type then you only need to implement the derivative of the forward transform. The inverse transform and its derivative are automatically computed for you! To get the inverse transform, simply call the `GetInverse()` method and then you can get the derivative from that.

It works for all important transform types (linear, b-spline, thin-plate-spline, and grid), even for arbitrary composition and any combination of inversion of any components. Transformation is available for the entire 3D space (not limited to the area of b-spine grid, etc) by smooth extrapolation, allowing robust inverse computation near the edges.

This is all really amazing, pretty much solves all spatial transformation related tasks in medical image computing. Credits go to David Gobbi.

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [April 28, 2025, 5:33am UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/7 "2025-04-28T05:33:28Z")

</div>

> [@lassoan](#):
>
> It works for all important transform types (linear, b-spline, thin-plate-spline, and grid), even for arbitrary composition and any combination of inversion of any components. Transformation is available for the entire 3D space (not limited to the area of b-spine grid, etc) by smooth extrapolation, allowing robust inverse computation near the edges.

Could you hint me to an example where this is used in combination with BSpline to get the derivatives of the forward and inverse transform?  
Is it possible to translate an existing ITK BSplineTransform to VTK?

---

<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:** [May 10, 2025, 12:53am UTC](https://discourse.itk.org/t/get-jacobian-wrt-to-position-for-bsplinetransform/7504/8 "2025-05-10T00:53:08Z")

</div>

> [@reox](#):
>
> Is it possible to translate an existing ITK BSplineTransform to VTK?

You can use this class in 3D Slicer to convert between ITK and VTK transforms:

> <https://github.com/Slicer/Slicer/blob/main/Libs/MRML/Core/vtkITKTransformConverter.h>
