tf.image.resize_bilinear和cv2.resize的区别

18

tf.image.resize_bilinear的结果与cv2.resize相比差别很大。

我觉得这有点令人困扰。将align_corners=True设置为始终合理并不总是因为四个角不总是应该固定在角落里。那么有没有办法使它更加“对称”一些呢?

可复现的代码:

import tensorflow as tf
import numpy as np
import cv2
np.set_printoptions(precision=3)
resize_shape = (10, 10)

a = np.ones((1, 2, 2, 1), dtype=np.float32)
a[0, 0, 0, 0] = 5.0
a[0, 1, 1, 0] = 5.0

b = tf.constant(a, dtype=tf.float32)
c = tf.image.resize_bilinear(b, resize_shape)

with tf.Session() as sess:
    np_c = sess.run(c)
    print np_c[0, :, :, 0]

print cv2.resize(a[0], resize_shape, interpolation=cv2.INTER_LINEAR)

获得的结果:

# tf.image.resize_bilinear
[[ 5.    4.2   3.4   2.6   1.8   1.    1.    1.    1.    1.  ]
 [ 4.2   3.72  3.24  2.76  2.28  1.8   1.8   1.8   1.8   1.8 ]
 [ 3.4   3.24  3.08  2.92  2.76  2.6   2.6   2.6   2.6   2.6 ]
 [ 2.6   2.76  2.92  3.08  3.24  3.4   3.4   3.4   3.4   3.4 ]
 [ 1.8   2.28  2.76  3.24  3.72  4.2   4.2   4.2   4.2   4.2 ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]]
# cv2.resize
[[ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 4.2   4.2   4.2   3.72  3.24  2.76  2.28  1.8   1.8   1.8 ]
 [ 3.4   3.4   3.4   3.24  3.08  2.92  2.76  2.6   2.6   2.6 ]
 [ 2.6   2.6   2.6   2.76  2.92  3.08  3.24  3.4   3.4   3.4 ]
 [ 1.8   1.8   1.8   2.28  2.76  3.24  3.72  4.2   4.2   4.2 ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]]

编辑

当设置align_corners=True时,图像的4个角和调整大小后的图像会对齐,但只有4个像素

考虑到调整大小的图像,图像中的4个角应该呈现为调整大小后图像的区域(就像cv2.resize一样),而不是在图像的角落处。

# tf.image.resize_bilinear(b, resize_shape, align_corners=True)
[[ 5.    4.56  4.11  3.67  3.22  2.78  2.33  1.89  1.44  1.  ]
 [ 4.56  4.21  3.86  3.52  3.17  2.83  2.48  2.14  1.79  1.44]
 [ 4.11  3.86  3.62  3.37  3.12  2.88  2.63  2.38  2.14  1.89]
 [ 3.67  3.52  3.37  3.22  3.07  2.93  2.78  2.63  2.48  2.33]
 [ 3.22  3.17  3.12  3.07  3.02  2.98  2.93  2.88  2.83  2.78]
 [ 2.78  2.83  2.88  2.93  2.98  3.02  3.07  3.12  3.17  3.22]
 [ 2.33  2.48  2.63  2.78  2.93  3.07  3.22  3.37  3.52  3.67]
 [ 1.89  2.14  2.38  2.63  2.88  3.12  3.37  3.62  3.86  4.11]
 [ 1.44  1.79  2.14  2.48  2.83  3.17  3.52  3.86  4.21  4.56]
 [ 1.    1.44  1.89  2.33  2.78  3.22  3.67  4.11  4.56  5.  ]]

“四个角落不总是应该固定在角落里。”您能再详细解释一下吗?我不确定您的意思。 - alkasm
@AlexanderReynolds 谢谢!我编辑了问题,希望更清晰明了。 - LI Xuhong
2
我之前写过一篇关于这个问题的解释,链接在这里。自那以后,OpenCV风格的调整大小已经成为TF v2.0中的一个选项。 - jake
2个回答

11


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接