cv2.imread()和Image.open()的区别和联系

1 cv2.imread()

import cv2
im_cv = cv2.imread("img_path")
print(im_cv.shape, type(im_cv))   # (479, 506, 3)  
# 下面是展示图像
cv2.imshow("show", im_cv)
cv2.waitKey()   # 显示窗口显示时间,默认是0,表示无限长
cv2.destroyWindow("show")  # 按任意键,销毁窗口

这里可以看出用cv2读出的数据格式是numpy,读出的数据按照flags=1的默认值进行读取,flags的数值如下,表示读入数据的格式:

cv2.imread()和Image.open()的区别和联系_第1张图片

2 Image.open()

from PIL import Image
import numpy as np

img = Image.open(img_path)
print(img, img.size)  #  (610, 477)
img.show()  # 图片展示

可以发现数据读出时,格式是PIL对象类,大小也只有宽和高,平且这里展示图片可以直接用show展示。

3 区别

Image.open() 它的返回值是PIL类型格式,且读取的数据处于打开状态,可以直接图片展示,但是不能直接读取其中的像素点值。
cv2.imread() 返回值是ndnarry,是真实数据值,可以直接读取其中的像素点值,但是不能直接显示,需要借助cv2.waitKey等展示。

4 联系 可相互转化

img = np.array(img_PIL)
print(type(img))   # 
img = Image.fromarray(img)
print(type(img))   # 

你可能感兴趣的:(数据预处理,opencv,python,计算机视觉)