OpenCV学习笔记(六):imread介绍

本文详细介绍了 OpenCV 中 imread 函数的用法及其参数设置。imread 函数可以从文件中加载图像,并根据不同的标志参数返回不同类型的图像数据。文章还列举了支持的图像格式,并解释了如何处理不同颜色模式和深度的图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容来源:http://docs.opencv.org/3.0.0/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56

Mat cv::imread(const String &  filename,
  int  flags = IMREAD_COLOR 
 )  

Loads an image from a file.

Parameters
filenameName of file to be loaded.
flagsFlag that can take values of cv::ImreadModes

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:

翻译:imread函数从文件中加载图像并返回该图像。如果该图像不能被读取(由于文件丢失、权限不正确、不支持或非法的格式等原因),该函数返回一个空的矩阵(Mat中的data项为NULL)。现在已经支持一下的文件格式:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • WebP - *.webp (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)
Note
  • The function determines the type of an image by the content, not by the file extension.
  • (该函数根据文件类容确定文件类型,而不是根据拓展名。)
  • On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
  • (在Windows、Mac平台下,OpenCV默然激活了libjpeg, libpng, libtiff, and libjasper库。所以,OpenCV总是可以读取JPEGs, PNGs, and TIFFs格式的文件。在Mac的操作系统下,也可以使用MacOSX系统自身的图像读取函数。但是由于MacOSX的管理方式,现在这些系统自带的读取方式返回的像素点会有所不同。
  • On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, "libjpeg-dev", in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
  • ()
Note
In the case of color images, the decoded images will have the channels stored in B G R order.
Examples:
demhist.cppdistrans.cppedge.cppffilldemo.cppfitellipse.cppgrabcut.cpphoughcircles.cpphoughlines.cpplsd_lines.cpp, morphology2.cpppca.cpp, and  watershed.cpp.

以上为官方文档对imread函数的说明,这里重点说一下flag参数cv::ImreadModes

Imread flags.

Enumerator
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.

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许可协议下的开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值