【问题标题】:Setting values of a tensor at the indices given by tf.where()在 tf.where() 给出的索引处设置张量的值
【发布时间】:2018-07-17 13:05:47
【问题描述】:
我正在尝试向保存图像灰度像素值的张量添加噪声。我想将像素值的随机数设置为 255。
我的想法是这样的:
random = tf.random_normal(tf.shape(input))
mask = tf.where(tf.greater(random, 1))
然后我试图弄清楚如何为mask 的每个索引将input 的像素值设置为 255。
【问题讨论】:
标签:
python
tensorflow
image-processing
noise
【解决方案1】:
tf.where() 也可以用于此,分配掩码元素为True 的噪声值,否则为原始input 值:
import tensorflow as tf
input = tf.zeros((4, 4))
noise_value = 255.
random = tf.random_normal(tf.shape(input))
mask = tf.greater(random, 1.)
input_noisy = tf.where(mask, tf.ones_like(input) * noise_value, input)
with tf.Session() as sess:
print(sess.run(input_noisy))
# [[ 0. 255. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 255.]
# [ 0. 255. 255. 255.]]