opencv4 imread函数第二个函数解析

    小白最近在学习opencv4,网上找的毛星云毛老师的opencv3编程入门,但自己下载的是opencv4,所以遇到了很多问题,百度到的各种文章都是千篇一律,最后自己看源码才得到解决。正是应了一句话,少百度,多源码。。开帖纪念下自己小白的发展史。

函数原型:Mat imread( const String& filename, int flags = IMREAD_COLOR );

  • 参数一:文件名。注意应是以项目根目录为基础的相对路径。
  • 参数二:imreadModes。取值从opencv4来看,较opencv2,3多了几个特定值,所以毛老师的opencv3书中有说flag>0来判定加载3通道彩图,灰阶还是aplha通道。所以在后来的flag取值了199,在opencv2,3是和1一样的,但是在opencv4中,效果不一样了。主要原因是扩展了flag值。

opencv4 flag值:

enum ImreadModes {
       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 (codec internal conversion).
       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.
     };

对于199这个值,实际显示效果是缩小了8倍的3通道。具体计算显示出是什么效果,其实还是转成二进制按位与比较。

你可能感兴趣的:(opencv,opencv)