Mat imread(const String& filename,int flags = IMREAD_COLOR);
返回Mat对象;
需要注意的是imread读取数据时会重新排列数据。
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)
参数flags:打开的参数,这个非常重要,因为如果设置不合适的话,很容易出现预想之外的效果。它可以指导将原图读取时进行一定的转换。默认值是IMREAD_LOAD_GDAL。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。
1 通道编码顺序
通道,与像素深度深度有关。灰度图通常是8比特的像素深度,则通道数为1。如果是彩色图,且为RGB编码,那么一般为24比特的像素深度,通道数为3。而有的彩色图的像素深度是16或者32比特。16比特可能有多种情况:一是压缩的RGB格式,二是YUV的输出。无论何种,都是只有2通道,需要手动解析分离。32比特(windows *.bmp)的像素深度对应的彩色图,则表示的是4通道,RGBA,多出的A表示的是透明度的索引。
另外读取时需要注意内部像素的编码顺序,这也依赖于imread的flags选项的取值,如果取值决定转成RGB,那么正常的顺序是BGR,排列顺序如下图所示。如果最后imread输出是四通道,多了Alpha通道,那么顺序是RGBA。
IMREAD_UNCHANGED
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image.
IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH
If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR
If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL
If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2
If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4
If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8
If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
Ref:https://blog.csdn.net/firstlai/article/details/70882240