1.读入的数据类型不同
PIL读入图像,以jepg为例,类型为PIL.JpegImagePlugin.JpegImageFile;
Opencv读入图像,直接就是numpy.ndarray。
2.宽、高顺序不同
PIL读入图像,顺序为:(宽,高),使用np.array()转换类型后,顺序为:(高,宽,通道数)
Opencv读入图像,顺序为:(高,宽,通道数)
3.通道顺序不同
PIL读入为R、G、B;
Opencv读入为B、G、R
4.代码验证对比
以读入该图像为例
from PIL import Image
import cv2
import numpy as np
print('-----PIL读取-----')
pil_img = Image.open('Dog.jpeg')
print('type(pil_img):', type(pil_img))
print('pil_img.size:', pil_img.size) # (宽,高)
pil_img_arr = np.array(pil_img)
print('pil_img_arr.shape:', pil_img_arr.shape) # (高,宽,通道数)
print('-----opencv读取-----')
opc_img = cv2.imread('Dog.jpeg')
print('type(opc_img):', type(opc_img))
print('opc_img.size:', opc_img.size)
print('opc_img.shape:', opc_img.shape) # (高,宽)
print('-----通道顺序对比-----')
p_r = pil_img_arr[:,:,0]
p_g = pil_img_arr[:,:,1]
p_b = pil_img_arr[:,:,2]
o_b = opc_img[:,:,0]
o_g = opc_img[:,:,1]
o_r = opc_img[:,:,2]
print('------')
运行结果:
-----PIL读取-----
type(pil_img): <class 'PIL.JpegImagePlugin.JpegImageFile'>
pil_img.size: (1000, 562)
pil_img_arr.shape: (562, 1000, 3)
-----opencv读取-----
type(opc_img): <class 'numpy.ndarray'>
opc_img.size: 1686000
opc_img.shape: (562, 1000, 3)
-----通道顺序对比-----
------
Process finished with exit code 0
各通道对比:
参考链接: CV2和PIL读取图像方法与区别对比