python - 如何在 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]
解决方案
这是一种可能的实现:
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. ]]
推荐阅读
- java - 从列表中获取文本
在 Groovy 中 - apache-spark - Spark:如何在 spark-submit 中设置 spark.yarn.executor.memoryOverhead 属性
- apache-flink - Flink 如何处理不适合内存的窗口和分区?
- python - 坚持如何抓取包含 JavaScript 表单的网页
- javascript - 如何用纯 JS 改变对象结构
- python - 解码谷歌云存储python api返回的protobuf文件
- javascript - Jasmine - 如何在没有实现的情况下监视函数
- python - 如何在pyspark中打印具有特征名称的随机森林的决策路径?
- jenkins - 我在哪里可以获得 Jenkins RestAPI 的官方文档,其中列出了所有可用的 API?
- r - 带有“gp”的 GAM 更平滑:在新位置进行预测