Python 如何将像素值替换为tensorflow操作?
我需要将图像中的像素值替换为图形中的操作。不幸的是,事先这样做不是一种选择,因为这是优化过程的一部分 在我找到解决方案之前,我只是简单地使用Python 如何将像素值替换为tensorflow操作?,python,tensorflow,image-processing,Python,Tensorflow,Image Processing,我需要将图像中的像素值替换为图形中的操作。不幸的是,事先这样做不是一种选择,因为这是优化过程的一部分 在我找到解决方案之前,我只是简单地使用tf.py_func(),但由于这个操作需要执行很多次,所以速度非常慢,效率也很低 # numpy function to perturb a single pixel in an image def perturb_image(pixel, img): # At each pixel's x,y position, assign its rgb v
tf.py_func()
,但由于这个操作需要执行很多次,所以速度非常慢,效率也很低
# numpy function to perturb a single pixel in an image
def perturb_image(pixel, img):
# At each pixel's x,y position, assign its rgb value
x_pos, y_pos, r, g, b = pixel
rgb = [r,g,b]
img[x_pos, y_pos] = rgb
return img
# pixel is a 1D tensor like [x-dim,y-dim,R,G,B]
# image is tensor with shape (x-dim,y-dim,3)
img_perturbed = tf.py_func(perturb_image,[pixel, image], tf.uint8)
我想到的一种方法是使用tf.add(扰动,图像)
,其中两者都具有相同的维度,扰动都是零,除了像素位置,需要将其RGB值更改为上述代码片段中pixel
中定义的相同值。不幸的是,我需要重写很多关于这个操作的代码,我正试图避免这些代码
您能想出一个解决方案,用另一个使用相同输入的tensorflow操作替换py_func
非常感谢您的帮助