opencv图像读入与输出详解
1. 输入参数分析
/*
接口函数:CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
分析:
输入参数: 1. filename: 输入图像文件名称,支持jpg,png等常用图像格式
2. flags 数据转换表示,enum枚举类型,具有如下格式,数据存储在 cv::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.
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.
*/
2. 显示一张图像
// 接口函数: CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
/*
1. CV_EXPORTS_W 是宏定义 __declspec(dllexport), 用于修饰 DLL 导出数据、函数、类或类成员函数。
以解决不同版本,不同编译器之间没有统一标准规范而导致编译出错的问题。opencv类库中,大量使用这种
方式修饰 数据、函数、类或类成员函数。
2. 参数分析: winname,是窗体名称,字符串类型
InputArray, 通用数据输入,通过构造的方式支持多种数据格式输入,主要包含 cv::Mat,cuda::GpuMat,std::vector
ogl::Buffer 及其模板形式等多种输入。
*/
3. 保存图像
/*
接口函数:CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
const std::vector<int>& params = std::vector<int>());
参数分析:
输入参数:1.filename,文件名,字符串类型;
2. img, 通用输入类型 InputArray;
3. params, 自定义数据存储,使用默认即可;
返回参数:Bool类型,如果保存成功,则返回真值
*/
代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
//using namespace cv;int main(int argc, char** argv) {
cv::String imgPath = "F:/软件开发/qt开发/08opencv/opencv素材/Image/";
//cv::String fileName = "Luffy.png";
cv::String fileName = "opencvlog.jpg";
cv::String pathFile = imgPath + fileName;
cv::Mat src;
// 1. 读入空图像,
src = cv::imread("");
if (src.data == NULL) { // 没有读入数据时,返回空。 src.data 为uchar* 指针,用来只想数据的其实位置
printf("图像读入失败,sizeof(src.data) = %d,\n",sizeof(src.data));
}// 2.以默认值读入一张图像
src = cv::imread(pathFile); // 采用默认方式读取图像
if (src.data == NULL) {
printf("图像赌徒失败\n");
return -1;
}
cv::imshow("1. 默认读图", src); // 显示一张图像
// 3.图像转换成灰度图像读图
src = cv::imread(pathFile, cv::IMREAD_GRAYSCALE); // 采用默认方式读取图像
cv::imshow("2. 转换成灰度读图", src); // 显示一张图像
// 4.图像放大两倍
src = cv::imread(pathFile, cv::IMREAD_REDUCED_GRAYSCALE_2); // 采用默认方式读取图像
cv::imshow("3. 图像缩小两倍,并灰度化", src); // 显示一张图像
// 5.图像缩小两倍
src = cv::imread(pathFile, cv::IMREAD_REDUCED_COLOR_2); // 采用默认方式读取图像
cv::imshow("4. 图像缩小两倍", src); // 显示一张图像
// 6. 显示一张图像
// 接口函数: CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
/*
1. CV_EXPORTS_W 是宏定义 __declspec(dllexport), 用于修饰 DLL 导出数据、函数、类或类成员函数。
以解决不同版本,不同编译器之间没有统一标准规范而导致编译出错的问题。opencv类库中,大量使用这种
方式修饰 数据、函数、类或类成员函数。
2. 参数分析: winname,是窗体名称,字符串类型
InputArray, 通用数据输入,通过构造的方式支持多种数据格式输入,主要包含 cv::Mat,cuda::GpuMat,std::vector
ogl::Buffer 及其模板形式等多种输入。
*/
// 7。保存图像
/*
接口函数:CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
const std::vector<int>& params = std::vector<int>());
参数分析:
输入参数:1.filename,文件名,字符串类型;
2. img, 通用输入类型 InputArray;
3. params, 自定义数据存储,使用默认即可;
返回参数:Bool类型,如果保存成功,则返回真值*/
bool saveFlag = cv::imwrite("E:/saveImg.jpg", src);
printf("saveFlag=%d\n",saveFlag);cv::waitKey(0); // 等待按键,输入为整形,单位为ms,返回为按键ACII码
//system("pause");
return 0;
}
输出结果: