我是靠谱客的博主 单身龙猫,这篇文章主要介绍盘点踩过的关于cv2 和PIL 图像读取的一些小坑1、首先像素读取顺序不同2、cv2 图像读取方法的参数解释,现在分享给大家,希望可以做个参考。

1、首先像素读取顺序不同

  • PIL 读取图像时的像素顺序是标准的RGB
复制代码
1
2
3
4
5
from PIL import Image img = Image.open("test.jpg") print img.size print img.getpixel((0,0))

输出结果是

复制代码
1
2
3
(533, 800) (217, 229, 225)
  • cv2 读取图像时的像素顺序是标准的BGR
复制代码
1
2
3
4
img = cv2.imread(""test.jpg"") print img.shape print img[0][0]

输出结果是

复制代码
1
2
3
(800, 533, 3) [225 229 217]
  • 若要cv2读取完图像也是RGB格式,则按如下方法
复制代码
1
2
3
4
img = cv2.imread(""test.jpg"")[..., ::-1] print img.shape print img[0][0]

输出结果是

复制代码
1
2
3
(800, 533, 3) [217 229 225]

和用PIL 读取完的一致

2、cv2 图像读取方法的参数解释

首先我们先来看一下这个函数的定义
def imread(filename, flags=None)

  • filename
    参数传入的是图像路径,支持解析的图像格式基本上覆盖全了
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
- Windows bitmaps - *.bmp, *.dib (always supported) - JPEG files - *.jpeg, *.jpg, *.jpe (see the *Note* section) - JPEG 2000 files - *.jp2 (see the *Note* section) - Portable Network Graphics - *.png (see the *Note* section) - WebP - *.webp (see the *Note* section) - Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported) - Sun rasters - *.sr, *.ras (always supported) - TIFF files - *.tiff, *.tif (see the *Note* section) - OpenEXR Image files - *.exr (see the *Note* section) - Radiance HDR - *.hdr, *.pic (always supported) - Raster and Vector geospatial data supported by GDAL (see the *Note* section)
  • flags
    @param flags Flag that can take values of cv::ImreadModes
    Flags指定了所读取图片的颜色类型, 默认值为1
    对应值为 -1 到 4
参数Value
IMREAD_UNCHANGEDIf set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALEIf set, always convert image to the single channel grayscale image.
IMREAD_COLORIf set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTHIf set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLORIf set, the image is read in any possible color format.
IMREAD_LOAD_GDALIf set, use the gdal driver for loading the image.
参数Value
flag=-1时8位深度,原通道
flag=08位深度,1通道
flag=18位深度 ,3通道
flag=2原深度,1通道
flag=3原深度,3通道
flag=48位深度 ,3通道
  • IMREAD_UNCHANGED :不进行转化,比如保存为了16位的图片,读取出来仍然为16位。
  • IMREAD_GRAYSCALE :进行转化为灰度图,比如保存为了16位的图片,读取出来为8位,类型为CV_8UC1。
  • IMREAD_COLOR :进行转化为三通道图像。
  • IMREAD_ANYDEPTH :如果图像深度为16位则读出为16位,32位则读出为32位,其余的转化为8位。
  • IMREAD_ANYCOLOR :
  • IMREAD_LOAD_GDAL :使用GDAL驱动读取文件,GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。

最后

以上就是单身龙猫最近收集整理的关于盘点踩过的关于cv2 和PIL 图像读取的一些小坑1、首先像素读取顺序不同2、cv2 图像读取方法的参数解释的全部内容,更多相关盘点踩过的关于cv2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部