Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bilinear interpolation implementations in Tensorflow and OpenCV

I'm trying to get a handle on bilinear interpolation as implemented in tensorflow and OpenCV. My understanding leads me to believe that the grid of interpolation points is positioned differently in the two libraries. This is evidenced by the different results obtained on a sample matrix:

import tensorflow as tf, numpy as np, cv2
a = np.arange(9, dtype=np.float32).reshape(3, 3)
cv2.resize(a, (2, 2))

outputs

array([[1. , 2.5],
       [5.5, 7. ]], dtype=float32)

while

tf.InteractiveSession()
tf.image.resize_images(a[None, :, :, None], (2, 2)).eval()[0, :, :, 0]

outputs

array([[0. , 1.5],
       [4.5, 6. ]], dtype=float32)

Is that diagnosis correct? If so, what are the schemes for laying down interpolation points in tensorflow and OpenCV?

like image 447
Alex Avatar asked Nov 08 '22 11:11

Alex


1 Answers

Commonly (in OpenCV, Matlab, scipy, etc.), pixels are assumed to cover unit areas and what is aligned are the very corners of the top-left and bottom-right pixels. Sampling interval changes to old_size / new_size.

In Tensorflow, there are two brand new schemes, neither of which matches the one above.

For align_corners=True, the corner pixel centers are aligned with new sampling interval of (old_size - 1) / (new_size - 1) in-between. For align_corners=False, only the top-left pixel centers are aligned and the rest is sampled with interval old_size / new_size.

like image 181
Tomas Petricek Avatar answered Nov 11 '22 18:11

Tomas Petricek