Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tf.where()给出的索引处设置张量值_Python_Tensorflow_Image Processing_Noise - Fatal编程技术网

Python 在tf.where()给出的索引处设置张量值

Python 在tf.where()给出的索引处设置张量值,python,tensorflow,image-processing,noise,Python,Tensorflow,Image Processing,Noise,我试图给一个张量添加噪声,这个张量包含图像的灰度像素值。我想将像素值的随机数设置为255 我的想法大致如下: random = tf.random_normal(tf.shape(input)) mask = tf.where(tf.greater(random, 1)) 然后我试图找出如何为掩码的每个索引将输入的像素值设置为255。where()也可以用于此,在掩码元素为True的地方指定噪声值,否则原始输入值: import tensorflow as tf input = tf.zer

我试图给一个张量添加噪声,这个张量包含图像的灰度像素值。我想将像素值的随机数设置为255

我的想法大致如下:

random = tf.random_normal(tf.shape(input))
mask = tf.where(tf.greater(random, 1))
然后我试图找出如何为
掩码
的每个索引将
输入的像素值设置为255。where()
也可以用于此,在掩码元素为
True
的地方指定噪声值,否则原始
输入值:

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