OpenCV4萌新之路——详解图像读取函数 “imread”


处理图像第一步当然是要读取一张图像,OpenCV给出的方法也很简单:

Mat cv::imread(const String& filename, int flags = IMREAD_COLOR);

一、函数简析

头文件 #include <opencv2/imgcodecs.hpp>

imread返回类型命名空间函数名参数1参数2
详细Matcvimreadconst String& filenameint 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

枚举名定义解释
-1IMREAD_UNCHANGEDIf set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.如果设置,则按原样返回加载的图像(使用Alpha通道,否则会被裁剪)
0IMREAD_GRAYSCALEIf set, always convert image to the single channel grayscale image (codec internal conversion).如果设置,则始终将图像转换为单通道灰度图像(编解码器内部转换)
1IMREAD_COLORIf set, always convert image to the 3 channel BGR color image.如果设置,请始终将图像转换为3通道BGR彩色图像
2IMREAD_ANYDEPTHIf set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.如果设置,则在输入具有相应深度时返回16位/ 32位图像,否则将其转换为8位
4IMREAD_ANYCOLORIf set, the image is read in any possible color format.如果设置,则以任何可能的颜色格式读取图像
8IMREAD_LOAD_GDALIf set, use the gdal driver for loading the image.如果设置,使用gdal驱动程序加载图像
16IMREAD_REDUCED_GRAYSCALE_2If set, always convert image to the single channel grayscale image and the image size reduced 1/2.如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/2
17IMREAD_REDUCED_COLOR_2If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/2
32IMREAD_REDUCED_GRAYSCALE_4If set, always convert image to the single channel grayscale image and the image size reduced 1/4.如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/4
33IMREAD_REDUCED_COLOR_4If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/4
64IMREAD_REDUCED_GRAYSCALE_8If set, always convert image to the single channel grayscale image and the image size reduced 1/8.如果设置,则始终将图像转换为单通道灰度图像,图像尺寸减小1/8
65IMREAD_REDUCED_COLOR_8If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.如果设置,则始终将图像转换为3通道BGR彩色图像,图像尺寸减小1/8
128IMREAD_IGNORE_ORIENTATIONIf 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中不同的图像读取模式,对比一下显示图像,体会一下差异在哪。

四、参考文献

  1. Image file reading and writing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值