tf.image.resize_bilinear与cv2.resize [英] tf.image.resize_bilinear vs cv2.resize

查看:601
本文介绍了tf.image.resize_bilinear与cv2.resize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tf.image.resize_bilinear的结果与cv2.resize完全不同.

我觉得这有点麻烦.设置align_corners=True并不总是合理的,因为四个角不一定总是固定在该角上.那么有什么办法使它更对称"吗?

I found this a little bothersome. Set align_corners=True is not always reasonable because the four corners are not always supposed to be fixed in the corner. So is there anyway to make it a little more "symmetry"?

要复制的代码:

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个像素.

When setting align_corners=True, 4 corners of images and resized images are aligned but only 4 pixels.

考虑到调整图像的大小,图像的4个角应在调整大小后的图像的4个角(如cv2.resize一样)显示 area (区域),而不是在正角4个点. >

Considering resizing images, the 4 corners in the image should present the areas in 4 corners of the resized image (like cv2.resize does), instead of 4 points at the very corner.

# 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.  ]]

推荐答案

这是一个已知问题,请参阅 https://github.com/tensorflow/tensorflow/issues/6720

This is a known issue, please see https://github.com/tensorflow/tensorflow/issues/6720

这篇关于tf.image.resize_bilinear与cv2.resize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆