python - Tensorflow 查找具有匹配值的像素

标签 python numpy tensorflow machine-learning binary-image

我正在尝试创建数据批处理来训练 2 类语义分割网络。目标分割图像有 2 层,第一层为 1 类所有像素,否则为 0。第二层像素反转。

在数据集中,我的输出图像是带有 [255,255,255][0,0,0] 的 3 channel RGB 图像。输入和输出图像存储在tf-record文件中。

当我在 numpy 中进行实验时,我使用以下代码创建了一个 2 channel 二进制图像:

c1_pix = np.all(op_img == np.array([255,255,255]), axis=2)
c1_pix = c1_pix.reshape(*(h,w), 1)
op_arr = np.concatenate((c1_pix, np.invert(c1_pix)), axis=2)

这给了我一个我想要的 2 层 1 和 0 图像。

我正在尝试在 tensorflow 中重复它,但我对此很陌生。我尝试了c1_pix = tf.where(tf.equal(op_img, [[[255,255,255]]]))。它似乎有效,但它返回 1 和 0 的 3 channel int64 张量,我无法反转它。

有人可以帮我解决这个问题吗?

谢谢

最佳答案

在查看了 tf 文档一段时间后,我想出了以下解决方案

c1_pix, _, _ = tf.split(tf.equal(op_img,[[[255,255,255]]]), 3, axis=-1)
c2_pix = tf.logical_not(c1_pix)
new_op_img = tf.concat([c1_pix, c2_pix], -1)

这对我有用。

关于python - Tensorflow 查找具有匹配值的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52705471/

相关文章:

python - Conda安装的包,Python找不到

python - Python 中 string.replace() 的意外行为

python - 如何对多张图像进行傅里叶变换并将输出保存到单个对象中

python-3.x - 使用 Tensorflow 训练模型的输入数组形状应该是什么

tensorflow - 如何使用Keras中的tile功能?

python - 从 tensorflow 链接加载 tarball 中的图像以训练 CNN

转换大型 PDF 时,Python/wand 代码会导致 "Killed"

python - pip3 安装 ibm_db 在 docker 镜像内失败

python - scikit learn 插补 NaN 以外的值

python - FFT 围绕特定频率的大量细节