首页 > 解决方案 > 如何在 Tensorflow 中将小图像添加到大图像?

问题描述

我想将较小的图像叠加到较大的图像上。

我尝试添加到切片中,但无法使其正常工作。

所以,作为一个简单的例子,我如何在 Tensorflow 中执行这个 NumPy 操作:

a = np.array([1, 1, 1, 1])
b = np.array([5, 5])
c = a
c[1:3] = c[1:3] + b
print(c)
# => [1 6 6 1]

标签: pythontensorflow

解决方案


这是一种可能的实现:

import tensorflow as tf

# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
    img_shape = tf.shape(img)
    img_rows, img_cols = img_shape[0], img_shape[1]
    patch_shape = tf.shape(patch)
    patch_rows, patch_cols = patch_shape[0], patch_shape[1]
    i_end = i + patch_rows
    j_end = j + patch_cols
    # Mix patch: alpha from patch, minus alpha from image
    overlay = alpha * (patch - img[i:i_end, j:j_end])
    # Pad patch
    overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
    # Make final image
    img_overlay = img + overlay_pad
    return img_overlay

测试:

img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, [])
j = tf.placeholder(tf.int32, [])
alpha = tf.placeholder(tf.float32, [])
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
    result = sess.run(img_overlay, feed_dict={
        img: [[[ 1], [ 2], [ 3], [ 4]],
              [[ 5], [ 6], [ 7], [ 8]],
              [[ 9], [10], [11], [12]],
              [[13], [14], [15], [16]]],
        patch: [[[10], [20], [30]],
                [[40], [50], [60]]],
        i: 2, j: 1, alpha: 0.5
    })
    print(result[..., 0])

输出:

[[ 1.   2.   3.   4. ]
 [ 5.   6.   7.   8. ]
 [ 9.  10.  15.5 21. ]
 [13.  27.  32.5 38. ]]

推荐阅读