# Differences after applying forward & inverse transform

**URL:** <https://discourse.itk.org/t/differences-after-applying-forward-inverse-transform/6536>\
**Category:** Beginner Questions\
**Tags:** python\
**Created:** [March 20, 2024, 9:45am UTC](https://discourse.itk.org/t/differences-after-applying-forward-inverse-transform/6536 "2024-03-20T09:45:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mckay42](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mckay42](https://discourse.itk.org/u/Mckay42)\
**Post date:** [March 20, 2024, 9:45am UTC](https://discourse.itk.org/t/differences-after-applying-forward-inverse-transform/6536/1 "2024-03-20T09:45:05Z")

</div>

Hi all,

I started using sitk a while ago and hope to use it for registration tasks in my project.  
While playing around with it, I tried to apply a simple Euler3D transform to my image and revert it with the the inverse transform.

**Expectation:** I should get my original image back with minor errors.

**Result:** I get rather large absolute errors in the order of double digit percentages.

I assume I missed something and this is intended behavior?  
Any tips and insights are appreciated.

My code runs in a Jupyter Notebook in VS Code:  
python 3.12.2  
sitk 2.3.1

```auto
import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as sitk

fixed_image = sitk.ReadImage("../data/training_001_ct.mha", sitk.sitkFloat32)

p1_transform = sitk.Euler3DTransform()
p1_transform.SetCenter(fixed_image.TransformContinuousIndexToPhysicalPoint(np.array(fixed_image.GetSize()) / 2.0))

p1_transform.SetTranslation([0, 0, 10])
p1_transform.SetRotation(0, 0, np.deg2rad(5))

moving_image = sitk.Resample(fixed_image, p1_transform, interpolator=sitk.sitkLinear)
moving_resampled = sitk.Resample(moving_image, p1_transform.GetInverse(), interpolator=sitk.sitkLinear)

difference = sitk.GetArrayFromImage(fixed_image)-sitk.GetArrayFromImage(moving_resampled)

plt.imshow(np.abs(difference[15,:,:]), interpolation='none', vmax=700, cmap="Greys_r")
plt.title("|difference| ; fixed-moving")
plt.colorbar()

```

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

---

<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:** [March 21, 2024, 1:31am UTC](https://discourse.itk.org/t/differences-after-applying-forward-inverse-transform/6536/2 "2024-03-21T01:31:48Z")

</div>

Hello @Mckay42,

Your expectation is not valid. What you are experiencing is due to interpolation and is not surprising when dealing with digital signals. Repeated interpolation is an issue. Also, because this specific image has a pretty large spacing in the z direction and you are translating in that direction the effects of using linear interpolation are more significant. Generally speaking repeated interpolation is discouraged (see [this notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/21_Transforms_and_Resampling.ipynb) section titled **Repeated Resampling - Just Say No** ).

The choice of interpolation method also plays a role, available interpolators are listed [here](https://simpleitk.org/doxygen/latest/html/namespaceitk_1_1simple.html#a7cb1ef8bd02c669c02ea2f9f5aa374e5). Linear is usually a compromise between interpolation quality and computational complexity.  
Code below illustrates this using a 2D image (easier to visualize):

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

fixed_image = sitk.ReadImage("training_001_ct.mha", sitk.sitkFloat32)
mid_slice = fixed_image.GetDepth() // 2
fixed_image = fixed_image[:,:,mid_slice]

p1_transform = sitk.Euler2DTransform()
p1_transform.SetCenter(fixed_image.TransformContinuousIndexToPhysicalPoint(np.array(fixed_image.GetSize()) / 2.0))

p1_transform.SetTranslation([0, 10])
p1_transform.SetAngle(np.deg2rad(5))

interps = {"linear": sitk.sitkLinear,
           "BSpline3": sitk.sitkBSpline3}

for s, interp in interps.items(): 
    moving_image = sitk.Resample(fixed_image, p1_transform, interpolator=interp)
    moving_resampled = sitk.Resample(moving_image, p1_transform.GetInverse(), interpolator=interp)
    diff_abs = sitk.Abs(fixed_image - moving_resampled)[50:-50,50:-50] #cropping to ignore changes introduced at image edges by default value
    print(f"mean absolute difference using {s}: {np.sum(sitk.GetArrayViewFromImage(diff_abs))/diff_abs.GetNumberOfPixels()}")

```

---

<div class="post-metadata">

**Author:** ![Mckay42](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mckay42](https://discourse.itk.org/u/Mckay42)\
**Post date:** [March 21, 2024, 5:54am UTC](https://discourse.itk.org/t/differences-after-applying-forward-inverse-transform/6536/3 "2024-03-21T05:54:18Z")

</div>

Thanks for the quick response!  
After playing around some more it was already dawning on me that his might not be the best idea.
