脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 快速解决cv2.imread()读取图像为BGR的问题

快速解决cv2.imread()读取图像为BGR的问题

2021-09-24 00:03阿达西家 Python

这篇文章主要介绍了快速解决cv2.imread()读取图像为BGR的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

opencv读取图像为b,g,r方法,比如

  1. img = cv2.imread("xx.jpg")
  2. cv2.imshow("xx",img)

展示的结果是正常的:

快速解决cv2.imread()读取图像为BGR的问题

但是此时读取到的img已经为bgr方式了,如果我们再用其他使用rgb方式读取的函数进行读取时就会出错,比如我用plt对图像进行显示,效果如下:

快速解决cv2.imread()读取图像为BGR的问题

因为plt函数是rgb方式读取的,所以会出错。这时我们可以手动改变img的通道顺序,如下:

  1. b,g,r = cv2.split(img)
  2. img_rgb = cv2.merge([r,g,b])
  3. plt.figure()
  4. plt.imshow(img_rgb)
  5. plt.show()

这时img_rgb就是rgb顺序的了.那么这时再用cv2.imshow()显示出来,rgb错误:

快速解决cv2.imread()读取图像为BGR的问题

补充:盘点踩过的关于cv2 和PIL 图像读取的一些小坑

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

PIL 读取图像时的像素顺序是标准的RGB

  1. from PIL import Image
  2. img = Image.open("test.jpg")
  3. print img.size
  4. print img.getpixel((0,0))

输出结果是

  1. (533, 800)
  2. (217, 229, 225)

cv2 读取图像时的像素顺序是标准的BGR

  1. img = cv2.imread(""test.jpg"")
  2. print img.shape
  3. print img[0][0]

输出结果是

  1. (800, 533, 3)
  2. [225 229 217]

若要cv2读取完图像也是RGB格式,则按如下方法

  1. img = cv2.imread(""test.jpg"")[..., ::-1]
  2. print img.shape
  3. print img[0][0]

输出结果是

  1. (800, 533, 3)
  2. [217 229 225]

和用PIL 读取完的一致

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

首先我们先来看一下这个函数的定义

  1. def imread(filename, flags=None)

filename

参数传入的是图像路径,支持解析的图像格式基本上覆盖全了

  1. - Windows bitmaps - \*.bmp, \*.dib (always supported)
  2. - JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Note* section)
  3. - JPEG 2000 files - \*.jp2 (see the *Note* section)
  4. - Portable Network Graphics - \*.png (see the *Note* section)
  5. - WebP - \*.webp (see the *Note* section)
  6. - Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported)
  7. - Sun rasters - \*.sr, \*.ras (always supported)
  8. - TIFF files - \*.tiff, \*.tif (see the *Note* section)
  9. - OpenEXR Image files - \*.exr (see the *Note* section)
  10. - Radiance HDR - \*.hdr, \*.pic (always supported)
  11. - Raster and Vector geospatial data supported by GDAL (see the *Note* section)

flags

  1. @param flags Flag that can take values of cv::ImreadModes

Flags指定了所读取图片的颜色类型, 默认值为1

对应值为 -1 到 4

参数 Value
IMREAD_UNCHANGED If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE If set, always convert image to the single channel grayscale image.
IMREAD_COLOR If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL If set, use the gdal driver for loading the image.
参数 Value
flag=-1时 8位深度,原通道
flag=0 8位深度,1通道
flag=1 8位深度 ,3通道
flag=2 原深度,1通道
flag=3 原深度,3通道
flag=4 8位深度 ,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许可协议下的开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/liu13364876993/article/details/79867061

延伸 · 阅读

精彩推荐