python - 如何在 tensorflow 中平移(或移动)图像

标签 python tensorflow

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

例如,如果原始图像是如下所示的 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/

相关文章:

python - 容器与 python 请求通信

python - 从 Tensorflow PrefetchDataset 中提取目标

python-3.x - 在tensorflow .ckpt文件中使用预训练模型

tensorflow - 可变大小的卷积神经网络输入和固定输出

python - Keras:简单数据的简单神经网络不起作用

python - 如何将 python 字典的键显示为 HTML 表头,并将每个键的值显示为该表头下的一行?

python - scipy:哪些发行版实现了 "fit"函数?

python - 如何使用 Elixir/SQLAlchemy 进行原子递增/递减

python - 在 Mac osx 10.10.2 上使用 Python 2.7.9 Anaconda 2.2.0 编译 igraph 的 C 核心时出现问题

仅在 CPU 上使用 CUDA 的 Tensorflow 运行版本