利用opencv读取摄像头并保存视频

本文介绍了一种使用OpenCV从摄像头捕获视频并将其保存为文件的方法。通过C++代码示例展示了如何初始化摄像头捕获设备、设置视频写入参数、循环读取并显示每一帧图像,并最终将这些帧写入到指定的视频文件中。


#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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值