您的位置:首页 > 运维架构

opencv摄像头拍摄视频并保存方法

2017-01-13 22:09 204 查看

一. 读取视频序列

OpenCV提供了一个简便易用的框架以提取视频文件和USB摄像头中的图像帧,如果只是单单想读取某个视频,你只需要创建一个cv::VideoCapture实例,然后在循环中提取每一帧。这里利用摄像头拍摄视频并保存成avi文件,代码如下:

#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\core\core.hpp>
using namespace cv;
using namespace std;
int main()
{
//打开摄像头
VideoCapture captrue(0);
//视频写入对象
VideoWriter write;
//写入视频文件名
string outFlie = "E://fcq.avi";
//获得帧的宽高
int w = static_cast<int>(captrue.get(CV_CAP_PROP_FRAME_WIDTH));
int h = static_cast<int>(captrue.get(CV_CAP_PROP_FRAME_HEIGHT));
Size S(w, h);
//获得帧率
double r = captrue.get(CV_CAP_PROP_FPS);
//打开视频文件,准备写入
write.open(outFlie, -1, r, S, true);
//打开失败
if (!captrue.isOpened())
{
return 1;
}
bool stop = false;
Mat frame;
//循环
while (!stop)
{
//读取帧
if (!captrue.read(frame))
break;
imshow("Video", frame);
//写入文件
write.write(frame);
if (waitKey(10) > 0)
{
stop = true;
}
}
//释放对象
captrue.release();
write.release();
cvDestroyWindow("Video");
return 0;
}运行后发现E盘目录下产生了一个名为“fcq.avi”的视频文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  摄像头 opencv