opencv作为一款强大的机器视觉库,以其简便性得到了各图像处理开发人员的青睐。现在就给大家介绍如何用opencv实现定时录像并以实际时间作为文件名保存。之前网上已经有一些类似的代码,但是大多数网友反映程序无法执行,主要分析有两个原因。电脑上未安装视频编码器,这里推荐大家XviD,网上不好下载可以私信我或是留下邮箱,我统一发送。然后在选择编码格式上,我选择的是'X', 'V', 'I', 'D'格式,也就是我们常见的avi格式。
#include "cv.h" #include "cxcore.h" #include "highgui.h" #include#include #include #include #include "stdio.h" int timea=100000; using namespace std; int main() { CvCapture* capture=cvCaptureFromCAM(0); CvVideoWriter* video=NULL; IplImage* frame=NULL; int n; if(!capture) //如果不能打开摄像头给出警告 { cout<<"Can not open the camera."< width,frame->height)); //创建CvVideoWriter对象并分配空间 //保存的文件名为camera.avi,编码要在运行程序时选择,大小就是摄像头视频的大小,帧频率是32 if(video) //如果能创建CvVideoWriter对象则表明成功 { cout<<"VideoWriter has created."< >timea; int ti=timea*25; cvNamedWindow("Camera Video",1); //新建一个窗口 int i = 0; while(i <= ti) // 让它循环ti次自动停止录取 { frame=cvQueryframe(capture); //从CvCapture中获得一帧 if(!frame) { cout<<"Can not get frame from the capture."< 0) break; //有其他键盘响应,则退出 } cvReleaseVideoWriter(&video); //如果不释放则春不上 cvReleaseCapture(&capture); cvDestroyWindow("Camera Video"); } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



