使用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来判断。
{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::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
cv::imwrite()函数会将大多数格式的图像以8位单通道或多通道形式保存,但对于像PNG、TIFF或者JPG2000这些非常灵活的文件格式,则允许以16位甚至是浮点数格式保存,有些可以保存4通道(BGRA),如果成功保存则返回true,否则通常会返回false(有些会报异常)。
https://blog.csdn.net/u012785169/article/details/96974928
https://blog.csdn.net/mao_hui_fei/article/details/78413268