#include <opencv2/core/mat.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap;
cap.open(1); //打开相机
VideoWriter writer;
double fps = 15.0; //保存视频的帧率
int coder = VideoWriter::fourcc('I', '4', '2', '0'); //保存视频的编码格式
string str = "C:/project/shipin.avi"; //保存视频的路径及后缀
bool flag = true;
Mat src;
while (1)
{
cap >> src;
if (flag)
{
writer.open(str, coder, fps, src.size(), true); //保存视频的格式
flag = false;
}
writer.write(src);
imshow("原图", src);
waitKey(1);
}
writer.release();
return 0;
}
保存视频要注意视频编码格式与后缀相对应,如fourcc('I', '4', '2', '0')与.avi相对应,其他的对应格式可从网上搜索找到