代码之家  ›  专栏  ›  技术社区  ›  Pumpkin

如何在TensorFlow中将小图像添加到大图像中?

  •  1
  • Pumpkin  · 技术社区  · 7 年前

    我想把一个较小的图像叠加到一个较大的图像上。

    我试过在一个切片上添加,但无法使其工作。

    因此,作为一个简单的例子,我如何在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]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   javidcf    7 年前

    这是一个可能的实现:

    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. ]]