opencv、matplotlib、pillow和pytorch读取数据的通道顺序

1 opencv读取数据的通道顺序

1.1 opencv读取数据相关说明

  • opencv默认读取的颜色通道顺序是:BGR
  • opencv读取的数据类型numpy数组,是uint8的整型数据,范围为0-255
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import torch
from torchvision import datasets, models, transforms


#1 opencv读取数据的通道顺序  默认读取的颜色通道是BGR  数据通道顺序是 hwc
def opencv_channel(img_path, show_mode=1):
    image = cv2.imread(img_path)
    print(f"image type: {type(image)}, image shape: {image.shape}")  # image shape: (305, 500, 3)   h w c
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)
    print(f"image type: {image.dtype}")  # image type: uint8
    print(f"min value: {np.min(image)}, max value: {np.max(image)}")  # min value: 0, max value: 255

    if show_mode == 1:
        # 如果用matplotlib显示opencv读取的图片,图片会发蓝,这是因为opencv读取的颜色通道顺序是BGR,而matplotlib读取的颜色通道顺序是RGB
        plt.imshow(image)
        plt.title("opencv BGR")
        plt.show()
    elif show_mode == 2:
        cv2.imshow("image", image)
        cv2.waitKey(0)

    # 使用plt正确显示opencv读取的数据,需要改变颜色通道顺序  BGR2RGB
    # 下面三种方法都可以 把opencv读取的BGR颜色通道顺序 更改为 RGB颜色通道顺序
    # 方法一:
    cvColor_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # 方法二:
    b, g, r = cv2.split(image)
    cvColor_image2 = cv2.merge([r, g, b])

    # 方法三:
    cvColor_image3 = image[:, :, :: -1]

    if show_mode == 1:
        plt.imshow(cvColor_image)
        plt.title("BGR2RGB")
        plt.show()
    elif show_mode == 2:
        cv2.imshow("image", cvColor_image)
        cv2.waitKey(0)


if __name__ == '__main__':
    img_path = "./bee.jpg"
    opencv_channel(img_path)

1.2 显示opencv读取的数据

1、使用cv2.imshow()

这种显示的是正常的

2、使用plt.imshow()显示opencv读取数据

你会发现显示如下,这是因为plt默认显示的颜色通道顺序为RGB,因此我们需要把opencv读取的数据从BGR转换为RGB

在这里插入图片描述

1.3 把opencv读取的BGR转换RGB的三种方式

1、方法一:

cvColor_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

2、方法二:

b, g, r = cv2.split(image)
cvColor_image2 = cv2.merge([r, g, b])

3、方法三:

cvColor_image3 = image[:, :, :: -1]

BGR转换为RGB之后,再用plt.imshow()进行显示,可以发现颜色已经正常了!

在这里插入图片描述

2 matplotlib读取数据的通道顺序

2.1 matplotlib读取数据相关说明

  • plt.imread()默认读取的颜色通道顺序是:RGB
  • plt.imread()读取的数据类型numpy数组,是uint8的整型数据,范围为0-255
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import torch
from torchvision import datasets, models, transforms


#2 matplotlib读取数据的通道顺序  默认读取的颜色通道是RGB  数据通道顺序是 hwc
def plt_channel(img_path):
    image = plt.imread(img_path)
    print(f"image type: {type(image)}, image shape: {image.shape}")
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)   h w c
    plt.imshow(image)
    plt.title("plt image")
    plt.show()

    # 可以把numpy数据转换为pillow数据
    pil_image = Image.fromarray(image)
    plt.imshow(pil_image)
    plt.title("numpy convert to pillow type")
    plt.show()

if __name__ == '__main__':
    img_path = "./bee.jpg"
    plt_channel(img_path)

显示结果如下:

在这里插入图片描述

2.2 把numpy数组类型转换为pillow类型

也可以把plt读取的numpy数组类型转化为pillow类型

pil_image = Image.fromarray(image)

在这里插入图片描述

3 pillow读取数据的通道顺序

3.1 pillow读取数据相关说明

  • pillow默认读取的颜色通道顺序是:RGB
  • pillow自己的数据结构的,但是可以转换成numpy数组,转换后的数组为unit8,0-255
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import torch
from torchvision import datasets, models, transforms


#3 pillow读取数据的通道顺序
def pillow_channel(img_path, show_mode=1):
    image = Image.open(img_path)
    print(f"image mode: {image.mode}")  # image mode: RGB
    print(f"image type: {type(image)}, image shape: {image.size}")
    # image type: <class 'PIL.JpegImagePlugin.JpegImageFile'>, image shape: (500, 305)   w, h

    if show_mode == 1:
        plt.imshow(image)
        plt.title("pillow image")
        plt.show()
    elif show_mode == 2:
        image.show()


    # 把pillow数据转换为numpy数据
    np_image = np.array(image)
    print(f"image type: {type(np_image)}, image shape: {np_image.shape}")
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)   h w c
    plt.imshow(np_image)
    plt.title("pillow convert to numpy type")
    plt.show()


if __name__ == '__main__':
    img_path = "./bee.jpg"
    pillow_channel(img_path)

因为,pillow默认读取图片的颜色通道也是RGB,因此用plt显示的时候是没有问题的!显示结果如下:

在这里插入图片描述

3.2 把pillow类型转换为numpy类型

把pillow类型的数据转换为numpy数组类型数据:

np_image = np.array(image)

在这里插入图片描述

4 pytorch读取数据的通道顺序

  • pytorch 读取数据类型为tensor
  • pytorch读取数据类型的通道顺序为:NCHW
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import torch
from torchvision import datasets, models, transforms


#4 pytorch读取数据的通道顺序
def torch_channel(imgs_dir):
    # 1、数据增强
    train_data_transforms = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
    ])

    # 2、从目录中读取数据集
    # 存放数据的目录
    root_data_dir = imgs_dir

    train_datasets = datasets.ImageFolder(root_data_dir, train_data_transforms)

    # 3、加载数据增强后的数据
    train_dataloaders = torch.utils.data.DataLoader(train_datasets,
                                                    batch_size=2,
                                                    shuffle=True,
                                                    num_workers=0)

    # 遍历读取数据
    for inputs, labels in train_dataloaders:
        print(f"inputs shape: {inputs.shape}")
        print(f"labels shape: {labels.shape}")

        '''
        # 输出结果:
        inputs shape: torch.Size([2, 3, 224, 224])  bs, c, h, w  即:NCHW, tensorflow读取的顺序为NHWC
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        
        '''


        # 可视化其中的图片,batch_size=2, 因此每个batch中有存储两张图片的数据
        # 先把tensor类型转换为numpy类型
        np_inputs = inputs.numpy()
        print(f"np_inputs type: {type(np_inputs)}, np_inputs shape: {np_inputs.shape}")
        # np_inputs type: <class 'numpy.ndarray'>, np_inputs shape: (2, 3, 224, 224)

        # 更改图片的数据的通道顺序, NCHW 改为 NHWC   0123   0231
        np_change_channel = np_inputs.transpose(0, 2, 3, 1 )
        print(f"np_change_channel type: {type(np_change_channel)}, np_change_channel shape: {np_change_channel.shape}")
        # np_change_channel type: <class 'numpy.ndarray'>, np_change_channel shape: (2, 224, 224, 3)

        # 显示图片,这里把每个batch中的两张图片放到一起显示
        out_image = np.hstack((np_change_channel[0], np_change_channel[1]))

        # # 如果用opencv显示需要再在转换一下颜色空间,转换为BGR,因为torchvision内部是基于Pillow实现的,默认是RGB颜色通道
        # out_image = cv2.cvtColor(out_image, cv2.COLOR_RGB2BGR)
        # cv2.imshow("image", out_image)
        # cv2.waitKey(0)


        plt.imshow(out_image)
        plt.title("pytorch tensor convert to numpy data")
        plt.show()


if __name__ == '__main__':
    imgs_dir = './hymenoptera/train'
    torch_channel(imgs_dir)

在这里插入图片描述

在这里插入图片描述

5 全部完整代码

完整代码如下:

'''
比较 opencv、matplotlib、pillow 和 pytorch读取数据的通道顺序
'''

__Author__ = "Shliang"
__Email__ = "shliang0603@gmail.com"


import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import torch
from torchvision import datasets, models, transforms


#1 opencv读取数据的通道顺序  默认读取的颜色通道是BGR  数据通道顺序是 hwc
def opencv_channel(img_path, show_mode=1):
    image = cv2.imread(img_path)
    print(f"image type: {type(image)}, image shape: {image.shape}")  # image shape: (305, 500, 3)   h w c
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)
    print(f"image type: {image.dtype}")  # image type: uint8
    print(f"min value: {np.min(image)}, max value: {np.max(image)}")  # min value: 0, max value: 255

    if show_mode == 1:
        # 如果用matplotlib显示opencv读取的图片,图片会发蓝,这是因为opencv读取的颜色通道顺序是BGR,而matplotlib读取的颜色通道顺序是RGB
        plt.imshow(image)
        plt.title("opencv BGR")
        plt.show()
    elif show_mode == 2:
        cv2.imshow("image", image)
        cv2.waitKey(0)

    # 使用plt正确显示opencv读取的数据,需要改变颜色通道顺序  BGR2RGB
    # 下面三种方法都可以 把opencv读取的BGR颜色通道顺序 更改为 RGB颜色通道顺序
    # 方法一:
    cvColor_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # 方法二:
    b, g, r = cv2.split(image)
    cvColor_image2 = cv2.merge([r, g, b])

    # 方法三:
    cvColor_image3 = image[:, :, :: -1]

    if show_mode == 1:
        plt.imshow(cvColor_image)
        plt.title("BGR2RGB")
        plt.show()
    elif show_mode == 2:
        cv2.imshow("image", cvColor_image)
        cv2.waitKey(0)

#2 matplotlib读取数据的通道顺序  默认读取的颜色通道是RGB  数据通道顺序是 hwc
def plt_channel(img_path):
    image = plt.imread(img_path)
    print(f"image type: {type(image)}, image shape: {image.shape}")
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)   h w c
    plt.imshow(image)
    plt.title("plt image")
    plt.show()

    # 可以把numpy数据转换为pillow数据
    pil_image = Image.fromarray(image)
    plt.imshow(pil_image)
    plt.title("numpy convert to pillow type")
    plt.show()




#3 pillow读取数据的通道顺序
def pillow_channel(img_path, show_mode=1):
    image = Image.open(img_path)
    print(f"image mode: {image.mode}")  # image mode: RGB
    print(f"image type: {type(image)}, image shape: {image.size}")
    # image type: <class 'PIL.JpegImagePlugin.JpegImageFile'>, image shape: (500, 305)   w, h

    if show_mode == 1:
        plt.imshow(image)
        plt.title("pillow image")
        plt.show()
    elif show_mode == 2:
        image.show()


    # 把pillow数据转换为numpy数据
    np_image = np.array(image)
    print(f"image type: {type(np_image)}, image shape: {np_image.shape}")
    # image type: <class 'numpy.ndarray'>, image shape: (305, 500, 3)   h w c
    plt.imshow(np_image)
    plt.title("pillow convert to numpy type")
    plt.show()




#4 pytorch读取数据的通道顺序
def torch_channel(imgs_dir):
    # 1、数据增强
    train_data_transforms = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
    ])

    # 2、从目录中读取数据集
    # 存放数据的目录
    root_data_dir = imgs_dir

    train_datasets = datasets.ImageFolder(root_data_dir, train_data_transforms)

    # 3、加载数据增强后的数据
    train_dataloaders = torch.utils.data.DataLoader(train_datasets,
                                                    batch_size=2,
                                                    shuffle=True,
                                                    num_workers=0)

    # 遍历读取数据
    for inputs, labels in train_dataloaders:
        print(f"inputs shape: {inputs.shape}")
        print(f"labels shape: {labels.shape}")

        '''
        # 输出结果:
        inputs shape: torch.Size([2, 3, 224, 224])  bs, c, h, w  即:NCHW, tensorflow读取的顺序为NHWC
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        inputs shape: torch.Size([2, 3, 224, 224])
        labels shape: torch.Size([2])
        
        '''


        # 可视化其中的图片,batch_size=2, 因此每个batch中有存储两张图片的数据
        # 先把tensor类型转换为numpy类型
        np_inputs = inputs.numpy()
        print(f"np_inputs type: {type(np_inputs)}, np_inputs shape: {np_inputs.shape}")
        # np_inputs type: <class 'numpy.ndarray'>, np_inputs shape: (2, 3, 224, 224)

        # 更改图片的数据的通道顺序, NCHW 改为 NHWC   0123   0231
        np_change_channel = np_inputs.transpose(0, 2, 3, 1 )
        print(f"np_change_channel type: {type(np_change_channel)}, np_change_channel shape: {np_change_channel.shape}")
        # np_change_channel type: <class 'numpy.ndarray'>, np_change_channel shape: (2, 224, 224, 3)

        # 显示图片,这里把每个batch中的两张图片放到一起显示
        out_image = np.hstack((np_change_channel[0], np_change_channel[1]))

        # # 如果用opencv显示需要再在转换一下颜色空间,转换为BGR,因为torchvision内部是基于Pillow实现的,默认是RGB颜色通道
        # out_image = cv2.cvtColor(out_image, cv2.COLOR_RGB2BGR)
        # cv2.imshow("image", out_image)
        # cv2.waitKey(0)


        plt.imshow(out_image)
        plt.title("pytorch tensor convert to numpy data")
        plt.show()



if __name__ == '__main__':
    img_path = "./bee.jpg"
    opencv_channel(img_path)
    plt_channel(img_path)
    pillow_channel(img_path)

    imgs_dir = './hymenoptera/train'
    torch_channel(imgs_dir)

参考:https://www.cnblogs.com/ranjiewen/p/10278234.html
参考:https://blog.csdn.net/cxx654/article/details/104237214 # 还有imagei和scipy
参考:https://blog.csdn.net/qq_36941368/article/details/82998296
参考:https://blog.csdn.net/oLingFengYu/article/details/88033668 # 不同框架通道顺序


创作不易,观众老爷们请留步… 动起可爱的小手,点个赞再走呗 (๑◕ܫ←๑)
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页