函数imread从指定的文件加载图像并返回它。如果无法读取图像(由于文件丢失、权限不正确、不支持或格式无效),函数将返回一个空矩阵(Mat::data==NULL)。
目前支持以下文件格式:

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)
PFM files - *.pfm (see the Note section)
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)

该函数根据内容而不是文件扩展名来确定图像的类型。
在彩色图像的情况下,解码后的图像将以B G R顺序存储通道。
当使用IMREAD_GRAYSCALE时,如果可用,将使用编解码器的内部灰度转换。结果可能不同于cvtColor()的输出
在Microsoft Windows* OS和MacOSX上,默认情况下使用OpenCV映像附带的编解码器(libjpeg、libpng、libtiff和libjasper)。因此,OpenCV总是可以读取jpeg、png和tiff。在MacOSX上,也有使用本地MacOSX图像阅读器的选项。但是要注意,由于MacOSX中嵌入了颜色管理,因此当前这些本地图像加载器提供的图像具有不同的像素值。
在Linux
、BSD口味和其他类似unix的开源操作系统上,OpenCV寻找与操作系统映像一起提供的编解码器。安装相关的包(不要忘记开发文件,例如Debian和Ubuntu中的“libjpeg-dev”)以获得编解码器支持,或者在CMake中打开OPENCV_BUILD_3RDPARTY_LIBS标志。
在CMake和@ref IMREAD_LOAD_GDAL中将WITH_GDAL标志设置为true以加载图像的情况下,将使用GDAL驱动程序来解码图像,支持以下格式:光栅、矢量。
如果EXIF信息嵌入到图像文件中,EXIF方向将被考虑,因此图像将相应地旋转,除非传递了@ref IMREAD_IGNORE_ORIENTATION标志。
使用imread_changed标志来保存PFM图像中的浮点值。

Parameters
filename: Name of file to be loaded.
flags: Flag that can take values of cv::ImreadModes
C++ default parameters
flags: IMREAD_COLOR
Getting OpenCV
Linux
You have several options of getting the OpenCV library:

install it from the repository, make sure to install -dev packages because they contain headers necessary for the crate build (also check that your package contains pkg_config files).

build OpenCV manually and set up the following environment variables prior to building the project with opencv crate:

PKG_CONFIG_PATH for the location of *.pc files
LD_LIBRARY_PATH for where to look for the installed *.so files during runtime
Windows package
Installing OpenCV is easy through the following sources:

from chocolatey, also install llvm package, it's required for building:

choco install llvm opencv
also set OPENCV_LINK_LIBS, OPENCV_LINK_PATHS and OPENCV_INCLUDE_PATHS environment variables (see below for details).

from vcpkg, also install llvm package, necessary for building:

vcpkg install llvm opencv4[contrib,nonfree]
macOS package
Get OpenCV from homebrew:

homebrew, be sure to also install llvm and pkg-config that are required for building:
brew install llvm pkg-config opencv
Manual build
You can of course always compile OpenCV of the version you prefer manually. This is also supported, but it requires some additional configuration.

You need to set up the following environment variables to point to the installed files of your OpenCV build: OPENCV_LINK_LIBS, OPENCV_LINK_PATHS and OPENCV_INCLUDE_PATHS (see below for details).
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐