PIL.Image.open、matplotlib.pyplot、skimage.imread 读取的图像矩阵通道顺序为RGB
cv2.imread、cv2.VideoCapture 读取的图像矩阵通道顺序为BGR
从BGR到RGB:imgRGB = imgBGR[:,:,::-1]
这句话的意思是对第三个维度(color维度。前两个维度分别为高、宽中的一个)进行反序操作,把BRG的color顺序反序为RGB
PIL对比cv2
区别:
cv2:(高,宽,通道数)。最稳定,推荐用cv2去判断宽、高
plt:(高,宽,通道数),有时候可能会变成(高、宽)
PIL:(宽,高),有时候可能会变成(高、宽)
from PIL import Image
import os
import cv2
import matplotlib.pyplot as pltpath = '/data1//tmp' # image path
imgs = [os.path.join(path,i) for i in os.listdir(path)]num = 0
for i in imgs:im = cv2.imread(i) # bgrh,w,c = im.shapeim3 = plt.imread(i)h3,w3,c3 = im3.shapeim2 = Image.open(i)w2,h2 = im2.sizert,fname = os.path.split(i)print('\n' + fname)print('cv2 shape(h,w)',im.shape)print('plt shape(h,w)',im3.shape)print('Image shape(w,h)', im2.size)print('cv2 width',w,'height',h)print('plt width',w3,'height',h3)print('Image width',w2,'height',h2)
输出:
BB21060018S0000_m002_w3648_h2736.jpgcv2 shape(h,w) (2736, 3648, 3)
plt shape(h,w) (2736, 3648, 3)
Image shape(w,h) (3648, 2736)cv2 width 3648 height 2736
plt width 3648 height 2736
Image width 3648 height 2736
原图:1920x1080
Image.open(imgPath)
:(1920, 1080)
cv2.imread(imgPath,0)
:(1080, 1920)
plt对比cv2
OpenCV读进来的图像,通道顺序为BGR, 而matplotlib的顺序为RGB
# cv2图片格式转为RGB显示
plt.imshow(img[:,:,::-1])
通过通道转换,僵尸变正常人
https://www.cnblogs.com/Alliswell-WP/p/10587500.html