#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(void)
{
//摄像头对应capture(0)
VideoCapture capture(0);
VideoWriter writer;
//保存的avi视频的名称
string outfile="VideoTest.avi";
//摄像头捕获的图像的高宽,即保存avi视频的高宽
int w=static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
int h=static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
Size S(w,h);
//设置视频文件的参数,编码格式参数CV_FOURCC,帧率20帧/s,
writer.open(outfile, CV_FOURCC('M','J','P','G'), 20, S,true);
//摄像头采集的一帧图像存到变量frame中
Mat frame;
//循环采集,保存
while (capture.isOpened())
{
//从摄像头得到一帧图像,存入frame
capture >> frame;
//将图像frame存入视频文件
writer << frame;
//显示图像
imshow("video", frame);
//按Esc键退出
if (cvWaitKey(20) == 27)
{
break;
}
}
//释放空间
capture.release();
writer.release();
return 0;
}