如何在tensorflow中翻译(或移位)图像

use*_*700 7 python tensorflow

我想让我的模型的输入图像(张量)向上/向下或向右/向左移动然后再填充.

例如,如果原始图像是3x3,如下所示,

1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)

然后,如果我转向左边,

2 3 0
5 6 0
8 9 0
Run Code Online (Sandbox Code Playgroud)

我发现在Tensorflow中有一个图像旋转功能,但我找不到翻译或移位.如果有内置功能,请告诉我,或建议实施方式.

ast*_*mme 6

我写了一个基于tf.contrib.image.transform执行此操作的函数:https://gist.github.com/astromme/8116a154be8dae5528f33669e490c19a

## Tensorflow image translation op
# images:        A tensor of shape (num_images, num_rows, num_columns, num_channels) (NHWC),
#                (num_rows, num_columns, num_channels) (HWC), or (num_rows, num_columns) (HW).
# tx:            The translation in the x direction.
# ty:            The translation in the y direction.
# interpolation: If x or y are not integers, interpolation comes into play. Options are 'NEAREST' or 'BILINEAR'
def tf_image_translate(images, tx, ty, interpolation='NEAREST'):
    # got these parameters from solving the equations for pixel translations
    # on https://www.tensorflow.org/api_docs/python/tf/contrib/image/transform
    transforms = [1, 0, -tx, 0, 1, -ty, 0, 0]
    return tf.contrib.image.transform(images, transforms, interpolation)
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

translation_op = tf_image_translate(images, tx=-5, ty=10)

with tf.Session() as sess:
    translated_images = sess.run(translation_op)
Run Code Online (Sandbox Code Playgroud)


sir*_*ogo 5

现在有一个功能(至少,从 TF v1.6 开始): tf.contrib.image.translate


sol*_*ice 4

我认为你可以结合tf.image.crop_to_bounding_boxtf.image.pad_to_bounding_box实现这一目标。这是 API: https: //www.tensorflow.org/api_guides/python/image#Cropping