2017-02-15 52 views
2

我想將我的模型的輸入圖像(張量)向上/向下或向右/向左移動,然後放大。如何在張量流中翻譯(或移位)圖像

例如,如果原始圖像是像下面的3x3,

1 2 3 
4 5 6 
7 8 9 

然後,如果我左移,

2 3 0 
5 6 0 
8 9 0 

我發現有在Tensorflow圖象旋轉的功能,但我無法找到翻譯或轉移。 請讓我知道,如果有一個內置的功能, 或建議的方式來實現。

回答

3

我寫了一個函數來完成此基礎上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)