matplotlib.pyplot.imread读取图片格式图片形状错误

读取图片格式有多个代码库,比如opencv-PIL-matplotlib-Skimage-Pytorch。
PIL读取图片函数是open,其他基本是imread方法。
PIL读取图片的格式是image,其他基本是numpy的array数组。
具体可以参考 https://www.jianshu.com/p/dd08418c306f
但还有一个不同点,matplotlib读取的数据会有一些问题,比如我这里遇到的是:

"Sizes of input arguments do not match"

代码如下:
用cv读取照片

import cv2
filepath = "1.jpg"

image = cv2.imread(filepath)
image.shape
>>> (1920, 2160, 3)

用matplotlib读取照片:

import matplotlib.image as Img
filepath = "1.jpg"
image2 = Img.imread(filepath)
image2.shape
>>> (1920, 2160)

查看了图片格式,是正常的jpg,RGB三通道。
测试了另一个jpg图片,用matplotlib和opencv读出来的图片shape又是一样的。

这里查了[matplotlib官网],查到以下说明:

Matplotlib can only read PNGs natively. Further image formats are supported via the optional dependency on Pillow. Note, URL strings are not compatible with Pillow. Check the Pillow documentation for more information.

测试了一些图片,PIL读取之后图片也只是二维数组,查看图片模式是“L”。
所以为了不产生读取错误,有两种方式:
1、照片读取还是尽量用opencv。opencv读取之后,都需要用cvtColor从BGR转RGB
2、PIL image.open之后,然后把图片用convert(“RGB”)

你可能感兴趣的:(OpenCV,图像视觉)