opencv 操作本地摄像头实现录像

时间:2022-06-18 16:03:26

直接上代码:

// demo1.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
using namespace std;
#include <core/core.hpp>
#include <highgui/highgui.hpp>
#include <cmath>
#include <ctime>
using namespace cv; int main()
{
Mat frame; //mat数据类型
VideoCapture capture(0);
capture >> frame; //流操作
VideoWriter writer("e:\\VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(frame.cols,frame.rows));
int f=frame.cols;
int p=frame.rows;
//cout<<frame.rows<<endl;
while (capture.isOpened())
{
int start=clock();
capture >> frame; //流操作
writer << frame;
imshow("video", frame);
if (cvWaitKey(30) == 113) //按下q键 程序退出
{
break;
}
cout<<"程序执行过程耗时:"<<clock()-start<<"毫秒"<<endl;
}
cout<<"图像宽度为:"<<frame.cols<<endl;
cout<<"图像高度为:"<<frame.rows<<endl;
cout<<frame.size<<endl;
return 0;
}