Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate(or shift) images in tensorflow

I want to make my input image (tensor) of my model to shift up/down or right/left and then pad.

For example, if the original image is 3x3 like below,

1 2 3
4 5 6
7 8 9

Then, if I shift to left,

2 3 0
5 6 0
8 9 0

I found that there is an image rotate function in Tensorflow but I couldn't find translate or shift. Please let me know if there is a built-in function, or suggest the way to implement.

like image 620
user270700 Avatar asked Feb 15 '17 14:02

user270700


2 Answers

I wrote a function to do this based on 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)

Use it like this:

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

with tf.Session() as sess:
    translated_images = sess.run(translation_op)
like image 78
Andrew Stromme Avatar answered Sep 28 '22 02:09

Andrew Stromme


There is now a function for this (at least, as of TF v1.6): tf.contrib.image.translate

like image 38
sirgogo Avatar answered Sep 28 '22 02:09

sirgogo