mynote/opencv/make_a_video.md
2018-02-25 01:24:12 +08:00

55 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 利用opencv录制视频
``` cpp
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
VideoCapture capture(0);//如果是笔记本0打开的是自带的摄像头1 打开外接的相机
Mat img;
VideoWriter vw; //新建一个多媒体文件
namedWindow( "cam" );
int fps = capture.get(CAP_PROP_FPS ); //获取摄像头的帧率
cout << capture.get( CAP_PROP_FRAME_WIDTH )<<capture.get( CAP_PROP_FRAME_HEIGHT )<<fps<<endl;
if(fps <= 0 )fps = 25;
//设置视频的格式
vw.open( "out.avi", VideoWriter::fourcc( 'x', '2', '6', '4' ), fps, Size( capture.get( CAP_PROP_FRAME_WIDTH ), capture.get( CAP_PROP_FRAME_HEIGHT ) ) );
if(!capture.isOpened( )) //判断摄像头是否打开
{
cout << "open video faild";
return 0;
}
cout << "open video success" << endl;
if(!vw.isOpened()) //判断视频文件是否创建
{
cout << "open vw faild"<< endl;
}
cout << "open vw success" << endl;
while(1)
{
capture.read( img); //读取视频帧
if(img.empty( ))
break;
imshow("cam", img ); //显示视频帧
vw.write( img ); //将视频帧写入文件
if(waitKey( 10 ) == 'q') //q键退出录制
break;
}
return 0;
}
```