2017-04-13 87 views
0

我是tensorflow的新手。我正在从文件中读取图像,并使用tf.image.decode_jpeg对其进行解码,然后使用matplotlib绘制解码图像。但不知何故原始和解码图像是不同的。Tensorflow没有正确解码图像

This is original Image

This is decoded image plotted with matplotlib

filenames = ['/Users/darshak/TensorFlow/100.jpg', '/Users/darshak/TensorFlow/10.jpg'] 
filename_queue = tf.train.string_input_producer(filenames) 

reader = tf.WholeFileReader() 
filename, content = reader.read(filename_queue) 

image = tf.image.decode_jpeg(content, channels=3) 

image = tf.cast(image, tf.float32) 

resized_image = tf.image.resize_images(image, [256, 256]) 

image_batch = tf.train.batch([resized_image], batch_size=9) 

sess = tf.InteractiveSession() 

coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(sess=sess, coord=coord) 

plt.imshow(image.eval()) 
plt.show() 
sess.close() 
+0

你能告诉我们你使用绘制代码图片? – Suever

+1

没有代码,我最好的猜测是你使用OpenCV来加载图像,它们将它们加载为BGR并将它们加载matplotlib,将它们绘制为RGB,从而产生问题。 –

回答

1

的问题的产生是因为plt.imshow(image.eval())取决于image元素类型解释该图像数据不同。

  • 如果imagetf.uint8张量(即,因为它是由tf.image.decode_jpeg()生产)将包含从0255用于R,G和B通道,以及plt.imshow()解释(0, 0, 0)为黑色和(255, 255, 255)为白色。

  • 当你施放image是一个tf.float32张量,它将包含数值从0.0255.0用于R,G和B通道,以及plt.imshow()解释(0.0, 0.0, 0.0)为黑色,但它解释(1.0, 1.0, 1.0)为白色。所有大于1.0的值均与1.0相同,结果图像显示变色。

如果打算来表示图像的tf.float32张量和可视化它,你应该255.0划分图像值:

image = tf.image.decode_jpeg(content, channels=3) 
image = tf.cast(image, tf.float32)/255.0 
+0

非常感谢。 –

+0

'tf.image.decode_jpeg'使用哪种算法,即dct方法来生成图像。根据文档,dct方法'默认为“。字符串,用于指定用于解压缩的算法提示。默认为“”,映射到系统特定的默认值。当前有效值为[“INTEGER_FAST”,“INTEGER_ACCURATE”]。我如何知道我的系统是使用INTEGER_ACCURATE还是INTEGER_FAST? – dpk