OpenCV4萌新之路——详解图像读取函数 “imread”
处理图像第一步当然是要读取一张图像,OpenCV给出的方法也很简单:
Mat cv::imread(const String& filename, int flags = IMREAD_COLOR);
一、函数简析
头文件 #include <opencv2/imgcodecs.hpp>
imread | 返回类型 | 命名空间 | 函数名 | 参数1 | 参数2 |
---|---|---|---|---|---|
详细 | Mat | cv | imread | const String& filename | int flags = IMREAD_COLOR |
详细 | - | - | - | 文件路径+文件名 | 图像读取模式 |
二、参数详解
1.String& filename
注:
imread函数支持读取的图像格式有
Windows bitmaps - *.bmp, *.dib (always supported)
JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section)
JPEG 2000 files - *.jp2 (see the Note section)
Portable Network Graphics - *.png (see the Note section)
WebP - *.webp (see the Note section)
Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
PFM files - *.pfm (see the Note section)
Sun rasters - *.sr, *.ras (always supported)
TIFF files - *.tiff, *.tif (see the Note section)
OpenEXR Image files - *.exr (see the Note section)
Radiance HDR - *.hdr, *.pic (always supported)
Raster and Vector geospatial data supported by GDAL (see the Note section)
2.flags = IMREAD_COLOR
值 | 枚举名 | 定义 | 解释 |
---|---|---|---|
-1 | IMREAD_UNCHANGED | If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation. | 如果设置,则按原样返回加载的图像(使用Alpha通道,否则会被裁剪) |
0 | IMREAD_GRAYSCALE | If set, always convert image to the single channel grayscale image (codec internal conversion). | 如果设置,则始终将图像转换为单通道灰度图像(编解码器内部转换) |
1 | IMREAD_COLOR | If set, always convert image to the 3 channel BGR color image. | 如果设置,请始终将图像转换为3通道BGR彩色图像 |
2 | IMREAD_ANYDEPTH | If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. | 如果设置,则在输入具有相应深度时返回16位/ 32位图像,否则将其转换为8位 |
4 | IMREAD_ANYCOLOR | If set, the image is read in any possible color format. | 如果设置,则以任何可能的颜色格式读取图像 |
8 | IMREAD_LOAD_GDAL | If set, use the gdal driver for loading the image. | 如果设置,使用gdal驱动程序加载图像 |
16 | IMREAD_REDUCED_GRAYSCALE_2 | If set, always convert image to the single channel grayscale image and the image size reduced 1/2. | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/2 |
17 | IMREAD_REDUCED_COLOR_2 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/2 |
32 | IMREAD_REDUCED_GRAYSCALE_4 | If set, always convert image to the single channel grayscale image and the image size reduced 1/4. | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/4 |
33 | IMREAD_REDUCED_COLOR_4 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/4 |
64 | IMREAD_REDUCED_GRAYSCALE_8 | If set, always convert image to the single channel grayscale image and the image size reduced 1/8. | 如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/8 |
65 | IMREAD_REDUCED_COLOR_8 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8. | 如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/8 |
128 | IMREAD_IGNORE_ORIENTATION | If set, do not rotate the image according to EXIF’s orientation flag. | 如果设置,请不要根据EXIF的方向标志旋转图像 |
三、测试代码
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#define IMAGE_ABSOLUTE_PATH "F:/Leraning_OpenCV4/Sakai_Izumi.jpg" //绝对路径
#define IMAGE_RELATIVE_PATH "./Sakai_Izumi.jpg" //相对路径
int main()
{
//读取图片
Mat src = imread(IMAGE_ABSOLUTE_PATH, IMREAD_COLOR);
//Mat src = imread(IMAGE_RELATIVE_PATH, 1);
//判读是否成功读取图片
if (src.empty())
{
std::cout << "Load img failed!" << endl;
return 0;
}
else
{
std::cout << "Load img success!" << endl;
}
//显示图片
imshow("src", src);
waitKey(0);
return 1;
}
1. 输入图像参数
2. 输出图像显示
3. 图像参数
4. 其他测试
ImreadModes 0 读取灰度图
ImreadModes 16 读取灰度图,宽、高都为原图的1/2
大家可以参考IMREAD_COLOR中不同的图像读取模式,对比一下显示图像,体会一下差异在哪。