# Unexpected Behavior of LinearInterpolateImageFunction

**URL:** <https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450>\
**Category:** Algorithms\
**Created:** [September 24, 2021, 5:42pm UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450 "2021-09-24T17:42:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krm15](https://discourse.itk.org/user_avatar/discourse.itk.org/krm15/32/2293_2.png) [@krm15](https://discourse.itk.org/u/krm15)\
**Post date:** [September 24, 2021, 5:42pm UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450/1 "2021-09-24T17:42:33Z")

</div>

Here is an example of using the LineageInterpolateImageFunction from the ITKSphinxExamples repository:  
[https://itk.org/ITKExamples/src/Core/ImageFunction/LinearlyInterpolatePositionInImage/Documentation.html](https://itk.org/ITKExamples/src/Core/ImageFunction/LinearlyInterpolatePositionInImage/Documentation.html)

In this example, a continuous value of 1.3 is located between index1 and index2 and at a distance of 0.3. Since the pixel values are 10 and 20 at these two indices, you get an output of 13.

However, if we change the image spacing to 0.5, the continuous position of 1.3 would be one would expect a different output of (0.7 x 20 + 0.3 x 30) = 23. But it appears that the interpolate function does not take into account image spacing. This is a problem because if we have different spacings in X, Y and Z (as is the case in microscopy modalities), the interpolation result wont be accurate.

Kishore

---

<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:** [September 24, 2021, 7:00pm UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450/2 "2021-09-24T19:00:08Z")

</div>

Linear interpolation works fine. ITK uses convention that pixel’s location designates its center, not a corner. See [ITKSoftwareGuide-Book1, section 4.1.4 (Defining Origin and Spacing)](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html#x45-500004.1) and possibly [ITKSoftwareGuide-Book2, chapter 3](https://itk.org/ITKSoftwareGuide/html/Book2/ITKSoftwareGuide-Book2ch3.html#x26-1360003.10).

That example should be remade to have non-unit spacing to better demonstrate the math.

P.S. Now I see that you made an error in your math. With pixel spacing of 0.5, location 1.3 no longer falls 30% into a pixel.

**Unit spacing:**

- pixel 0 has extent -0.5, 0.5
- pixel 1 has extent 0.5, 1.5
- pixel 2 has extent 1.5, 2.5

Physical location 1.3 is 30% of spacing away from center of pixel 1 and 70% of spacing away from center of pixel 2.

**Spacing 0.5:**

- pixel 0 has extent -0.25, 0.25
- pixel 1 has extent 0.25, 0.75
- pixel 2 has extent 0.75, 1.25
- pixel 3 has extent 1.25, 1.75

Physical location 1.3 is 40% of spacing away from center of pixel 3 and 60% of spacing away from center of pixel 2.

@krm15 do you want to propose a PR to update that example with the above math?

---

<div class="post-metadata">

**Author:** ![krm15](https://discourse.itk.org/user_avatar/discourse.itk.org/krm15/32/2293_2.png) [@krm15](https://discourse.itk.org/u/krm15)\
**Post date:** [September 25, 2021, 2:45am UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450/3 "2021-09-25T02:45:45Z")

</div>

@dzenanz : Yes, I got the convention correct now – thank you!

But even then, going by your calculation for the spacing of 0.5, the pixel value is 0.6 x 30 (pixel 3) + 0.4 x 20 (pixel 2) = 26. The answer we get from the code block is 13 irrespective of what the image spacing is set to.

How do I propose a PR?

---

<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:** [September 25, 2021, 5:40pm UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450/4 "2021-09-25T17:40:23Z")

</div>

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md>

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [October 27, 2021, 12:53pm UTC](https://discourse.itk.org/t/unexpected-behavior-of-linearinterpolateimagefunction/4450/5 "2021-10-27T12:53:14Z")

</div>

Note that the examples repository is here:

> **[GitHub - InsightSoftwareConsortium/ITKSphinxExamples: Cookbook examples for...](https://github.com/InsightSoftwareConsortium/ITKSphinxExamples)**
>
> Cookbook examples for the Insight Toolkit documented with Sphinx - GitHub - InsightSoftwareConsortium/ITKSphinxExamples: Cookbook examples for the Insight Toolkit documented with Sphinx
