![](/img/trans.png)
[英]using tf.where() to select 3d tensor by 2d conditions & replacing elements in a 2d indices with keys and values
[英]Setting values of a tensor at the indices given by tf.where()
我正在尝试将噪声添加到容纳图像灰度像素值的张量中。 我想将像素值的随机数设置为255。
我在考虑以下方面:
random = tf.random_normal(tf.shape(input))
mask = tf.where(tf.greater(random, 1))
然后尝试找出如何为mask
每个索引将input
的像素值设置为255。
tf.where()
也可以用于此,在mask元素为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.]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.