
cv2,PIL,plt,tensorflow方法图片显示及plt的的plt.colorbar()的使用需要注意
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow
·
几种图像读取方式总结
import matplotlib.image as img :img.imread()
import PIL.Image as Img :Img.open()
import tensorflow as tf :tf.read_file()
import cv2:cv2.imread()
需要注意:一、对于低版本的tf,需要将图片经过tf.Session()执行
二、cv2读取文件中不能有中文字符
三、cv2读取三通道和单通道的不同
四、cv2的取的图片文件使用cv.imshow()显示时,需要注意图像的像素值范围必须映射到0-255
五、plt.colorbar()必须在plt.imshow()之后
需要将plt.colorbar()放在要显示图像命令后边
否则出现,找不到映射关系的错误
RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a c
使用PIL和matplotlib.image两种方式读取
import matplotlib.image as img
# import PIL.Image as Img
import matplotlib.pyplot as plt
# img=Img.open(r'F:\共享文件夹\论文编辑\简化FCN\annotations\annotations\trimaps\Abyssinian_1.png')
img=img.imread(r'F:\共享文件夹\论文编辑\简化FCN\annotations\annotations\trimaps\Abyssinian_1.png')
# plt.colorbar()
# print(set(img.flatten())) # {0.007843138, 0.011764706, 0.003921569} 和{1,2,3}
plt.imshow(img)
plt.colorbar()
plt.show()
从图中看出,两种读取的图片像素值不一样,但显示现象一样,从colorbar看出,色彩范围一样,
计算1/255= 0.003921569,
2/255 = 0.007843138,
3/255 = 0.011764706
发现,有255对应关系,在不同模块读取图片后会对图片进行处理


使用tensoflow读取
import tensorflow as tf
import matplotlib.pyplot as plt
img = tf.read_file(r'F:\共享文件夹\论文编辑\简化FCN\annotations\annotations\trimaps\Abyssinian_1.png')
with tf.Session() as sess:
img= sess.run(tf.image.decode_jpeg(img))
print(set(img.flatten())) #{1, 2, 3} #
print(img.shape)
'''
(400, 600, 1)
'''
plt.imshow(img)
plt.show()
使用opencv的cv2.imread()和plt显示
参考:https://blog.csdn.net/qq_24815615/article/details/107885393
import cv2
import matplotlib.pyplot as plt
import os
os.chdir(r'F:\共享文件夹\论文编辑\简化FCN\annotations\annotations\trimaps',0) #这里必须设置0参数,表示单通道
img=cv2.imread(r'Abyssinian_1.png') # 像素值是{1,2,3}
plt.imshow(img)
plt.colorbar()
plt.show() # 一定使用

cv2.imread()和cv2.imshow()
参考:https://blog.csdn.net/weixin_39190382/article/details/113615763
cv2.imread()不能读取中文路径,
必须设置参数是读取单通道图像,
需要将图像像素值映射到0-255
import cv2
import matplotlib.pyplot as plt
import os
import numpy as np
def image_normalization(img, img_min=0, img_max=255): # 像素映射
img = np.float32(img)
epsilon = 1e-12
img = (img - np.min(img)) * (img_max - img_min) / ((np.max(img) - np.min(img)) + epsilon) + img_min
return img
os.chdir(r'F:\共享文件夹\论文编辑\简化FCN\annotations\annotations\trimaps')
img=cv2.imread(r'Abyssinian_1.png',0) # 不能访问中文路径
img = image_normalization(img)
# cv2.imshow('single_cannel',(img-1)*255.0/2.0),也可以使用,将1-3,映射到0-255
cv2.imshow('single_cannel',img)
cv2.waitKey(0)

一个面向所有人的开源机器学习框架
最近提交(Master分支:1 个月前 )
4f64a3d5
Instead, check for this case in `ResolveUsers` and `ResolveOperand`, by querying whether the `fused_expression_root` is part of the `HloFusionAdaptor`.
This prevents us from stepping into nested fusions.
PiperOrigin-RevId: 724311958
1 个月前
aa7e952e
Fix a bug in handling negative strides, and add a test case that exposes it.
We can have negative strides that are not just -1, e.g. with a combining
reshape.
PiperOrigin-RevId: 724293790
1 个月前
更多推荐




所有评论(0)