import cv2
from skimage import io
from PIL import Image
from torchvision import transforms
to_tensor = transforms.ToTensor()
# 灰度图像
cv_img = cv2.imread(r'test_model\data\test\img\MCUCXR_0301_1.png')
print('----------------------cv2-------------------------')
print(type(cv_img)) # numpy.ndarray
print(cv_img.shape) # H W C # 默认为rgb图像(3通道,其实是BGR)
cv_tensor = to_tensor(cv_img)
print(cv_tensor.shape) # C H W
print(cv_img)
skimage_img = io.imread(r'test_model\data\test\img\MCUCXR_0301_1.png')
print('----------------------skimage---------------------')
print(type(skimage_img)) # numpy.ndarray
print(skimage_img.shape) # H W
skimage_tensor = to_tensor(skimage_img)
print(skimage_tensor.shape) # C H W (对于rgb图像,顺序为RGB)
print(skimage_img)
PIL_img = Image.open(r'test_model\data\test\img\MCUCXR_0301_1.png')
print('----------------------PIL-------------------------')
print(type(PIL_img)) # PIL专有
print(PIL_img.mode) # mode:'RGBA'、'RGB'、'L'
PIL_tensor = to_tensor(PIL_img)
print(PIL_tensor.shape) # C H W
print(PIL_img)
# RGBA彩色图像
cv_img = cv2.imread(r'test_model\data\test\img\1.png')
print('----------------------cv2-------------------------')
print(type(cv_img))
print(cv_img.shape) # H W C # 默认为rgb图像(4通道,其实是BGRA)
cv_tensor = to_tensor(cv_img)
print(cv_tensor.shape) # C H W
print(cv_img)
skimage_img = io.imread(r'test_model\data\test\img\1.png')
print('----------------------skimage---------------------')
print(type(skimage_img))
print(skimage_img.shape) # H W C (对于rgba图像,默认读取RGB,顺序为RGB) 加上限定条件为BGRA
skimage_tensor = to_tensor(skimage_img)
print(skimage_tensor.shape) # C H W
print(skimage_img)
PIL_img = Image.open(r'test_model\data\test\img\1.png')
print('----------------------PIL-------------------------')
print(type(PIL_img))
print(PIL_img.mode) # mode:'RGBA'、'RGB'、'L'
PIL_tensor = to_tensor(PIL_img)
print(PIL_tensor.shape) # C H W
print(PIL_img)
cv2,skimage,PIL读取图像后的类型,高、宽、通道数的顺序,以及RGB顺序
最新推荐文章于 2023-06-23 17:05:44 发布