1.读写图像
#include <iostream>
#include <string>
#include <sstream>
//OpenCV提供的跨平台I/O函数core和highgui
//core用于基本的图像数据处理,包含基本类,比如矩阵
//highgui包含读函数、写函数以及用图形界面显示图像的函数
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
//imread是读取图像的主函数,此函数打开图像并以矩阵格式存储它
//imread(”图像路径字符串“,用于指定要加载的图像类型<默认为彩色图像;可选的>)
Mat color = imread("lena.jpg");
Mat gray = imread("lena.jpg", IMREAD_GRAYSCALE);
if (! color.data)
{
cout << "Could not open or find the image" << endl;
return 1;
}
//imwrite函数用于保存图像
//imwrite("保存图像的路径和图像的扩展名格式",要保存的矩阵图像)
imwrite("lenaGray.jpg", gray);
//通过使用矩阵的.cols和.rows属性访问图像的列数和行数,即图像的宽度和高度
int myRow = color.cols-1;
int myCol = color.rows-1;
//访问图像的一个像素
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value(R,G,B): (" << (int)pixel[0] << ","<< (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
//imshow函数显示图像
//imshow("图像标题",图像矩阵)
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
//waitKey函数参数为要等待按键的毫秒数,如果参数为0,则这个函数会一直等待下去,直到用户按下某个键
waitKey(0);
return 0;
}
知识补充
结果
2.读取视频和摄像头
#include <iostream>
#include <string>
#include <sstream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
//CommandLineParser类可以帮助我们管理输入命令行参数
//我们必须为CommandLineParser做的第一件事是在常量char向量中定义我们需要或允许的参数,每一行必须采取以下模式:
// ”{name_param | default_value | description}“
//name_param可以以@开头,这会将此参数定义为默认输入
const char* keys =
{
"{help h usage ? | |print this message}"
"{@video | |Video file, if not defined try to use webcamera}"
};
int main(int argc, const char** argv)
{
//构造函数将获取main函数的输入和先前定义的key常量
CommandLineParser parser(argc, argv, keys);
parser.about("Chapter2. V1.0.0");
//.has类方法检查参数是否存在
//此程序中用于检查用户是否添加参数help或?
//然后用类函数显示所有描述的参数
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
//.get<typeName>(parameterName)函数可以访问和读取任何输入参数
String videoFile = parser.get<String>(0);
//检查获取的参数是否被正确解析,并在其中一个参数未被解析时显示错误信息
if (! parser.check())
{
parser.printErrors();
return 0;
}
//VideoCapture类属于videoio子模块,用于视频读取和摄像头读取
//创建对象后,检查输入的命令行参数videoFile是否有路径文件名,如果是空的,尝试打开网络摄像头;如果有文件名,则打开视频文件
//open函数将视频文件名或我们要打开的索引摄像头作为参数,如果我们有一个摄像头,可以用0作为参数
VideoCapture cap;
if (videoFile != "")
cap.open(videoFile);
else
cap.open(0);
//isOpened函数检查是否可以读取视频文件名或者摄像头
if (!cap.isOpened())
return -1;
//创建一个窗口,使用namedWindow函数和无限循环来显示帧
//用>>操作抓取每一帧,如果正确检索到帧,使用imshow函数显示帧
//waitKey(30)等待30毫秒,检查用户是否使用任何键停止应用程序的执行
namedWindow("Video", 1);
for(;;)
{
Mat frame;
cap >> frame;
if (frame.empty())
return 0;
imshow("Video", frame);
if (waitKey(30) >= 0)
break;
}
//release函数释放矩阵,释放所有的视频资源
cap.release();
return 0;
}
结果
3、基本数据存储
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char** argv)
{
//FileStorage类把一些OpenCV或其他数值数据写入文件,同时使用流运算符<<操作STL流
//创建保存数据的文件,调用构造函数,并提供包含所需扩展名格式的路径文件名(XML或YAML)以及第二个要写入的参数集
FileStorage fs("test.yml", FileStorage::WRITE);
int fps = 5;
fs << "fps" << fps;
Mat m1 = Mat::eye(2, 3, CV_32F);
Mat m2 = Mat::ones(3, 2, CV_32F);
//mul函数乘以矩阵的每一个元素
Mat result = (m1+1).mul(m1+3);
fs << "Result" << result;
fs.release();
//通过调用FileStorage构造函数并使用适当的参数、路径和FileStorage::READ来打开一个保存的文件
FileStorage fs2("test.yml", FileStorage::READ);
Mat r;
fs2["Result"] >> r;
std::cout << r << std::endl;
fs2.release();
return 0;
}
结果
[8, 3, 3;
3, 8, 3]