我想让我的模型的输入图像(张量)向上/向下或向右/向左移动,然后填充。
例如,如果原始图像是如下所示的 3x3,
1 2 3
4 5 6
7 8 9
然后,如果我向左移动,
2 3 0
5 6 0
8 9 0
我发现 Tensorflow 中有图像旋转功能,但我找不到平移或移位功能。 如果有内置功能,请告诉我, 或建议实现方式。
最佳答案
我基于 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)
关于python - 如何在 tensorflow 中平移(或移动)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42252040/