import matplotlib.pyplot as plt
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_buffer = f.read()
image = tf.image.decode_jpeg(image_buffer)
with tf.Session() as sess:
img = sess.run(image)
print(img)
plt.imshow(img)
这是图片的输出
[[34 34 34]
[34 34 34]
[34 34 34]
...
[41 41 41]
[41 41 41]
[41 41 41]]
[[35 35 35]
[35 35 35]
[35 35 35]
...
[41 41 41]
[41 41 41]
[41 41 41]]]
这里是正常的猫。但是我想resize一下图片
import matplotlib.pyplot as plt
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_buffer = f.read()
image = tf.image.decode_jpeg(image_buffer)
image = tf.image.resize_images(image,(224,224))
with tf.Session() as sess:
img = sess.run(image)
plt.imshow(img)
额,没有看出,就是白白的,但是这个白不是普通的白,而是其中含有像素的。还有一句话
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
我要看看输出的数据到底是什么
[[34.92859 34.92859 34.92859 ]
[34.92859 34.92859 34.92859 ]
[34.92859 34.92859 34.92859 ]
...
[41.107117 41.178528 41.142822]
[40.964294 41.035706 41. ]
[40.964294 41.035706 41. ]]
[[31.714233 31.714233 31.714233]
[31.714233 31.714233 31.714233]
[31.714233 31.714233 31.714233]
...
[40.989796 40.989796 40.989796]
[41. 41. 41. ]
[41. 41. 41. ]]
[[33.32141 33.32141 33.32141 ]
[33.32141 33.32141 33.32141 ]
[33.32141 33.32141 33.32141 ]
...
[40.97577 40.97577 40.97577 ]
[41. 41. 41. ]
[41. 41. 41. ]]]
这是有数值的啊,不应该是白的啊,我仔细品味了一些那句话
float要在0-1之间,int要在0-255之间
这里的数值是float类型,但是都是大于1的,是不是因为这样才不显示呢,改改
import matplotlib.pyplot as plt
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_buffer = f.read()
image = tf.image.decode_jpeg(image_buffer)
image = tf.image.resize_images(image,(224,224))
with tf.Session() as sess:
img = sess.run(image)
img /= 225 #转到0-1之间
#img = img.astype(np.uint8) #转为整型
plt.imshow(img)
发现可以了,但是问题是从哪里出现的呢?
image = tf.image.decode_jpeg(image_buffer) # 返回的是uint8
image = tf.image.resize_images(image,(224,224)) #返回的是float
我们在resize的时候,返回的是float类型的,并且值是大于1的,错误就是从这里产生的,tensorflow提供了转换函数
image = tf.image.convert_image_dtype(image,tf.float32)
就是将uint8类型的数据转为0-1之间的float类型,本质就是img = img/255.
,但是如果本身就是float的话,这个函数就不工作了
import matplotlib.pyplot as plt
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_buffer = f.read()
image = tf.image.decode_jpeg(image_buffer) # uint8
image = tf.image.convert_image_dtype(image,tf.float32) # 0~1 (float)
image = tf.image.resize_images(image,(224,224))# 0~1 (float)
with tf.Session() as sess:
img = sess.run(image) #正常显示
plt.imshow(img)
改变一下顺序
import matplotlib.pyplot as plt
filename = '1.jpg'
with tf.gfile.FastGFile(filename,'rb') as f:
image_buffer = f.read()
image = tf.image.decode_jpeg(image_buffer) # uint8
image = tf.image.resize_images(image,(224,224))# img >1 (float)
image = tf.image.convert_image_dtype(image,tf.float32) # not work(img >1)
with tf.Session() as sess:
img = sess.run(image) #不显示
plt.imshow(img)