0
点赞
收藏
分享

微信扫一扫

openCV学习之路(2-1)---深度解析imread函数

小猪肥 2022-02-28 阅读 44

代码如下:

#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  

using namespace cv;
int main()
{
	// 读入一张图片(poyanghu缩小图)    
	Mat img = imread("D:\\faceDetection.png",0);
	// 创建一个名为 "图片"窗口    
	namedWindow("图片");
	// 在窗口中显示图片   
	imshow("图片", img);
	// 等待6000 ms后窗口自动关闭    
	waitKey(6000);
	return 0;
}

函数如下:

CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

@brief Loads an image from a file.
从文件加载图像

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 ).
*函数 imread 从指定文件加载图像并返回。 如果无法读取图像(由于缺少文件、权限不正确、格式不受支持或无效),该函数将返回一个空矩阵( Mat::data==NULL )。
*
Currently, the following file formats are supported:
目前,支持以下文件格式:

  • 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 *.pxm, *.pnm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)
  • OpenEXR Image files - *.exr (see the Notes section)
  • Radiance HDR - *.hdr, *.pic (always supported)
  • Raster and Vector geospatial data supported by Gdal (see the Notes section)

Note:

  • The function determines the type of an image by the content, not by the file extension.
  • 该函数通过内容而不是文件扩展名确定图像的类型。
  • In the case of color images, the decoded images will have the channels stored in B G R order.
  • 在彩色图像的情况下,解码后的图像将以 B G R 顺序存储通道。
  • 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 ofthe color management embedded into MacOSX.
  • 在 Microsoft Windows OS 和 MacOSX 上,默认使用 OpenCV 图像随附的编解码器(libjpeg、libpng、libtiff 和 libjasper)。 因此,OpenCV 始终可以读取 JPEG、PNG 和 TIFF。 在 MacOSX 上,还有一个使用原生 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.
  • 在 Linux、BSD 风格和其他类似 Unix 的开源操作系统上,OpenCV 会查找随操作系统映像提供的编解码器。 安装相关软件包(不要忘记开发文件,例如,Debian* 和 Ubuntu* 中的“libjpeg-dev”)以获得编解码器支持或在 CMake 中打开 OPENCV_BUILD_3RDPARTY_LIBS 标志。
  • In the case you set WITH_GDAL flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image, then [GDAL](http://www.gdal.org) driver will be used in order to decode the image by supporting the following formats: [Raster](http://www.gdal.org/formats_list.html),[Vector](http://www.gdal.org/ogr_formats.html).
  • 如果您在 CMake 中将 WITH_GDAL 标志设置为 true 并 @ref IMREAD_LOAD_GDAL 加载图像,则 [GDAL][(http://www.gdal.org](http://www.gdal. org) 驱动程序将用于通过支持以下格式来解码图像:[Raster](http://www.gdal.org/formats_list.html),[Vector](http://www.gdal.org/ogr_formats.html)。
  • If EXIF information are embedded in the image file, the EXIF orientation will be taken into account and thus the image will be rotated accordingly except if the flag.
  • 如果图像文件中嵌入了 EXIF 信息,则将考虑 EXIF 方向,因此图像将相应旋转,除非标志。

@param filename Name of file to be loaded.
@param filename 要加载的文件名。
@param flags Flag that can take values of cv::ImreadModes.
@param flags 可以采用 cv::ImreadModes 值的标志。

//! Imread flags
enum ImreadModes {
       IMREAD_UNCHANGED = -1, // !< 如果设置原样返回加载的图像(使用 alpha 通道,否则会按按钮,则图像)。
       IMREAD_GRAYSCALE = 0, //!< 如果设置,则始终将图像转换为单通道灰度图像。
       IMREAD_COLOR = 1, //!< 如果设置,则始终将图像转换为 3 通道 BGR 彩色图像。
       IMREAD_ANYDEPTH = 2, //!< 如果设置,则在输入具有相应深度时返回 16 位/32 位图像,否则将其转换为 8 位。
       IMREAD_ANYCOLOR = 4, //!< 如果设置,则以任何可能的颜色格式读取图像。
       IMREAD_LOAD_GDAL = 8, //!< 如果设置,则使用 gdal 驱动程序加载图像。
       IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸缩小 1/2。
       IMREAD_REDUCED_COLOR_2 = 17, //!< 如果设置,则始终将图像转换为 3 通道 BGR 彩色图像,并且图像尺寸减小 1/2。
       IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小 1/4。
       IMREAD_REDUCED_COLOR_4 = 33, //!< 如果设置,则始终将图像转换为 3 通道 BGR 彩色图像,并且图像尺寸减小 1/4。
       IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸缩小 1/8。
       IMREAD_REDUCED_COLOR_8 = 65, //!< 如果设置,则始终将图像转换为 3 通道 BGR 彩色图像,并且图像尺寸减小 1/8。
       IMREAD_IGNORE_ORIENTATION = 128 //!< 如果设置,则不要根据 EXIF 的方向标志旋转图像。
     };
举报

相关推荐

0 条评论