Matplotlib中显示图像
对于显示image的数据范围不敏感
可以是[0,255]也可以是[0, 1.0]
但是需要指定cmap
##########################################
## float ##
# img = np.random.rand(300, 400)
# img = np.random.rand(300, 400)*255
## int ##
img=np.random.randint(0, 256, (300, 400))
##########################################
## show ##
plt.imshow(img, cmap="gray")
# plt.imshow(img, cmap=plt.cm.gray)
# plt.imshow(img, cmap=plt.get_cmap("tab20"))
plt.axis('off') # 不显示坐标轴
plt.show()
PIL中图像显示方式
摘自《python计算机视觉编程》
其中Image.open()函数返回的是PngImageFile类型的对象;
导入pylab相当于导入:
import numpy as np
import matplotlib.pyplot as plt
需要数据范围是[0, 255], 而[0, 1.0]对数据显示没用
##########################################
## float ##
# img = np.random.rand(300, 400)
# img = np.random.rand(300, 400)*255
## int ##
img=np.random.randint(0, 256, (300, 400))
##########################################
Img = Image.fromarray(img)
Img.show()
opencv2中图像显示方式
摘自《OpenCV 3计算机视觉 python语言实现(原书第二版)》
cv2.imread()函数返回的是numpy中ndarray类型的数据
需要数据是float的[0, 1.0]
##########################################
## float ##
img = np.random.rand(300, 400)
# img = np.random.rand(300, 400)*255
## int ##
# img=np.random.randint(0, 256, (300, 400))
##########################################
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
scikit-image图像显示
>>> import os
>>> filename = os.path.join(skimage.data_dir, 'moon.png')
>>> from skimage import io
>>> moon = io.imread(filename)
>>> import matplotlia.pyplot as plt
>>> plt.imshow(moon)
or
>>> skimage.io.imshow(moon)
另一种skimage.viewer显示
from skimage import data
from skimage.viewer import ImageViewer
image = data.coins()
viewer = ImageViewer(image)
viewer.show()
数据范围[0, 1.0]或者[0, 255]都可以
float的[0, 1.0]显示灰度图
float的[0, 255]和int的[0, 255]显示索引图,即需要cmap
##########################################
## float ##
# img = np.random.rand(300, 400)
# img = np.random.rand(300, 400)*255
## int ##
img=np.random.randint(0, 256, (300, 400))
##########################################
io.imshow(img)
plt.show()