简体   繁体   English

如何在张量流中反转黑白图像(或灰度图像)? (黑色<->白色变化)

[英]How can I invert BLACK and WHITE (or grayscale) images in tensorflow? (black <->white change)

I want to change some pictures which is consist of black and white elements only (MNIST). 我想更改一些仅由黑白元素(MNIST)组成的图片。

I can easily convert black to white, white to black by these code in python element 我可以通过python元素中的这些代码轻松地将黑色转换为白色,将白色转换为黑色

def inverseImageBW_array(originalImage):
    temp = 1 - originalImage
    temp = -1.* originalImage
    return temp

however, I cannot do this when I am treating tensorflow code. 但是,当我处理张量流代码时,我无法做到这一点。 I tried to do this by 我试图这样做

tf.multiply(1- originalImage, -1)

buy it does not work. 购买它不起作用。

I know we can easily change brightness or contrast by tf.image functions but I think there is no functions which support black and white invert. 我知道我们可以通过tf.image函数轻松更改亮度或对比度,但是我认为没有支持黑白反转的函数。

Please help me. 请帮我。

Not sure why something does not work for you. 不知道为什么有些东西对您不起作用。 If your image is a 2d tensor or 3d tensor with a batch, then broadcasting should take care of everything: 如果您的图像是带有批次的2d张量或3d张量,则广播应处理所有事情:

import tensorflow as tf

a = tf.constant([[0.5, 0.3], [0.2, 0.8]])
b = 1 - a
with tf.Session() as sess:
    print sess.run(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM