tensorflow - 如何在 tensorflow 中将输入图像与掩码相乘?

标签 tensorflow

我想将每个输入图像乘以与输入图像大小相同的掩码。我将如何在 tensorflow 中做到这一点?

到目前为止,我的图像读取功能如下所示:

img_contents = tf.read_file(input_queue[0])
label_contents = tf.read_file(input_queue[1])
img = tf.image.decode_png(img_contents, channels=3) 
label = tf.image.decode_png(label_contents, channels=1)

# Now I want to do something like this?
mask = tf.constant(1.0, dtype=tf.float32, shape=img.shape)
img_masked = tf.multiply(img,mask)

这可能吗? 不确定 img 是否已经是张量对象,我可以在这里使用该函数。我是 tensorflow 的新手...

最佳答案

这是适合我的代码。我正在使用 jupyter notebook 来运行代码。

%matplotlib inline
import tensorflow as tf
from matplotlib.image import imread 
import matplotlib.pyplot as plt

# Loading test image from the local filesystem 
x = tf.Variable(imread("test_img.jpg"),dtype='float32')
x_mask = tf.Variable(imread("test_mask.jpg"),dtype='float32')
img_mult = tf.multiply(x,x_mask)

plt.imshow(imread("test_img.jpg"))
plt.show()
plt.imshow(imread("test_mask.jpg"))
plt.show()

sess = tf.Session() 
sess.run(tf.global_variables_initializer())
res = sess.run(img_mult)

plt.imshow(res)
plt.show()

此外,这是一个很好的 YouTube 教程,涵盖了使用 TF 进行图像处理:https://www.youtube.com/watch?v=bvHgESVuS6Q&t=447s

关于tensorflow - 如何在 tensorflow 中将输入图像与掩码相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41749674/

相关文章:

python - Tensorflow Dataset.from_generator 在tensorflow 2.0中是否已弃用?它抛出 tf.py_func 弃用错误

python - 如何在 TensorFlow 回归中指定 2 个或更多输出标签

tensorflow 对称矩阵

python - Tensorflow:是否可以修改检查点中的全局步骤

tensorflow - 在 Tensorflow 中导出推理图时出现“解析输入...形状不完整”错误

python - 将目录中的图像作为 Tensorflow 数据集加载

tensorflow - keras 模型中的平均权重

python - 无法正确展开 numpy 数组

machine-learning - 如何使用 tf.reset_default_graph()

python - 如何解释和转换 Keras 分类器的预测值?