Skip to content

Instantly share code, notes, and snippets.

@astromme
Last active July 24, 2018 17:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astromme/8116a154be8dae5528f33669e490c19a to your computer and use it in GitHub Desktop.
Save astromme/8116a154be8dae5528f33669e490c19a to your computer and use it in GitHub Desktop.
import tensorflow as tf
## 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)
@astromme
Copy link
Author

Example use:

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment