Skip to content

Instantly share code, notes, and snippets.

@0xlitf
Created June 20, 2016 08:45
Show Gist options
  • Save 0xlitf/b77fef67590e68ecd0729612538d8704 to your computer and use it in GitHub Desktop.
Save 0xlitf/b77fef67590e68ecd0729612538d8704 to your computer and use it in GitHub Desktop.
opencv录制视频
#include <highgui/highgui.hpp>
#include <cv.h>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
CvCapture* capture = cvCaptureFromCAM(0);
int FrameW = 320;
int FrameH = 240;
IplImage *FrameCopy = cvCreateImage(cvSize(FrameW, FrameH), 8, 3);
if (!capture)
{
cout << "Can not open the camera." << endl;
system("pause");
return -1;
}
auto frame = cvQueryFrame(capture);
CvVideoWriter* video = cvCreateVideoWriter("P15.avi",
CV_FOURCC('M', 'J', 'P', 'G'), //这一位控制是否弹出视频设置窗口,-1为弹出设置界面
8,
cvSize(FrameW, FrameH)); //创建CvVideoWriter对象并分配空间
//保存的文件名为camera.avi,编码要在运行程序时选择,大小就是摄像头视频的大小,帧频率是32
if (video) //如果能创建CvVideoWriter对象则表明成功
{
cout << "视频写入对象已创建." << endl;
}
cvNamedWindow("Camera Video", 1); //新建一个窗口
while (1)
{
frame = cvQueryFrame(capture); //从CvCapture中获得一帧
if (!frame)
{
cout << "无法获得视频幀." << endl;
break;
}
cvResize(frame, FrameCopy, 1);
if (cvWriteFrame(video, FrameCopy)) {//判断是否写入成功,如果返回的是1,表示写入成功
cout << "写入ok" << endl;
}
cvShowImage("Camera Video", frame); //显示视频内容的图片
if (cvWaitKey(2) > 0) break; //有其他键盘响应,则退出
}
cvReleaseVideoWriter(&video);
cvReleaseCapture(&capture);
cvReleaseImage(&FrameCopy);
cvDestroyWindow("Camera Video");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment