在Tensorflow中将RGBA图像转换为黑白图像

时间:2016-03-07 04:46:11

标签: tensorflow

tf.image.decode_png()可以输出灰度,RGB和RGBA图像。

但我想在 Tensorflow (不使用枕头)中将RGBA转换为纯黑色和白色。

请给我一些建议。

1 个答案:

答案 0 :(得分:5)

使用tf.select进行阈值处理

pil_image = PilImage.open('/temp/panda.png')
show_pil_image(pil_image)

enter image description here

pil_buf = open('/temp/panda.png').read()
contents = tf.placeholder(dtype=tf.string)
decode_op = tf.image.decode_png(contents, channels=1)
gray_image = tf.squeeze(decode_op) # shape (127,127,1) -> shape (127,127)
sess = create_session()
[decoded] = sess.run([gray_image], feed_dict={contents: pil_buf})
show_pil_image(PilImage.fromarray(decoded))

enter image description here

contents = tf.placeholder(dtype=tf.string)
decode_op = tf.image.decode_png(contents, channels=1)
gray_image = tf.squeeze(decode_op) # shape (127,127,1) -> shape (127,127)
select_op = tf.select(gray_image>127, 255*tf.ones_like(gray_image), tf.zeros_like(gray_image))
sess = create_session()
[decoded] = sess.run([select_op], feed_dict={contents: pil_buf})
show_pil_image(PilImage.fromarray(decoded))

enter image description here