# Visualize 2D transform

**URL:** <https://discourse.itk.org/t/visualize-2d-transform/3896>\
**Category:** Algorithms\
**Tags:** simpleitk, registration, python\
**Created:** [February 24, 2021, 10:04am UTC](https://discourse.itk.org/t/visualize-2d-transform/3896 "2021-02-24T10:04:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [February 24, 2021, 10:04am UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/1 "2021-02-24T10:04:31Z")

</div>

Hi all,  
I am using SimpleITK to perform non rigid registration (bsplines). I am also using 3d Slicer to visualize the produced transform. Now the transform .tfm file is only read by slicer if it’s a 3d transform and not 2d. What I am currently doing is to compute the dense displacement field and then saving it as an image which slicers accepts. However I am wondering if there is a better way by using only the .tfm file? something like converting the 2d bspline transform to a 3d version of it so that slicer is able to read it?  
Thanks!

---

<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:** [February 24, 2021, 3:07pm UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/2 "2021-02-24T15:07:54Z")

</div>

Hello @rogertrullo ,

Please see code below (haven’t tested it beyond seeing that it doesn’t crash). Basically, the idea is to convert the transformation to a 3D one, where the displacements for the BSpline control points in the z direction are all zeros.

```auto
import SimpleITK as sitk

bspline_2d_file_name = 'bspline_example.tfm'
tx = sitk.BSplineTransform(sitk.ReadTransform(bspline_2d_file_name))
coefficient_images = tx.GetCoefficientImages()

new_coeffs = []
for img in coefficient_images:
    new_image = sitk.Image(img.GetSize() + (1,), img.GetPixelID())
    new_image.SetOrigin(img.GetOrigin() + (0,))
    new_image.SetSpacing(img.GetSpacing() + (1,))
    new_image[:,:,0] = img
    new_coeffs.append(new_image)

# Add zero displacements in z direction.
new_coeffs.append(sitk.Image(new_coeffs[0].GetSize(),new_coeffs[0].GetPixelID()))
bspline3d = sitk.BSplineTransform(new_coeffs, tx.GetOrder())
sitk.WriteTransform(bspline3d, '3d_'+bspline_2d_file_name)

```

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [February 26, 2021, 2:49pm UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/3 "2021-02-26T14:49:02Z")

</div>

Thank you very much @zivy vy  
I am having an issue in this line:

```
new_image[:,:,0] = img

```

_SimpleITK/SimpleITK.py in **setitem** (self, idx, value)_  
_#the index parameter was an invalid set of objects_  
_raise IndexError(“invalid index”)_

The shapes seem to correspond well:

```
print(new_image.GetSize())
(63, 51, 1)
print(img.GetSize())
(63, 51)    

```

Do you have any idea what can be the issue?  
Thanks again!

---

<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:** [February 26, 2021, 2:55pm UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/4 "2021-02-26T14:55:34Z")

</div>

Hello @rogertrullo,

I am working with the latest version of SimpleITK, 2.0.2. Support for this assignment style was added in the 2.x releases.

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [February 26, 2021, 2:58pm UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/5 "2021-02-26T14:58:00Z")

</div>

I have this version:  
SimpleITK-2.0.0rc2.dev908+g8244e  
I will update and try again

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [February 26, 2021, 3:04pm UTC](https://discourse.itk.org/t/visualize-2d-transform/3896/6 "2021-02-26T15:04:39Z")

</div>

it works now! thank you so much!
