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

时间:2017-02-15 14:25:45

标签: python tensorflow

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

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

1 2 3
4 5 6
7 8 9

然后,如果我转向左边,

2 3 0
5 6 0
8 9 0

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

3 个答案:

答案 0 :(得分:5)

我根据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)

像这样使用:

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

with tf.Session() as sess:
    translated_images = sess.run(translation_op)

答案 1 :(得分:3)

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

答案 2 :(得分:3)

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