How to translate (or shift) images in a tensor stream

I want my input image (tensor) of my model to shift up / down or left / right, and then on the panel.

For example, if the original image is 3x3, as shown below,

1 2 3
4 5 6
7 8 9

Then if I move left

2 3 0
5 6 0
8 9 0

I found that Tensorflow has an image rotation function, but I could not find a translation or a shift. Please let me know if there is a built-in function, or suggest a way to implement it.

+4
source share
3 answers

I think you can combine tf.image.crop_to_bounding_boxand tf.image.pad_to_bounding_boxto achieve this. Here's the API: https://www.tensorflow.org/api_guides/python/image#Cropping

+2
source

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)
+4

Now for this there is a function (at least with TF v1.6): tf.contrib.image.translate

+1
source

Source: https://habr.com/ru/post/1669830/


All Articles