打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Tensorflow 读取任意图片 tf.image.decode_image(tf.read_file(filename))
# -*- coding: utf-8 -*-
import tensorflow as tf
import cv2

file1='456.png'
file2="234.jpg"

file_contents = tf.read_file(file1)
image = tf.image.decode_image(file_contents)#解码png格式


img2=tf.image.decode_image(tf.read_file(file2))

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

img,img2=sess.run([image,img2])
print (img.shape)
print (img2.shape)

cv2.imshow('321', img)
cv2.imshow('3345', img2)
cv2.waitKey(0)

这里用tf.image.decode_image读取图片,用cv2显示,则显示不正常:
原因:两种读取,图片的格式是不一样的。一个可能是rgb读取,一个可能是bgr方式的图。

使用plt则可以显示正常
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import tensorflow as tf

file1="234.jpg"
file2='11.jpg'

file_contents = tf.read_file(file1)
image = tf.image.decode_image(file_contents)

img2=tf.image.decode_image(tf.read_file(file2))

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

img, img2 = sess.run([image, img2])

plt.figure(1) # 图像显示
plt.imshow(img)
plt.figure(2)
plt.imshow(img2)
plt.show()

用opencv显示的,请参靠另一个博客。
tf.image.decode_image读取图片,opencv保存图片 Tensorflow读取图片



该函数可以读灰度图,rgb图,以上读取的是rgb图。

下面读取的是rgb图

# -*- coding: utf-8 -*-
import tensorflow as tf
import cv2
import numpy as np

file1="000001.png"

file_contents = tf.read_file(file1)
image = tf.image.decode_image(file_contents)#读取的格式,是bgr格式


with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

img = sess.run(image)

cv2.imshow("img",img)
#opencv显示的格式是BGR
cv2.waitKey(0)

 
 





本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
TensorFlow 第八课 使用inception-v3
教程 | 经得住考验的「假图片」:用TensorFlow为神经网络生成对抗样本
基于Tensorflow和TF-Slim图像分割示例
Tensorflow
TensorFlow基础入门十大操作总结
keras训练的h5模型转换为pb模型
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服