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

1439

被折叠的 条评论
为什么被折叠?



