天天看点

OpenCV3 图像读取和保存 ( imread() 和 imwrite() )

目录

HighGUI模块

cv::imread()

CV::imwrite()

HighGUI模块

OpenCV把用于操作系统、文件系统以及摄像机等硬件设备交互的函数纳入到HighGUI(High-level Graphical User Interface)模块中 。有了HighGUI模块,对于窗口打开、显示图像、读出或者写入图像相关的文件(包含图像和视频)、处理简单的鼠标点击、鼠标移动和键盘事件,创建一些有用的控件,eg:滑动条,并将其融入窗口之中。

HighGUI库可以划分三个部分:硬件相关部分、文件系统部分、图形用户界面。

cv::imread()

/*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:

-   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.
-   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.
-   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.
-   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).
-   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 @ref IMREAD_IGNORE_ORIENTATION is passed.
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes
*/
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
           

cv::imread()读取一张图片时,cv::imread()并不关心文件的扩展名是什么,而是分析文件中前几个字节(被称为文件的识别标识或者“魔法序列”)来确定图像的编码格式,第二个参数flags可以设置为一下的格式。

{
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image.
       IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
       IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
       IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
       IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
       IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
       IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
       IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
       IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
}
           

使用cv::imread()读取一张图片时,并不关心文件的扩展名是什么,而是分析文件中前几个字节(被称为文件识别标识或者“魔法序列”)来确定图像的编码格式。

第二个参数flags可以被设置为上述表格中的任意一个值,默认情况下,flag被设置为cv::IMREAD_COLOR。这个值表示图片将被以三通道8位的格式读取。在这种情况下即使原图像是灰度图像,读到内存中仍然有三通道,每个通道拥有相同数据的图像,如果flags被设置为cv::IMREAD_GRAYSCALE,那么不管文件内部图像是几通道,图像都将以灰度图的格式加载。当flags是cv::IMREAD_ANYCOLOR。在此种情况下,图片的载入方式取决于其内部的图像的情况,如果是彩色图像的就以三通道的形式读取,如果是灰度图,则按单通道的形式载入。 

cv::IMREAD_UNCHANGED具有另一个独特的效果:读取图像的时候,它将保留图像中的alpha通道。

cv::IMREAD_ANYDEPTH标志,表明输入的通道数据大于8位,使得图像载入过程不会发生数据类型转换(分配用于存储数据的数组类型将会由其内部数据类型决定)。

cv::imread()在载入图像失败时,它不会抛出异常,而是仅返回一空的cv::Mat,可以使用cv::Mat::empty()==true来判断。

CV::imwrite()

imwrite()函数与cv::imread()函数形成功能互补关系,输入为三个参数,如下所示

bool cv::imwrite
( const string& filename,
  InputArray img,
  const vector<int>& params=vector<int>()
);
           

第一个参数给定了文件名,文件名的扩展部分用来决定以何钟格式保存图像。

第二个参数是待存储的输入图像。第三个参数被用作特殊类型文件的写入操作时所需要的数据。输入参数为内部整型数据的一个STL vector中的整型序列的具体内容为:一系列的参数ID,以及与该参数对应的参数值,每一个参数ID之后跟着其对应的值。

IMWRITE_JPEG_QUALITY =1,
    IMWRITE_PNG_COMPRESSION =16,
    IMWRITE_PNG_STRATEGY =17,
    IMWRITE_PNG_BILEVEL =18,
    IMWRITE_PNG_STRATEGY_DEFAULT =0,
    IMWRITE_PNG_STRATEGY_FILTERED =1,
    IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2,
    IMWRITE_PNG_STRATEGY_RLE =3,
    IMWRITE_PNG_STRATEGY_FIXED =4,
    IMWRITE_PXM_BINARY =32
           
标志 含义 取值范围 默认值
IMWRITE_JPEG_QUALITY JPEG的质量 0~100 95
IMWRITE_PNG_COMPRESSION  PNG压缩值(更高意味着更多的压缩) 0~9 3
IMWRITE_PXM_BINARY  对PPM,PGM或PBM文件是否使用二值格式 0或1 1

cv::imwrite()函数会将大多数格式的图像以8位单通道或多通道形式保存,但对于像PNG、TIFF或者JPG2000这些非常灵活的文件格式,则允许以16位甚至是浮点数格式保存,有些可以保存4通道(BGRA),如果成功保存则返回true,否则通常会返回false(有些会报异常)。

继续阅读