# Bilinear interpolation behavior SimpleITK

**URL:** https://discourse.itk.org/t/bilinear-interpolation-behavior-simpleitk/4123
**Category:** Beginner Questions
**Tags:** python, simpleitk
**Created:** [May 12, 2021, 10:05am UTC](https://discourse.itk.org/t/bilinear-interpolation-behavior-simpleitk/4123 "2021-05-12T10:05:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nhjnhjnhj](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/258eb7/32.png) [@nhjnhjnhj](https://discourse.itk.org/u/nhjnhjnhj)
#### Post date: [May 12, 2021, 10:05am UTC](https://discourse.itk.org/t/bilinear-interpolation-behavior-simpleitk/4123/1 "2021-05-12T10:05:06Z")

</div>

Hi,

This code [bilinear\_inter.py · GitHub](https://gist.github.com/Jonas1312/e0d4729a431fc956be73eda54a13d027) compares how linear interpolation is implemented in opencv, pytorch and simpleitk libraries.

The input array is:  
[[1, 2],  
[3, 4]]

The output of this script is the following:

cv2:  
[[1. 1.25 1.75 2.]  
[1.5 1.75 2.25 2.5]  
[2.5 2.75 3.25 3.5]  
[3. 3.25 3.75 4.]]

torch (no align corners):  
tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],  
[1.5000, 1.7500, 2.2500, 2.5000],  
[2.5000, 2.7500, 3.2500, 3.5000],  
[3.0000, 3.2500, 3.7500, 4.0000]]]])

torch (align corners):  
tensor([[[[1.0000, 1.3333, 1.6667, 2.0000],  
[1.6667, 2.0000, 2.3333, 2.6667],  
[2.3333, 2.6667, 3.0000, 3.3333],  
[3.0000, 3.3333, 3.6667, 4.0000]]]])

sitk:  
[[1. 1.5 2. 0.]  
[2. 2.5 3. 0.]  
[3. 3.5 4. 0.]  
[0. 0. 0. 0.]]

cv2 does not align corners while pytorch gives the choice (align\_corners parameter).

However, I don’t understand why SimpleITK gives such an output… I’d like to know what’s wrong in my code ([bilinear\_inter.py · GitHub](https://gist.github.com/Jonas1312/e0d4729a431fc956be73eda54a13d027)).

Thanks

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [May 12, 2021, 12:22pm UTC](https://discourse.itk.org/t/bilinear-interpolation-behavior-simpleitk/4123/2 "2021-05-12T12:22:20Z")

</div>

Hello,

Please review SimpleITK’s Fundamental Concepts on the Image:  
[https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html#images](https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html#images)

I’d recommend drawing the original input image, including its origin, and identifying the locations of the corners of the image. Then doing the same for the output image you are requesting from the ResampleImageFilter.

---

<div class="post-metadata">

### Author: ![nhjnhjnhj](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/258eb7/32.png) [@nhjnhjnhj](https://discourse.itk.org/u/nhjnhjnhj)
#### Post date: [May 12, 2021, 12:59pm UTC](https://discourse.itk.org/t/bilinear-interpolation-behavior-simpleitk/4123/3 "2021-05-12T12:59:07Z")

</div>

> resample.SetOutputOrigin((-0.25, -0.25))

thanks @blowekamp
